zoukankan      html  css  js  c++  java
  • UVa11076

    11076 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 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
    31
    2 3
    31
    1 2
    0
    Sample Output
    1332
    444

    题意:
           输入n个数字,这些数字的任意排列总是一个数。你的任务是求出所有这些数的和。

    分析:

           其实,输入的每个数字在每一位出现的概率都是相同的,于是,我们可以先计算出所有输入数字的平均值A。而可重复的排列数就是P=n!/(n1!*n2!*…*nk!)最终的答案就是11…1(n个1)*A*P。

     1 #include <cstdio>
     2 #include <cstring>
     3 #define ll long long
     4 int N;
     5 ll factorial[13];
     6 int num[10];
     7 const ll one[13] = {
     8     0, 1, 11, 111, 1111, 11111, 111111, 1111111, 11111111,
     9     111111111, 1111111111, 11111111111, 111111111111
    10 };
    11 void cal_factorial(){
    12     factorial[0] = 1,factorial[1] = 1;
    13     for(int i = 2 ; i <= 12 ; i++) factorial[i] = i * factorial[i - 1];
    14 }
    15 int main(){
    16     cal_factorial();
    17     int sum = 0;
    18     while(scanf("%d",&N) == 1 && N){
    19         sum = 0;
    20         memset(num,0,sizeof num);
    21         for(int i = 0 ; i < N ; i++){
    22             int tmp; scanf("%d",&tmp);
    23             num[tmp]++,sum += tmp;
    24         }
    25         ll ans = factorial[N - 1] * sum;
    26         for(int i = 0 ; i < 10 ; i++) ans /= factorial[num[i]]; // 平均数需要除以N,将分子上N
    27         printf("%lld
    ",ans * one[N]);
    28     }
    29     return 0;
    30 }
    View Code
  • 相关阅读:
    Lesson 3 Nehe
    Lesson 2 Nehe
    Lesson 1 Nehe
    Lesson 1 Nehe
    JavaScript 字符串与数组转换函数[不用split与join]
    华中科大校长:教授被称为“叫兽”是教育的悲哀
    /etc/profile、~/.bash_profile等几个文件的执行过程
    cygwin下遇到system没有执行的问题
    发短信 汉字编码 utf-8 UCS-2BE
    UTF-8与UNICODE的关系及代码转换
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5850795.html
Copyright © 2011-2022 走看看