POJ 3187 Backward Digit Sums
Description
输入n,sum,求1~n的数,如何排列之后,相邻两列相加,直到得出最后的结果等于sum,输出1~n的排列(杨辉三角)
http://poj.org/problem?id=3187
Code
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
long long c[25][25];
void combine_init()
{
memset(c, 0, sizeof(c));
c[0][0] = 1;
for(int i = 1; i <= 15; ++i)
{
c[i][0] = c[i][i] = 1;
c[i][1] = i;
for(int j = 1; j < i; ++j)
if(c[i][j] == 0)
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]);/// % mod
}
}
int a[15];
int main()
{
int n, num, sum;
combine_init();
while(cin >> n >> num)
{
for(int i = 0; i < n; ++i)
a[i] = i + 1;
do
{
sum = 0;
for(int i = 0; i < n; ++i)
sum += c[n - 1][i] * a[i];
if(sum == num)
{
for(int i = 0; i < n; ++i)
cout << a[i] << ' ';
cout << '\n';
break;
}
}
while(next_permutation(a, a + n));
}
return 0;
}