Description flavor recently played in the game of guessing numbers, now she also wants you to play this game. Guess the rules of the numbers game is like this, tell you a positive integer n
(2<=n<=11), and then taste the heart will think of a number of n-digit string (the number of the first number of bits may be 0). Taste will randomly arrange n
Number of digits, which may produce n! n digits. (n!=1x2x3x4x5x......xn,n! reads "n factorial"). Like taste
Three-digit ABC, there will be a total of six three-digit numbers, respectively, ABC,ACB,BAC,BCA,CAB,CBA and then the flavor will sum up the N-bits of this n!
To S (if the first digit of a number starts with a number of 0, then the sum of these 0 is gone.) If there is a number "0123", the sum is added to the value of S is 123), she
will tell you the sum of S minus the value of the number in her mind, please guess the taste of the heart to think of the number. The input contains two lines in total. The first line is an integer n (meaning as described earlier), the second line is a positive integer s, which represents the sum of the number of n! minus the value of that number in the taste heart.
2≤n≤11, 0≤s≤10^18. If the first digit of the number starts with a number of 0, then these 0 must also be output when outputting (see Example 3). Idea: Set the number of each position already know the answer, the derivation formula can find that s only with the sum of digits and the original number of direct search for the original number will time out, so the enumeration 0-9 each appeared several times, using S and the sum of the number of the original number, verify that the number of occurrences of 0-9 matches
1 varB:Array[0..9] ofLongint;2 Pt,i,n:longint;3 S,q,ans,m,max:int64;4 P:boolean;5 6 procedureDFS (K,g,t:longint); //g Shengyugeshu t Shuweihe7 varI,tt:longint;8 Flag:boolean;9 Q:int64;Ten One begin A ifP Thenexit; - - ifk=Ten Then the begin - ifG>0 Thenexit; -Q:=t*m-s; ans:=Q; - ifQ>max Thenexit; + ifq<0 Thenexit; -Fillchar (b,sizeof (b),0); tt:=N; + whileQ>0 Do A begin atInc (B[QMoD Ten]); -Q:=qDiv Ten; Dec (TT); - End; -b[0]:=b[0]+tt; -flag:=true; - fori:=0 to 9 Do in ifA[i]<>b[i] Then beginFlag:=false; BreakEnd; - ifFlag Then beginPt:=tt; P:=true;End; to exit; + End; - the ifg=0 Then * begin $DfsTen,0, T);Panax Notoginseng exit; - End; the + {if k=9 then A begin the a[9]:=g; + DFS (10,0,t+g*9); - a[9]:=0; $ exit; $ end;} - - fori:=0 toG Do the begin -a[k]:=i;WuyiDFS (k +1, g-i,t+k*i); thea[k]:=0; - ifP Thenexit; Wu End; - End; About $ begin - - READLN (n); - fori:=1 toN Dom:=m*Ten+1; A fori:=1 toN-1 Dom:=m*i; + Readln (s); the fori:=1 toN Domax:=max*Ten+9; -p:=false; $Dfs0N0); the fori:=1 toPt DoWrite0); the write (ans); the the End.
"Bzoj" 3189 guessing numbers (math, search)