ZOJ 3768 Continuous Login (find the rule and then divide it into two points)

Source: Internet
Author: User

Continuous Login Time Limit: 2 Seconds Memory Limit: 131072 KB Special Judge

Pierre is recently obsessed with an online game. to encourage users to log in, this game will give users a continuous login reward. the mechanism of continuous login reward is as follows: If you have not logged in on a certain day, the reward of that day is 0, otherwise the reward is the previous day's plus 1.

On the other hand, Pierre is very fond of the numberN. He wants to get exactlyNPoints reward with the least possible interruption of continuous login.

Input

There are multiple test cases. The first line of input is an integerTIndicates the number of test cases. For each test case:

There is one integerN(1 <=N<= 123456789 ).

Output

For each test case, output the days of continuous login, separated by a space.

This problem is special judged so any correct answer will be accepted.

Sample Input
4201969
Sample Output
4 43 4 232 3
Hint

20 = (1 + 2 + 3 + 4) + (1 + 2 + 3 + 4)

19 = (1 + 2 + 3) + (1 + 2 + 3 + 4) + (1 + 2)

6 = (1 + 2 + 3)

9 = (1 + 2) + (1 + 2 + 3)

Some problem has a simple, fast and correct solution.

Author: ZHOU, Yuchen
Source: The 14th Zhejiang University Programming Contest




It means that you can find the least number of groups, so that all the sum of the equals numbers of all the groups you are looking for is the given number. For the specific meaning of the question, see the sequence form of the question.
Solution:
Okay, the idea is just getting started. I first typed a table to store the first n items and found that it hit 13000. Then we started yy and wanted to find them directly from the largest to the smallest, but we found that the number of groups could not be minimized.
Then I want to write bfs, but the mark is not marked, and I still cannot find it, so I started to find the rule.
I listed 1 ~ 20. Only three groups can be split.
Then I started typing the code, but it is still not very sure. Let Mr. Hao write the number of groups for the first 100 items next to the enumeration ,,

First, check whether there can be a group. If you can,
No, try two groups again. The first group is enumeration, and the second group is binary.
If not, try three groups. The first two groups are enumerated, and the third group is divided into two groups.
If it doesn't work, it won't work.
In fact, I have been debugging for half a day ,,,,
Algorithm complexity O (n * log2 (n ))
Question address: http://acm.zju.edu.cn/onlinejudge/showProblem.do? ProblemId = 5231
AC code:
#include
 
  #include
  
   #include
   
    #include
    
     #include
     
      #include#define ll long longusing namespace std;int dp[20005];int len;int erfen(int x){    int l=1,r=len;    int mid;    mid=(l+r)>>1;    while(r>l)    {        if(dp[mid]==x) return mid;        if(dp[mid]
      
       >1; } if(dp[l]>x) l--; return l;}int main(){ int i; dp[0]=0; dp[1]=1; for(i=1;i<=20000;i++) { dp[i]=dp[i-1]+i; if(dp[i]>=123456789) break; } len=i; int tes,n; cin>>tes; int a,b,c; while(tes--) { cin>>n; int p=erfen(n); if(dp[p]==n) { printf("%d\n",p); continue; } int flag=0; for(a=p;a>=1;a--) { b=erfen(n-dp[a]); if(dp[b]+dp[a]==n) { flag=1; break; } } if(flag) { printf("%d %d\n",a,b); continue; } flag=0; for(a=p;a>=1;a--) { int s=erfen(n-dp[a]); for(b=s;b>=1;b--) { c=erfen(n-dp[a]-dp[b]); if(dp[a]+dp[b]+dp[c]==n) { flag=1; break; } } if(flag) break; } if(flag) { printf("%d %d %d\n",a,b,c); continue; } }}
      
     
    
   
  
 


Related Article

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.