UVa 11076 Add Again (Combinatorial mathematics)

Source: Internet
Author: User

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/

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.