【数论-数位统计】UVa 11076 - Add Again
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 |
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,所以应先乘再除;
【代码】:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef unsigned long long ull;
const int maxn = ;
int x, a[maxn], num[maxn];
ull C[maxn];
const ull basic[] =
{
, , , , , , , ,
, , ,
};
void init()
{
C[] = C[] = ;
for(int i = ; i <= ; i++)
{
C[i] = C[i-]*i;
}
} int main()
{
init();
int n;
while(scanf("%d", &n) && n)
{
memset(num, , sizeof(num));
ull ans = ;
for(int i = ; i < n; i++)
{
scanf("%d", &x);
ans += x;
num[x]++;
}
ull times = C[n];
for(int i = ; i < ; i++)
{
times /= C[num[i]];
}
ans = ans*times*basic[n-]/n;
cout << ans << endl;
}
return ;
}
【数论-数位统计】UVa 11076 - Add Again的更多相关文章
- Uva 11076 Add Again (数论+组合数学)
题意:给你N个数,求把他们的全排列加和为多少 思路:对于这道题,假设数字k1在第一位,然后求出剩下N-1位的排列数num1,我们就可以知道k1在第一位时 排列有多少种为kind1, 同理,假设数字k2 ...
- 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 ...
- UVA 11076 - Add Again(组合)
题目链接 脑子抽了,看错题了,神奇的看成没有0了.主要问题把n个数插入m个相同的数,把m个数给分成1-m堆,然后插到n+1空里. #include <cstdio> #include &l ...
- UVA 11076 Add Again
题目链接:UVA-33478 题意为给定n个数,求这n个数能组成的所有不同的排列组成的数字的和. 思路:发现对于任意一个数字,其在每一位出现的次数是相同的.换言之,所有数字的每一位相加的和是相同的. ...
- Codeforces 55D Beautiful Number (数位统计)
把数位dp写成记忆化搜索的形式,方法很赞,代码量少了很多. 下面为转载内容: a positive integer number is beautiful if and only if it is ...
- [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 ...
- 动态规划——区间DP,计数类DP,数位统计DP
本博客部分内容参考:<算法竞赛进阶指南> 一.区间DP 划重点: 以前所学过的线性DP一般从初始状态开始,沿着阶段的扩张向某个方向递推,直至计算出目标状态. 区间DP也属于线性DP的一种, ...
- 数论 UVA 11076
这道题目的意思简单易懂说的是给你n个数(可能有重复相同的数字),列出他们所有排列的情况,再逐位相加,求出和,例如:给你1,2,3,则排列的情况为<123>, <132>, &l ...
- 紫书 例题 10-22 UVa 1640(数位统计)
这道题的题解有几个亮点 一个是每次只统计一个数字来简化思维 一个是统计当前位数的时候分三个部分来更新答案 具体看代码,有注释 #include<cstdio> #include<cs ...
随机推荐
- bitmap的实现方法
bitmap是一个十分有用的结构.所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素.由于采用了Bit为单位来存储数据,因此在存储空间方面,可以大大节省. 适用 ...
- Quality Center11初始化失败
打开start_a.jsp页面总是闪退,原因如下: 初始化失败因为证书签名过期了.把IE选项里证书检查的三项勾掉就好了(检查发行商的证书是否吊销.检查服务器证书吊销.检查已下载的程序的签名)
- [转]directsound抓取麦克风PCM数据封装类
网上有很多方法从麦克风读取PCM数据,不想一一举例.只是在这里发布一个我自己写的directsound的麦克风PCM数据采集类,通过它,可以很方便的利用directsound技术把麦克风的数据采集到, ...
- hibernate[版本四]知识总结
1.hibernate是orm对象关系映射,是对jdbc的封装 2.hibernate版helloworld 2.1导入jar <dependencies> <dependency& ...
- MFC中常用的内容
在程序中更改静态文本内容. GetDlgItem(IDC_STATIC)->SetWindowText("欢迎"); 不用UpdateData(false); 如果提示con ...
- ASCII,GB2312,GBK,Unicode,Utf-8
1.ASCII:American Stardand Code for Information Interchange,是当时美国制定出来的一套编码系统,使用7位或8位二进制来表示西文字符,0-31以及 ...
- 电脑右键新建文本文档(txt)消失的解决办法
其实只需要一个注册表就可以了 下载地址http://pan.baidu.com/s/1hr7r0fM 拿走不谢! 注册表的内容是这样的,你也可以新建一个文件把后缀名改成.reg然后把下面的内容copy ...
- sre_constants.error: unbalanced parenthesis
Traceback (most recent call last): File "androidmarket82.py", line 108, in <module> ...
- mac下批量删除.svn文件
mac下.svn是隐藏文件,而且即使我们调成可见的,一个一个删也很麻烦.今天正好同事问起来这个命令,于是想可能有些人也需要,于是还是放到博客里吧 命令比较简单,其实就是一条linux命令,打开终端,首 ...
- hdu 5428 The Factor 分解质因数
The Factor Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/contest ...