UVa 11076 - Add Again (排列之和 组合数学)
2014-08-27 14:10
387 查看
Problem C
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.
Problemsetter: Md. Kamruzzaman
Special Thanks: Shahriar Manzoor
题意:
输入n个数字,这些数字的任何一种排列都是一个整数,你的任务是求出所有这些整数之和。
对于m个数,要求他的和,可以像这样求
对于每一位i,求m个数第i为上的和然后乘第i位相应的权值10^i(i从0开始)
对于第i位j出现的方法数则是除去一个j后的其他数的排列方法数
这个题unsigned long long 也会超
但是 unsigned long long / long long 也能过
数据没有这么大?
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 integerN (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 |
Special Thanks: Shahriar Manzoor
题意:
输入n个数字,这些数字的任何一种排列都是一个整数,你的任务是求出所有这些整数之和。
对于m个数,要求他的和,可以像这样求
对于每一位i,求m个数第i为上的和然后乘第i位相应的权值10^i(i从0开始)
对于第i位j出现的方法数则是除去一个j后的其他数的排列方法数
这个题unsigned long long 也会超
但是 unsigned long long / long long 也能过
数据没有这么大?
#include <cstdio> #include <iostream> #include <vector> #include <algorithm> #include <cstring> #include <string> #include <map> #include <cmath> #include <queue> #include <set> using namespace std; //#define WIN #ifdef WIN typedef __int64 LL; #define iform "%I64d" #define oform "%I64d\n" #define oform1 "%I64d" #else typedef long long LL; #define iform "%lld" #define oform "%lld\n" #define oform1 "%lld" #endif #define S64I(a) scanf(iform, &(a)) #define P64I(a) printf(oform, (a)) #define P64I1(a) printf(oform1, (a)) #define REP(i, n) for(int (i)=0; (i)<n; (i)++) #define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++) #define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++) const int INF = 0x3f3f3f3f; const double eps = 10e-9; const double PI = (4.0*atan(1.0)); int vis[10]; unsigned long long C[20][20]; void init(int n) { memset(C, 0, sizeof(C)); C[0][0] = 1; for(int i=1; i<=n; i++) { C[i][0] = C[i][i] = 1; for(int j=1; j<i; j++) { C[i][j] = C[i-1][j-1] + C[i-1][j]; } } } int main() { int n; init(15); while(scanf("%d", &n) != EOF && n) { int sum = 0; memset(vis, 0, sizeof(vis)); for(int i=0; i<n; i++) { int t; scanf("%d", &t); vis[t]++; sum += t; } unsigned long long p10 = 1; unsigned long long ans = 0; for(int i=0; i<n; i++) { for(int j=1; j<10; j++) if(vis[j]) { vis[j]--; int lev = n-1; LL tt = 1; for(int k=1; k<10; k++) if(vis[k]) { tt *= C[lev][vis[k]]; lev -= vis[k]; } ans += p10 * j * tt; vis[j]++; } p10 *= 10; } cout<<ans<<endl; } return 0; }
相关文章推荐
- Uva 11076 Add Again 解题报告(组合数学)
- UVALive 6909 Kevin's Problem 数学排列组合
- UVA 12712 Pattern Locker(简单排列组合数学题)
- UVA 11076 - Add Again(组合)
- UVa 11076 (有重元素的排列) Add Again
- 排列组合·UVA 11076·排列之和
- UVA - 11076 Add Again (重复元素的排列)
- UVa 10290 {Sum+=i++} to Reach N (数论-整数和素数,组合数学-排列组合)
- UVa 11076 Add Again(数学)
- (组合数学3.3.2.2)UVA 10497 - Sweet Child Makes Trouble(特殊的排列问题——错排方案数)
- UVA 11174-组合数学+组合数取模+dfs
- 数学 ( 排列组合 )——HDU 5194
- UVALive-8079 Making a Team 排列组合公式化简
- UVALive 7040 Color (容斥原理 + 组合数学递推公式 + 求逆元 + 基础数论)
- X-factor Chains (poj 3421 数学排列组合)
- 离散数学基础——(1)排列组合
- 组合数学之排列组合(Permutations and Combinations)(四种情况)
- Add Again(UVA 11076)
- HDU 4043 FXTZ II (组合数学-排列组合)
- UVa 11401 数三角形(组合数学)