11076-add Again
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php? Option=com_onlinejudge&itemid=8&category=467&page=show_problem&problem=20 17
Summation of sequence of integers are always a common problem in Computer. Rather than computing blindly some intelligent make the task techniques. Here you are have to find the summation of a sequence of integers. The sequence is a interesting one and it are the all possible permutations of a given set of digits. For example, if the digits are <1 2 3>, then six; possible permutations are <123> <132> T;231>, <312>, <321> and the sum of them is 1332.
Input
Each input set would start with a positive integer N (1≤n≤12). The next line would contain N decimal digits. Input is terminated by n=0. There'll is at most 20000 test set.
Output
For each test set, there should is a one line output containing the summation. The value would fit in 64-bit unsigned integer.
Sample input Output for sample input
Thinking: the idea of average
Because each number appears in the same number of places,
So the average number of ans= per bit *n a 1
For example <1 1 2 2>, we have:
Complete code:
/*0.042s*/
#include <cstdio>
#include <cstring>
const long Long one[13] =
{
0, 1, 11, 1111, 11111, 111111, 1111111, 11111111, 111111111, 1111111111, 11111111111, 111111111111
};
Long Long a[10], fac[13];//factorial
int main (void)
{
int n, num, count;
Long ans;
Fac[0] = 1;
for (int i = 1; I <= i++)
Fac[i] = i * fac[i-1];///compute factorial
while (scanf ("%d", &n), N)
{
MEMS ET (A, 0, sizeof (a));
Count = 0;
for (int i = 0; i < n; i++)
{
scanf ("%d", &num);
Count + = num;
a[num]++;
}
Ans = fac[n-1] * count;
for (int i = 0; i < ++i)
ans/= fac[a[i]];
printf ("%lld\n", ans * one[n]);
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/