ZJUT Practice 3.10 MAYBE JIANDAN! G - Add Again(uva 11107)

Problem C Add Again Input: Standard Input

Output: Standard Output

Summation of sequence of integers is always a common problem in Computer Science. Rather than computing blindly, some intelligent techniques make the task simpler. Here you have to find the summation of a sequence of integers. The sequence is an interesting one and it is 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>, <213>, <231>, <312>, <321> and the sum of them is 1332.

Input

Each input set will start with a positive integer N (1≤N≤12). The next line will contain N decimal digits. Input will be terminated by N=0. There will be at most 20000 test set.

Output

For each test set, there should be a one line output containing the summation. The value will fit in 64-bit unsigned integer.

Sample Input                             Output for Sample Input

3

1 2 3

3

1 1 2

0

 

1332

444

 

 

Problemsetter: Md. Kamruzzaman

Special Thanks: Shahriar Manzoor

 

 1 #include<iostream>
 2 #include<string.h>
 3 #include<stdio.h>
 4 using namespace std;
 5 int a[10],permu[13]={1};
 6 int main(){
 7     for(int i=1;i<=12;i++)
 8         permu[i]=i*permu[i-1];
 9     for(int n;scanf("%d",&n) && n;){
10         memset(a,0,sizeof(a));
11         int temp;
12         for(int i=0;i<n && scanf("%d",&temp);i++)
13             a[temp]++;
14         long long int ans=0;
15         for(int i=1;i<=9;i++){
16             long long int sum=1;
17             if(a[i]>0){
18                 for(int j=0;j<=9;j++){
19                     if(j==i)
20                         sum*=permu[a[j]-1];
21                     else
22                         sum*=permu[a[j]];
23                 }
24                 ans+=i*permu[n-1]*1.0/sum;
25             }
26         }
27         long long int final_ans=0;
28         for(int i=0;i<n;i++)
29             final_ans=final_ans*10+ans;
30         printf("%lld\n",final_ans);
31     }
32 }

final_ans=111.....1(n位1)*ans;

ans=∑i*(n-1)!/(a1!*a2!*....(ai-1)!*...*a9!) ;(i=0-->9)

公式就是这样。

posted @ 2013-03-16 00:40  姜楠  阅读(268)  评论(0编辑  收藏  举报