UVA 11076 Add Again(有重复元素的全排列问题)
2015-10-16 15:09
525 查看
UVA 11076 Add Again
题意:
输入N个数字,求这几个数字组成的数之和。如:1,2,3.组成的数有123,132,213,231,312,321,和为1332.
思路:
有重复元素的全排列类问题:有k个元素,第 i 个元素 ai 有 ci 个,设总的全排列个数为 x。
那么:设共有 n 个元素(n=∑ai),得方程 a1!a2!⋯ak!∗x=n!
得到全排列个数 x ,进而得到每一个数字在每一位出现的次数为 x / n。
例如:
1,2,2每一个数字在每一位出现的次数为 3/3 = 1;
1,2,3每一个数字在每一位出现的次数为 6/3 = 2。
回到求和问题,
<1,2,3>组成的数字有:
123
132
213
231
312
321
sum = (6 * 2)(6 * 2)(6 * 2)
换成十进制即 sum = 12*10^2 + 12 * 10 + 12 = 1332。
综上,
答案的每一位 = 所有数字之和乘以出现次数,
故 sum=x/n∗∑aici∗11..11(n个1)
代码:
//#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <ctime> #include <algorithm> #include <functional> #include <string> #include <map> #include <set> #include <vector> #include <queue> #include <stack> #include <bitset> using namespace std; #define IOS std::ios::sync_with_stdio(false);std::cin.tie(0) #define clr( x , y ) memset(x,y,sizeof(x)) #define cls( x ) memset(x,0,sizeof(x)) #define pr( x ) cout << #x << " = " << x << endl #define pri( x ) cout << #x << " = " << x << " " #define test( t ) int t ; cin >> t ; int kase = 1 ; while( t-- ) #define out( kase ) printf( "Case %d: " , kase++ ) #define mp make_pair #define pii pair<int,int> #define pli pair<long long,int> #define pll pair<long long,long long> #define fs first #define se second #define pb push_back typedef long long lint; typedef long long ll; typedef long long LL; typedef unsigned long long ULL; const double eps = 1e-8 ; const int inf = 0x3f3f3f3f ; const long long INF = 0x3f3f3f3f3f3f3f3fLL ; // const int N = ; int num[20] , a[20] ; lint fac[20] ; lint basic[] = { 1 , 11 , 111 , 1111 , 11111 , 111111 , 1111111 , 11111111 , 111111111 ,1111111111 ,11111111111 , 111111111111 } ; int n ; void init() { fac[0] = 1 ; for ( int i = 1 ; i < 20 ; i++ ) fac[i] = fac[i-1] * i ; } void work() { lint ans = 0 ; cls( num ) ; for ( int i = 0 ; i < n ; i++ ) { scanf( "%d" , a + i ) ; ans += a[i] ; num[a[i]] ++ ; } lint x = fac ; for (int i = 0; i < 10 ; i++) { x /= fac[num[i]] ; } ans = ans * x * basic[n-1] / n ; cout << ans << endl ; } int main() { // freopen("my.in","r",stdin); // freopen("my.out","w",stdout); init() ; while( cin >> n && n ) { work() ; } return 0; }
相关文章推荐
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge
- C.445 - Marvelous Mazes
- 1.10494 - If We Were a Child Again
- 2.424 - Integer Inquiry
- 3.10250 - The Other Two Trees
- 5.465 - Overflow
- 6.113 - Power of Cryptography
- 7.10161 - Ant on a Chessboard
- 8.621 - Secret Research
- 9.401 - Palindromes
- A.537 - Artificial Intelligence?