Add Again (sorting of repeated elements) UVA11076, adduva11076

Source: Internet
Author: User

Add Again (sorting of repeated elements) UVA11076, adduva11076
Add Again
 

 

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 integerN (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 shocould 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

 

Train of Thought: for the nth digit, there are k Elements in total, where the nth element has ni, and the number of all elements is required: the number of all elements = (n-1 )! /(N1! * N2! * N3! * N4 !.. (Ni-1 )!.. * Nk !) Calculate the number of times each number appears in each position, and multiply by I to add up to the contribution value of the I element. Reprinted. Please indicate the source: Search for the child

 

 

1 # include <stdio. h> 2 # include <string. h> 3 # define LL unsigned long 4 5 int a [10]; // The question is not clear, it is the number between 0-9; 6 int B [15]; 7 LL jie [15]; 8 int N; 9 void init () 10 {11 jie [0] = 1; 12 for (LL I = 1; I <15; I ++) 13 {14 jie [I] = I * jie [I-1]; 15} 16} 17 LL chsort (LL x) 18 {19 LL cnt = 1; 20 for (int I = 0; I <10; I ++) 21 {22 if (a [I]) 23 {24 if (I = x) 25 cnt * = jie [a [I]-1]; 26 else27 cnt * = jie [a [I]; 28} 29} 30 return cnt; 31} 32 int main () 33 {34 // freopen ("Add.txt", "r", stdin); 35 36 LL ans, sum; 37 init (); 38 while (scanf ("% d", & N), N) 39 {40 sum = 0; 41 memset (a, 0, sizeof ()); 42 for (int I = 0; I <N; I ++) 43 {44 int tp; 45 scanf ("% d", & tp ); 46 a [tp] + +; 47 // sum + = B [I]; 48} 49 ans = 0; 50 // ans = jie [N-1] * sum; 51 for (LL I = 0; I <10; I ++) 52 {53 if (a [I]) 54 {55 ans + = jie [N-1] * I/chsort (I); 56} 57} 58 LL kk = 0; 59 for (int I = 1; I <= N; I ++) 60 {61 kk = kk * 10 + ans; 62} 63 printf ("% llu \ n", kk ); 64} 65 return 0; 66}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.