UVa 11076 Add Again (组合数学)

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 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

思路:平均数思想

由于每个数出现在各个位的次数是一样的,

所以ans=每个位的平均数*排列数*n个1

比如<1 1 2 2>,我们有:

完整代码:

/*0.042s*/

#include<cstdio>
#include<cstring>
const long long one[13] =
{
    0, 1, 11, 111, 1111, 11111, 111111, 1111111, 11111111,
    111111111, 1111111111, 11111111111, 111111111111
};

long long a[10], fac[13];//factorial

int main(void)
{
    int n, num, count;
    long long ans;
    fac[0] = 1;
    for (int i = 1; i <= 12; i++)
        fac[i] = i * fac[i - 1];///计算阶乘
    while (scanf("%d", &n), n)
    {
        memset(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 < 10; ++i)
            ans /= fac[a[i]];
        printf("%lld\n", ans * one[n]);
    }
}

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索input
, long
, 平均数
, sequence
, The
个位
turbo组合again下载、11076 50 9、addoil少女组合梦涵、see you again、begin again,以便于您获取更多的相关知识。

时间: 2024-12-09 03:09:25

UVa 11076 Add Again (组合数学)的相关文章

UVa 10954 Add All:哈弗曼树

10954 - Add All Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1895 Yup!! The problem name reflects your task; just add a set of numbers. But you may fe

UVa 10198 Counting:组合数学&amp;amp;高精度

10198 - Counting Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1139 The Problem Gustavo knows how to count, but he is now learning how write numbers. As

UVa 861 Little Bishops (组合数学)

861 - Little Bishops Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=36&page=show_problem&problem=802 A bishop is a piece used in the game of chess which is played on a board of squar

UVa 10237 Bishops:组合数学

10237 - Bishops Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1178 A bishop is a piece used in the game of chess which is played on a board of square grids. A bishop ca

UVa 10994 Simple Addition :组合数学

10994 - Simple Addition Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1935 计算sum{i从右往左数的第一个非0数字,p<=i<=q}. 见代码.. 01./*0.016s*/ 02. 03.#include<

uva 10954 - Add All

点击打开链接uva 10954 题目意思:     有n个数需要求和,每一次求和都要付出和当前和相等的代价,例如1+2 = 3,那么这一次的代价就是3,问我们怎么选择求和的次序才能使得这个代价的总和最小. 解题思路:     1:贪心                        2:先看一下计算n个数需要几步:                              n =  1    0                              n =  2    1          

UVa 10954:Add All

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1895 原题: Yup!! The problem name reflects your task; just add a set of numbers. But you may feel yourselves condescended, to wri

UVa 10157 Expressions:组合数学&amp;amp;高精度

10157 - Expressions Time limit: 10.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=34&page=show_problem&problem=1098 Let X be the set of correctly built parenthesis expressions. The elements of X a

UVa 10018 Reverse and Add (数学&amp;amp;利克瑞尔数)

10018 - Reverse and AddTime limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=959 The Problem The "reverse and add" method is simple: choose a number,