Partition
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 258 Accepted Submission(s): 127
Problem Description
Define f(n) as the number of ways to perform n in format of the sum of some positive integers. For instance, when n=4, we have
4=1+1+1+1
4=1+1+2
4=1+2+1
4=2+1+1
4=1+3
4=2+2
4=3+1
4=4
totally 8 ways. Actually, we will have f(n)=2(n-1) after observations.
Given a pair of integers n and k, your task is to figure out how many times that the integer k occurs in such 2(n-1) ways. In the example above, number 1 occurs for 12 times, while number 4 only occurs once.
4=1+1+1+1
4=1+1+2
4=1+2+1
4=2+1+1
4=1+3
4=2+2
4=3+1
4=4
totally 8 ways. Actually, we will have f(n)=2(n-1) after observations.
Given a pair of integers n and k, your task is to figure out how many times that the integer k occurs in such 2(n-1) ways. In the example above, number 1 occurs for 12 times, while number 4 only occurs once.
Input
The first line contains a single integer T(1≤T≤10000), indicating the number of test cases.
Each test case contains two integers n and k(1≤n,k≤109).
Each test case contains two integers n and k(1≤n,k≤109).
Output
Output the required answer modulo 109+7 for each test case, one per line.
Sample Input
2 4 2 5 5
Sample Output
5 1本题在比赛的时候出答案好快,但一时又想不出缘由,于是找规律1,2,5,12,28,64·······1,2,2*2+2^0,2*5+2^1,2*12+2^2······于是想到递推式:a[0]=1,a[1]=2a[i]=2*a[i-1]+2^(i-2),i>1,i=n-k由于1≤n,k≤109很大,直到递推式还不能解决问题,需要得到通式a[0]=1,a[1]=2a[n-k]=(n-k+3)*2^(n-k-2),n-k>1由于n-k-2可能很大,需要用快速幂计算#include<cstdio> #include<iostream> using namespace std; const __int64 mod=1000000000+7; __int64 optimized_pow_n(__int64 x, __int64 n) //快速幂x^n { __int64 pw = 1; while (n > 0) { if (n&1) pw *= x; pw=pw%mod; x*=x; x=x%mod; n>>= 1; } return pw%mod; } int main() { int t,n,k; cin>>t; while(t--) { scanf("%d%d",&n,&k); if(k>n) printf("0\n"); else if(n==k) printf("1\n"); else if(n-1==k) printf("2\n"); else { printf("%I64d\n",(3+n-k)*optimized_pow_n(2,(__int64)(n-k-2))%mod); } } return 0; }


被折叠的 条评论
为什么被折叠?



