| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 4788 | Accepted: 2759 |
Description
3 1 2 4
4 3 6
7 9
16
Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ's mental arithmetic capabilities. Write a program to help FJ play the game and keep up with the cows.
Input
Output
Sample Input
4 16
Sample Output
3 1 2 4
Hint
There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[12],b[12];
int main()
{
int i,n,k;
scanf("%d%d",&n,&k);
for(i=0;i<n;i++) //排列为1-n递增的情况
{
a[i]=i+1;
b[i]=a[i];
}
int m=n;
while(m>1)
{
for(i=0;i<m-1;i++)
b[i]+=b[i+1];
m--;
}
if(b[0]==k)
{
for(i=0;i<n;i++)
{
if(i)
printf(" ");
printf("%d", a[i]);
}
printf("\n");
return 0;
}
while(next_permutation(a,a+n))
{
for(i=0;i<n;i++) //三角形累加过程
b[i]=a[i];
m=n;
while(m>1)
{
for(i=0;i<m-1;i++)
b[i]+=b[i+1];
m--;
}
if(b[0]==k)break;
}
for(i=0;i<n;i++)
{
if(i)
printf(" ");
printf("%d", a[i]);
}
printf("\n");
return 0;
}
1万+



被折叠的 条评论
为什么被折叠?



