CodeChef November Challenge 2013.Missing some chairs

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\n", res);
64         }
65         
66         return 0;
67 }

转载于:https://www.cnblogs.com/flyingpeguin/p/3416697.html

The Sleeping Teaching Assistant A university computer science department has a teaching assistant (TA) who helps undergraduate students with their programming assignments during regular office hours. The TA&rsquo;s office is rather small and has room for only one desk with a chair and computer. There are three chairs in the hallway outside the office where students can sit and wait if the TA is currently helping another student. When there are no students who need help during office hours, the TA sits at the desk and takes a nap. If a student arrives during office hours and finds the TA sleeping, the student must awaken the TA to ask for help. If a student arrives and finds the TA currently helping another student, the student sits on one of the chairs in the hallway and waits. If no chairs are available, the student will come back at a later time. Using POSIX threads, mutex locks, and/or semaphores, implement a solution that coordinates the activities of the TA and the students. Details for this assignment are provided below. Using Pthreads, begin by creating N students. Each will run as a separate thread. The TA will run as a separate thread as well. Student threads will alternate between programming for a period of time and seeking help from the TA. If the TA is available, they will obtain help. Otherwise, they will either sit in a chair in the hallway or, if no chairs are available, will resume programming and will seek help at a later time. If a student arrives and notices that the TA is sleeping, the student must notify the TA using a semaphore. When the TA finishes helping a student, the TA must check to see if there are students waiting for help in the hallway. If so, the TA must help each of these students in turn. If no students are present, the TA may return to napping. Perhaps the best option for simulating students programming—as well as the TA providing help to a student—is to have the appropriate threads sleep for a random period of time using the sleep() API:
06-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值