Vijos 1085 Sunnypig Breaking the Triangle Guan

Source: Internet
Author: User

{This problem 5 correct, five timeouts, do not blindly believe my code, who has a better algorithm or optimization please leave a message, (*^__^*) hehe ...}

Background

The playful Sunnypig asked Charles to create a fantasy world for him, and Charles gladly agreed. However, Charles, who has always been good at spot, would never easily have a fantasy world easily sunnypig, so Charles set up a lot of institutions in the construction process, only after sunnypig cracked these obstacles, can try to the fantasy world most play the ultimate baby- Time-Space Shuttle machine. Although the other treasures in the fantasy world are also very interesting, how can the playful sunnypig let go of the boss's chance? So he began the journey of breaking the barrier.

Describe

The second obstacle stems from an ancient mathematical discovery-the Yang Hui Triangle, but it should be the inverted Yang Hui triangle. If the 1~n is given an arrangement of a, then the A1, A2 Add, A2, A3 add ... An-1, an Add, then get a set of n-1 elements of the series B; Add B1, B2, B2, B3 Add, Bn-2, Bn-1 Add, then get a set of n-2 elements of the series ... In this way, the end result is a number T. The problem that Charles gives Sunnypig is to give N and T, and in the shortest possible time, find the 1~n that can get T and the smallest dictionary order by the above operation. After the Hanoi tower problem training, Sunnypig began to think calmly ...

Format input Format

There are multiple sets of data for each group of data:
A row of two integers n (0<n<=20), and T is the last number to be calculated.

Use end of file to determine the end of the input.

Output format

For each set of test data output a row of n integers, separated by a space, no extra space at the end of the line, indicating the 1~n to meet the requirements of a permutation.

Thinking of solving problems

By test instructions, there are two numbers a, B, t=a+b;

There are three numbers a,b,c when T=a+2b+c

There are four numbers a,b,c,d when t=a+3b+3c+d

......

Not hard to come by, in line with Yang Hui Triangle

Then, you can enumerate the values after each coefficient, then add them, do appropriate pruning, and you can come to an answer

Some people say that is to use the sort inequality to do, but I am a school slag and Konjac Konjac, even see that so-called A^2+B^2+C^2>=AB+AC+BC do not know how to use ...

1  ProgramYanghui;2 varN,i,j,sum,t,flag:longint;3A:Array[0.. -,0.. -] ofLongint;4B:Array[0.. -] ofLongint;5SArray[0.. -] ofLongint;6pd:array[1.. -] ofBoolean;7 procedureyanghui;//find out the Yang Hui triangle8 varI,j:longint;9 beginTena[1,1]:=1; One   fori:=2  to  -  Do A    begin -      forj:=1  toI Do -     begin thea[i,j]:=a[i-1, j]+a[i-1, J-1]; -     End; -    End; - End; + procedureDFS (n,k:longint); - varI:longint; + begin A     ifflag=1  Thenexit;//If you already have an answer, quit at     ifSum>t Thenexit;//if you have exceeded the value of the evaluation, exit -     ifsum+ (s[n]-s[k-1]) *n<t Thenexit;//if it is impossible to reach the desired value, exit -     if(K-1> (n+1)Div 2) and(b[n-k+2]>b[k-1]) Thenexit;//exit if the value at the back is larger than the value of the corresponding position (because this is not the first value in the dictionary order) -     ifk=n+1  Then -     begin -         ifSum=t Then in         begin -              fori:=1  toN DoWrite (B[i],' '); to Writeln; +flag:=1; -         End; the exit; *     End; $      fori:=1  toN Do//simple backtracking for answersPanax Notoginseng     if( notPd[i]) Then -     begin thesum:=sum+i*A[n,k]; +b[k]:=i; Apd[i]:=true; theDFS (n,k+1); +         ifflag=1  Thenexit; -sum:=sum-i*A[n,k]; $pd[i]:=false; $     End; - End; -  the begin - Yanghui;Wuyi      while  notEof Do the     begin - Fillchar (pd,sizeof (PD), false); Wuflag:=0; -sum:=0; About read (n,t); $          fori:=1  toN Dos[i]:=s[i-1]+a[n,i];//prefix and representing the sum of the values on the first Yang Hui triangle of the nth row -DFS (N,1); -     End; - End.

Vijos 1085 Sunnypig Breaking the Triangle Guan

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.