Add Again (repeating element sort)

Source: Internet
Author: User

ADD Again
Input:
standard input

Output: Standard Output

Summation of sequence of integers are always a common problem in computer science. Rather than computing blindly, some intelligent techniques make the task simpler. Here is the summation of a sequence of integers. The sequence is an interesting one and it are the all possible permutations of a given set of digits. For example, if the digits is <1 2 3>, then six possible permutations is <123>, <132>, <213>, &L T;231>, <312>, <321> and the sum of them is 1332.

Input

Each input set would start with a positive integern (1≤n≤12). The next line would contain N decimal digits. Input would be terminated by n=0. There'll is at the 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

3

1 2 3

3

1 1 2

0

                     

1332

444

Problemsetter:md. Kamruzzaman

Special Thanks:shahriar Manzoor

/* Test instructions probably is to give you n number, with this n number can compose M integer, for m integer and this problem knowledge point is a repeating element of the total number of elements: there are k elements, including the first element I have NI, perfection permutation number: The number of =n!/(N1!*n2!*n3!*n4!.... *nk!) Figure out each number of times each position, and then add up on the PS: This problem will be a long long to use unsigned long long, seemingly UVA before the problem often has this kind of egg pain ah */


#include <stdio.h> #include <string.h> #define LL unsigned long longint a[10];//The topic is not very clear, is the number between 0-9; int b[15]; LL jie[15];int n;void init () {    jie[0]=1;    for (LL i=1;i<15;i++)    {        jie[i]=i*jie[i-1];    }} int main () {    freopen ("Add.txt", "R", stdin);    LL ANS,SUM,TP;    Init ();    while (scanf ("%d", &n), N)    {        sum=0;        memset (A,0,sizeof (a));        for (int i=0;i<n;i++)        {            int tp;            scanf ("%d", &TP);            a[tp]++;            SUM+=TP;        }        Ans=jie[n-1]*sum;        for (LL i=0;i<10;i++)        {            if (A[i])            Ans/=jie[a[i]];        }        LL kk=0;        for (int i=1;i<=n;i++)        {            kk=kk*10+ans;        }        printf ("%llu\n", KK);    }    return 0;}

all permutations = (n-1)!/(n1!* N2!*n3!*n4!.. (ni-1)!.. *nk!) Figure out the number of times each position is counted, and then multiply I to add up the contribution value of the I element

#include <stdio.h> #include <string.h> #define LL unsigned long longint a[10];//The topic is not very clear, is the number between 0-9; int b[15];    LL jie[15];int n;void init () {jie[0]=1;    for (LL i=1;i<15;i++) {jie[i]=i*jie[i-1];    }}ll Chsort (ll x) {ll cnt=1;            for (int i=0;i<10;i++) {if (A[i]) {if (i==x) cnt*=jie[a[i]-1];        else Cnt*=jie[a[i]]; }} return cnt;}    int main () {//Freopen ("Add.txt", "R", stdin);    LL ans,sum;    Init ();        while (scanf ("%d", &n), N) {sum=0;        memset (A,0,sizeof (a));            for (int i=0;i<n;i++) {int tp;            scanf ("%d", &AMP;TP); a[tp]++;        Sum+=b[i]; } ans=0;        Ans=jie[n-1]*sum;            for (LL i=0;i<10;i++) {if (A[i]) {ans+=jie[n-1]*i/chsort (i);        }} LL kk=0;    for (int i=1;i<=n;i++) {Kk=kk*10+ans;    } printf ("%llu\n", KK); } return 0;}



Add Again (repeating element sort)

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.