This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes need a little help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need a little help" Watch "need a little help" New topic
Author

need a little help

Iqbal Habibie
Greenhorn

Joined: Dec 04, 2013
Posts: 5
I have an even number 1 to 10 and each number have a frequency that comes up.

example the number 2,4,6,8 then the frequency of each number are 2,1,0,0.It means that we can use up to two digits of 2 and one digit of 4. There are exactly 8 distinct numbers that can be
constructed using the above digits: 2, 4, 22, 24, 42, 224, 242, 422. The sum of all those numbers is 982. but the first line of input contains an integer T (T ≤ 500) denoting the number of testcases. Each testcase contains nine (not four) integers Pi (0 ≤ Pi ≤ 9) denoting the number of i-th digit for i = 1..9.
And the Output For each test case, the output contains a line in the format Case #x: M, where x is the case number (starting from 1) and M is the output in a single line the sum of all possible numbers generated from the available digits. Modulo the output with 1,000,000,007.

I have tried to sum up 1 to 1000. How I settle to the case above?

Code:
Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 843
    
  14

How would YOU, not Java, solve the task?
StopCoding <- click and read


Formely Pawel Pawlowicz
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
And welcome to the Ranch
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 449
    
    5
Alway a wise advise, the question how an OP would do it by hand. And I am curious to what OP
will come up with.

But having read three times OP's question, I must admit: that's not a trivial question!
It is certainly very different from the sum that OP does in his code.

The problem being equal to: you have a collection containing k white marbles, m red, n yellow, p green, ...
Then form every possible subcollection from these marbles, where wwgw is different from wgww,
value each marble according to the place where it is in the subcollection, and then find the total sum
of all these subcollections (again, if I understand OP correctly).

Not trivial at all. And something tells me that there must be a formula that calculates, at once, the
required sums, given the frequencies. But what that formula should be, I have, as yet, no clue.

Interesting!

Greetz,
Piet
Sresh Rangi
Ranch Hand

Joined: Nov 28, 2012
Posts: 45
    
    1
The constraints are too high to be adding individual values together. You want to rewrite the sum to use multiplication and factorials where possible.

Take your example, you want to calculate: 2 + 4 + 22 + 24 + 42 + 224 + 242 + 422

This is the sum of numbers of length 1, 2, 3. Take the ones of length 3 for example:
224 + 242 + 422

Break it into hundreds, tens, and units:
(2*100 + 2*10 + 4*1) + (2*100 + 4*10 + 2*1) + (4*100 + 2*10 + 2*1)

and regroup the values:
(2*2*100 + 4*1*100) + (2*2*10 + 4*1*10) + (2*2*1 + 4*1*1)
(2*2*100 + 2*2*10 + 2*2*1) + (4*1*100 + 4*1*10 + 4*1*1)
2*2*(100+10+1) + 4*1*(100+10+1)

So the digit '2' in the 3 digit numbers contributes a value to the total equal to: 2 * 2 * ((10^3 - 1) / 9)
The first '2' is the digit, the second is the number of combinations of possible digits remaining after using the '2', and '3' is the number of digits.

So there's a subproblem here: Calculate the number of combinations possible after removing a digit. This should be easier than the original problem but it's still not obvious. I suspect something based on binomial coefficients or the inclusion/exclusion principle should do it (all possibilities minus the union of possibilities that violate the constraints for each digit).

Do you have a link to the testcases or the original problem?
Sresh Rangi
Ranch Hand

Joined: Nov 28, 2012
Posts: 45
    
    1
Does every digit in the sum need to be even? That would reduce the possibilities so you use a more brute-force solution.
Iqbal Habibie
Greenhorn

Joined: Dec 04, 2013
Posts: 5
I have try improving from above to this,but i am confuse :
1). Each case add number if you choose a number, example below i choose 2,4,22,24,42,224,242,422 then i sumup but i don't get into loop of the case. if i input sample Input:3,the ouput should like Case#1:982;then sample Input : 0 2 0 1 0 0 0 0 0 ,the ouput should like Case#2:2
2). How i add the Modulo 1,000,000,007?

Here's my code


The output :
Enter the size of the input you want to enter: 8
Enter 8 numbers: 2 4 22 24 42 224 242 422
Output 500 times
Digits Frequency
0 0
1 91
2 85
3 73
4 85
5 77
6 89
7 0
8 0
9 0
The sum of the numbers is: 982BUILD SUCCESSFUL (total time: 20 seconds)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
No, don't use that switch. Use an array and the digits can be the indices for the array.
Iqbal Habibie
Greenhorn

Joined: Dec 04, 2013
Posts: 5
Indices array,could you give me some idea?thank you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
Create a ten‑element int[] and use each element as a counter.
Iqbal Habibie
Greenhorn

Joined: Dec 04, 2013
Posts: 5
Do you create ten elements int() like int feequency above?

And how I'm gonna get sampel input by 3 and the output case #1:982, and there is 3 case?is it using by print the line?
 
jQuery in Action, 2nd edition
 
subject: need a little help
 
Similar Threads
Happy/unhappy numbers
Product problem
Binary Search problem
Help with Perfect Number program
For Loop