Backward Digit Sums
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 5664 |
|
Accepted: 3280 |
Description
FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to n (1 <= n <=) in a certain order and then sum adjacent numbers to produce a New list with one fewer number. They repeat this until only a single number was left. For example, one instance of the game (when n=4) might go like this:
3 1 2 4 4 3 6 7 9 16
Behind FJ ' s back, the cows has started playing a more difficult game, in which they try to determine the starting SE Quence from the final total and the number N. Unfortunately, the game is a bit above FJ ' s mental arithmetic capabilit ies.
Write a program to help FJ play the game and keep up with the cows.
Input
Line 1:two space-separated integers:n and the final sum.
Output
Line 1:an Ordering of the integers 1..N so leads to the given sum. If There is multiple solutions, choose the one that's lexicographically least, i.e., that puts smaller numbers first.
Sample Input
4 16
Sample Output
3 1 2 4
Hint
Explanation of the sample:
There is and possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.Puzzle : Let the sequence based on the result 16 be searched 3 1 2 43 1 2 4
4 3 6
7 9
-just see this question there is an idea of violence all the whole line to find the answer, but directly I ruled out, first trouble and may also time out, PS (n<= 10 timeout a ghost AH); and then I found the wrong rule:ad= ((n-1) *n* (n+1)/2-sum)/(n-2);The sum of the first and last two items is determined ... For the time being do not know right and wrong, I have been debugging n long, I gave up, direct violence on a ...
#include <cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>using namespacestd;#defineMem (x, y) memset (x,y,sizeof (x))#defineSI (x) scanf ("%d", &x)#definePI (x) printf ("%d", X)#defineP_ printf ("")Const intmaxn=1010;intANS[MAXN];intVIS[MAXN];intn,m;intFlot;inta[ the];voidDfsintnum) { if(Flot)return; if(num==N) { intsum=0, temp=N; for(intI=0; i<n;i++) a[i]=Ans[i]; while(temp>1){ for(intI=0; i<temp-1; i++) {A[i]+=a[i+1]; } temp--; } Sum=a[0]; if(sum==M) { for(intI=0; i<n;i++){ if(i) p_; printf ("%d", Ans[i]); } puts (""); Flot=1; } return ; } for(intI=0; i<n;i++){ if(vis[i+1])Continue; Ans[num]=i+1; Vis[i+1]=1; DFS (Num+1); Vis[i+1]=0; }}intMain () { while(~SCANF ("%d%d",&n,&M)) { if(n==1) {puts ("1");Continue; } mem (Vis,0); Flot=0; DFS (0); } return 0;}
Backward Digit Sums (violence)