Hdu 5439 Aggregated Counting)

Source: Internet
Author: User

Hdu 5439 Aggregated Counting)
Aggregated CountingTime Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission (s): 665 Accepted Submission (s): 302

Problem Description Aggregated Counting Meetup (ACM) is a regular event hosted by Intercontinental Crazily Passionate Counters (ICPC ). the ICPC people recently proposed an interesting sequence at ACM2016 and encountered a problem needed to be solved.

The sequence is generated by the following scheme.
1. First, write down 1, 2 on a paper.
2. The 2nd number is 2, write down 2 2's (including the one originally on the paper). The paper thus has 1, 2, 2 written on it.
3. The 3rd number is 2, write down 2 3's. 1, 2, 2, 3, 3 is now shown on the paper.
4. The 4th number is 3, write down 3 4's. 1, 2, 2, 3, 3, 4, 4 is now shown on the paper.
5. the procedure continues indefinitely as you can imagine. 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6 ,....

The ICPC is widely renowned for its counting ability. At ACM2016, they came up with all sorts of intriguing problems in regard to this sequence, and here is one: Given a positive number N , First of all, find out the position of the last N That appeared in the sequence. for instance, the position of the last 3 is 5, the position of the last 4 is 8. after obtaining the position, do the same again: Find out the position of the last (position number ). for instance, the position of the last 3 is 5, and the position of the last 5 is 11. ICPC wowould like you to help them tackle such problems efficiently.


Input The first line contains a positive integer T, T ≤ 2000 , Indicating the number of queries to follow. Each of the following T Lines contain a positive number N (n = 109) Representing a query.
Output the last position of each query N . In case the answer is greater 1000000006 , Please modulo the answer 1000000007 .
Sample Input

3 3 10 100000 

Sample Output
11 217 507231491 

Source 2015 ACM/ICPC Asia Regional Changchun Online
Recommend hujie | We have carefully selected several similar problems for you: 56645663566256615660 question: generate a sequence according to the following rules to make f (n) returns the value of f (n) for the last position of n in the sequence.

Solution:

To column a in the original order, the following rules can be introduced based on the definition and sequence generation rules:

  • F (n) is equal to the first n of a and
  • F (n) is the last position where n appears in.

    F (n) indicates the first m of a, and m indicates the position where n finally appears in. Therefore, the formula of f (n) can be written as follows:

    F (n) = 1 + (2 + 3) * 2 + (4 + 5) * 3 + (6 + 7 + 8) * 4 +... + (... + n) * t

    This is probably the above content. Here I use several arrays to access the above process and sort out the formula.

    For details, see the code.

     

    # Include
       
        
    # Include
        
         
    # Include
         
          
    # Include using namespace std; # define N 450010 const int Mod = (1e9 + 7); long a [N]; // It indicates the ending number of I, int B [N]; // It indicates that I appears several times long s [N]; // indicates the first I and int main () {a [1] = 1; a [2] = 3; a [3] = 5; B [1] = 1; B [2] = 2; B [3] = 2; s [1] = 1; s [2] = 11; s [3] = 38; int k = 4, pre = 4, kk = 3; // k is used for loop, pre indicates the subscript of the current column, kk represents the number corresponding to the subscript while (a [k-1] <= 1e9) {for (k = pre; k
          
    a[mid-1])            {                break;            }            else            {                if (n>a[mid])                {                    l=mid+1;                }                else                    r=mid;            }        }        printf ("%lld\n",(s[mid-1]+mid*((a[mid-1]+1+n)*(n-a[mid-1])/2))%Mod);    }    return 0;}

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.