Blue Bridge series of waves (unresolved: Run timeout)

Source: Internet
Author: User

Although there is no solution, but the general idea is still there, to ask for AC code.

Problem Descriptionobserve this sequence:
1 3 0 2-1 1-2 ...

The last item in this series is always 2 or 3 less than the previous one.

The building is curious about the series, and he wants to know how many of the integers of length n and S and the latter always add a to the previous item or decrease B. Input Formatthe first line of input contains four integers, n s a B, meaning as stated earlier. output FormatThe output line contains an integer that represents the number of scenarios that satisfy the condition. Since this number is large, the remainder of the output scheme number is divided by 100000007. Sample Input4 2 3Sample Output2Sample Descriptionthe two series are 2 4 1 3 and 7 4 1-2 respectively. data size and conventionsfor 10% of data,1<=n<=5,0<=s<=5,1<=a,b<=5;
For 30% of data,1<=n<=30,0<=s<=30,1<=a,b<=30;
For 50% of data,1<=n<=50,0<=s<=50,1<=a,b<=50;
For 70% of data, 1<=n<=100,0<=s<=500,1<=a, b<=50;
For 100% of data, 1<=n<=1000,-1,000,000,000<=s<=1,000,000,000,1<=a, b<=1,000,000. idea: Recursive enumeration of each number of series, judging and whether or not equal to the input. is the word Count +1. first solve the problem of the range of the first number. The approximate interval is s-n*a;s+n*b;because the run timed out. Further optimization was done. set the first number to X, thetwo enumeration intervals for a sequence:x,x+a,x+2a,x+3a........x+ (n-1) A;x,x-b,x-2b,x-3b.........x-(n-1) b;because and for S,Solution to:x = (s-a*n* (n-1)/2)/n;x = (s + b*n* (n-1)/2)/n;recursive enumeration for the For loop. Timeout Code:
Import Java.util.scanner;class main{static int ans = 0,sum = 0,n,s,a,b;public static void Main (string[] arge) {Scanner cin  = new Scanner (; n = Cin.nextint (); s = Cin.nextint (); a = Cin.nextint (); b = Cin.nextint (); for (int i = (s-a*n * (n-1)/2)/n;i<= (S + b*n* (n-1)/2)/n;i++) {DFS (i,n-1);} Ans%= 100000007;sop1 (ans); public static void Dfs (int i,int n) {sum + = i;if (n = = 0) {if (sum = = s) ans++;sum-= I;return;} DFS (i + a,n-1);d FS (i-b,n-1), sum-= i;} public static void Sop1 (Object obj) {System.out.println (obj);}}

commit, or timeout. Really do not know how to optimize the. The AC code is obtained.

Blue Bridge series of waves (unresolved: Run timeout)

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: 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.