Add Again
Input: Standard Input

Output: Standard Output

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

3

1 2 3

3

1 1 2

0

  1.  

1332

444

 

 

题意:给你n个数字(0~9,1<=n<=12),问这些数字构成的所有不重复排列的和。

分析:举个例子

含重复数字时能构成的所有不重复排列的个数为:(n!)/((n1!)*(n2!)*...*(nn!)),其中ni指数字i出现的次数。

又因为每个数字在每一位出现的概率时等可能的。

比如1 1 2,所能构成的所有情况为

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1

而1、2、3出现在个、十、百位的次数时一样的,即6/3;

则每个数字在每一位出现的次数为 [(n!)/((n1!)*(n2!)*...*(nn!))]/n;(含重复数字时同样适用)

简化加法,即每个数字在每一位均出现1次时这个数字的和为 x*1...1 (n个1)

则n个数字在每一位出现times次,即为所求答案。ans = (a1+a2+...+an)*(1...1)*[(n!)/((n1!)*(n2!)*...*(nn!))]/n;

切忌:[(n!)/((n1!)*(n2!)*...*(nn!))]/n*(a1+a2+...+an)*(1...1)这样表达时错误的,当n个数字相同时,[(n!)/((n1!)*(n2!)*...*(nn!))] = 1, 1/n会得到0,所以应先乘再除;

【代码】:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. using namespace std;
  6. typedef unsigned long long ull;
  7. const int maxn = ;
  8. int x, a[maxn], num[maxn];
  9. ull C[maxn];
  10. const ull basic[] =
  11. {
  12. , , , , , , , ,
  13. , , ,
  14. };
  15. void init()
  16. {
  17. C[] = C[] = ;
  18. for(int i = ; i <= ; i++)
  19. {
  20. C[i] = C[i-]*i;
  21. }
  22. }
  23.  
  24. int main()
  25. {
  26. init();
  27. int n;
  28. while(scanf("%d", &n) && n)
  29. {
  30. memset(num, , sizeof(num));
  31. ull ans = ;
  32. for(int i = ; i < n; i++)
  33. {
  34. scanf("%d", &x);
  35. ans += x;
  36. num[x]++;
  37. }
  38. ull times = C[n];
  39. for(int i = ; i < ; i++)
  40. {
  41. times /= C[num[i]];
  42. }
  43. ans = ans*times*basic[n-]/n;
  44. cout << ans << endl;
  45. }
  46. return ;
  47. }

【数论-数位统计】UVa 11076 - Add Again的更多相关文章

  1. Uva 11076 Add Again (数论+组合数学)

    题意:给你N个数,求把他们的全排列加和为多少 思路:对于这道题,假设数字k1在第一位,然后求出剩下N-1位的排列数num1,我们就可以知道k1在第一位时 排列有多少种为kind1, 同理,假设数字k2 ...

  2. UVA 11076 Add Again 计算对答案的贡献+组合数学

    A pair of numbers has a unique LCM but a single number can be the LCM of more than one possiblepairs ...

  3. UVA 11076 - Add Again(组合)

    题目链接 脑子抽了,看错题了,神奇的看成没有0了.主要问题把n个数插入m个相同的数,把m个数给分成1-m堆,然后插到n+1空里. #include <cstdio> #include &l ...

  4. UVA 11076 Add Again

    题目链接:UVA-33478 题意为给定n个数,求这n个数能组成的所有不同的排列组成的数字的和. 思路:发现对于任意一个数字,其在每一位出现的次数是相同的.换言之,所有数字的每一位相加的和是相同的. ...

  5. Codeforces 55D Beautiful Number (数位统计)

    把数位dp写成记忆化搜索的形式,方法很赞,代码量少了很多. 下面为转载内容:  a positive integer number is beautiful if and only if it is  ...

  6. [ACM] ural 1057 Amount of degrees (数位统计)

    1057. Amount of Degrees Time limit: 1.0 second Memory limit: 64 MB Create a code to determine the am ...

  7. 动态规划——区间DP,计数类DP,数位统计DP

    本博客部分内容参考:<算法竞赛进阶指南> 一.区间DP 划重点: 以前所学过的线性DP一般从初始状态开始,沿着阶段的扩张向某个方向递推,直至计算出目标状态. 区间DP也属于线性DP的一种, ...

  8. 数论 UVA 11076

    这道题目的意思简单易懂说的是给你n个数(可能有重复相同的数字),列出他们所有排列的情况,再逐位相加,求出和,例如:给你1,2,3,则排列的情况为<123>, <132>, &l ...

  9. 紫书 例题 10-22 UVa 1640(数位统计)

    这道题的题解有几个亮点 一个是每次只统计一个数字来简化思维 一个是统计当前位数的时候分三个部分来更新答案 具体看代码,有注释 #include<cstdio> #include<cs ...

随机推荐

  1. Android实例-Delphi开发蓝牙官方实例解析(XE10+小米2+小米5)

    相关资料:1.http://blog.csdn.net/laorenshen/article/details/411498032.http://www.cnblogs.com/findumars/p/ ...

  2. 使用MySQL正则表达式查询

    MySQL用WHERE子句对正则表达式提供了初步的支持,允许你指定用正则表达式过滤SELECT检索出的数据. REGEXP后所跟的东西作为正则表达式处理. 代码 SELECT prod_name FR ...

  3. codeforces 651A Joysticks

    A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  4. [C语言 - 7] 结构体struct

    A. 基本知识   与数组的对比 数组: 构造类型 只能有多个相同类型的数据构成   结构体: 结构体类型 可以由多个不同类型的数据构成   1. 定义类型 struct Student { int ...

  5. Spring Data JPA教程, 第二部分: CRUD(翻译)

    我的Spring Data Jpa教程的第一部分描述了,如何配置Spring Data JPA,本博文进一步描述怎样使用Spring Data JPA创建一个简单的CRUD应用.该应用要求如下: pe ...

  6. C++中标准容器Vector,元素操作.insert()小结

    insert() 函数有以下三种用法: iterator insert( iterator loc, const TYPE &val );  //在指定位置loc前插入值为val的元素,返回指 ...

  7. PostgreSQL的 initdb 源代码分析之十二

    继续分析 /* Now create all the text config files */ setup_config(); 将其展开: 实质就是,确定各种参数,分别写入 postgresql.co ...

  8. C语言中用宏来作注释

    看了PostgreSQL的代码后,我觉得有不理解的地方,比如: 例如这样的: /* Options that may appear after CATALOG (on the same line) * ...

  9. 使用charles 抓包

    1.保证手机和Mac连接的是同一个无线局域网 2.在Mac上打开Charles Proxy,菜单栏操作:Proxy→Proxy Setting,检查端口,一般保持默认,HTTP Proxy端口为888 ...

  10. 开始学习web前端技术

    不能再蹉跎了,不能再徘徊了,不能再犹豫了,犹豫徘徊等于白来…… 感觉之前浪费了太多的岁月,必须得学习一门实用的技术来充实自己空虚的心情了. 想来想去网页应该是万金油的,大大小小多多少少都得用到.既然如 ...