CodeChef November Challenge 2013.Missing some chairs

11051 ワード

Description
A new school in Byteland is now in the process of renewing some classrooms with new, stronger and better chairs, so that the students can stay still and pay attention to class :)
However, due to budget and logistic reasons, it's only possible to carry a chair at a time to the classroom, which means that for a long time, many students will be up, waiting for their chair to arrive.
The teacher, however, as she is very clever, decided to challenge her students with a problem: "Imagine that there are N students in the classroom and that there are only K chairs. In how many ways, can I choose K elements from the class to sit down, if I see them as being distinct?"
Lira replied immediately with the right answer, so, the teacher decided to make the game a little funnier: "Okay Lira, as you are so fast, now I want you to tell me exactly the same thing, but, with the addition that the value of K is changing, this is, I want you to tell me the sum of the number of ways I can sit down K of you, if the value of K goes from 1 (meaning that there are no chairs in the classroom but one) to N (meaning that all of your chairs arrived). Can you be as fast now? As the answer might get large I want you to tell me the result modulo 1000000007. (109 + 7)"
As you might have noticed, it's time for you to help Lira solving this variant of the problem. :D
Input
The first line of the input file contains an integer T, denoting the number of test cases on the input file.
Afterwards, T lines follow, each containing an integer N, the number of students that the teacher will try to sit down as the number of chairs goes from 1 to N.
Output
For each test case, you should output an integer, denoting the sum of the number of ways the teacher can make N students sit down on K chairs, as K goes from 1 to N, modulo 109 + 7.
Constraints
  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 100000000

  • Example
    Input:
    
    2
    
    1
    
    2
    
    
    
    Output:
    
    1
    
    3

    Analyse
    This question is very simple to find the method:
    F(n) = Σ[C(1, n) + C(2, n) + ... + C(n, n)]
    But what is difficult is how to calculate the fomula in 2seconds.
    According to the Binomial theorem:
    (a + b)n = Σ{[C(0, n) * a0 * bn] + [C(1, n) * a1 * bn - 1] + ... + [C(n, n) * an * b0]}
    Thus if a = 1, b = 1 then we get:
    (1 + 1)n = Σ{[C(0, n) * 10 * 1n] + [C(1, n) * 11 * 1n - 1] + ... + [C(n, n) * 1n * 10]}
                  = Σ[C(0, n) + C(1, n) + ... + C(n, n)]
                  = 2n
    So F(n) = 2n -  C(0, n) = 2n - 1
    Code
     1 /* References: www.codechef.com/NOV13/problems/MCHAIRS/
    
     2  * C(0,n)+C(1,n)+C(2,n)+...+C(n.n)=2^n
    
     3  * (1+x)^n=C(0.n)+C(1,n)x+C(2,n)x^2+C(3,n)x^3+…+C(n,n)x^n
    
     4  */
    
     5 #include <cstdio>
    
     6 #include <cmath>
    
     7 #include <iostream>
    
     8 #include <cstring>
    
     9 
    
    10 using namespace std;
    
    11 
    
    12 typedef long long ll;        
    
    13 
    
    14 const ll x = 1000000007;
    
    15 
    
    16 ll r[33];
    
    17 ll b[33];
    
    18 
    
    19 inline ll ipow(ll base, int p)
    
    20 {
    
    21         ll res = 1;
    
    22         while(p > 1)
    
    23         {
    
    24                 int i = 32;
    
    25                 for(i = 32; i > 0 && p > 1; i--)
    
    26                 {
    
    27                         if(b[i] <= p)
    
    28                         {
    
    29                                 p -= b[i];
    
    30                                 res = (res * r[i]) % x;
    
    31                                 i += 1;
    
    32                         }
    
    33                 }
    
    34         }
    
    35         if(p == 1) { res = (res * 2) % x; }
    
    36 
    
    37         return res;
    
    38 }
    
    39 
    
    40 int main()
    
    41 {
    
    42         int t = 0;
    
    43         int i = 0;
    
    44         ll n = 0;
    
    45         ll res = 1;
    
    46 
    
    47         memset(r, 1, sizeof(r));
    
    48         r[1] = 4;
    
    49         b[1] = 2;
    
    50         for(i = 2; i < 33; i++)
    
    51         {
    
    52                 r[i] = (r[i - 1] * r[i - 1]) % x;
    
    53                 b[i] = b[i - 1] * 2;
    
    54         }
    
    55 
    
    56         scanf("%d", &t);
    
    57         for(i = 0; i < t; i++)
    
    58         {
    
    59                 scanf("%lld", &n);
    
    60                 
    
    61                 res = (ipow(2, n) + x - 1) % x;
    
    62 
    
    63                 printf("%lld
    ", res); 64 } 65 66 return 0; 67 }