2013 多校训练 hdu 4602 Partition 解题报告
Partition
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1458 Accepted Submission(s): 607
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≤10 9).
Each test case contains two integers n and k(1≤n,k≤10 9).
Output
Output the required answer modulo 10
9+7 for each test case, one per line.
Sample Input
2 4 2 5 5
Sample Output
5 1
Source
Recommend
liuyiding
已知f(n)=2^(n-1) 给出k看f(n)里面 k能出现多少次!
没啥好办法,一点点写出来找规律啊!
cnt[i]表示i出现的次数!
f(1)=1
cnt[1]=1;
f(2)=2
cnt[2]=1,cnt[1]=2
f(3)=4
cnt[3]=5,cnt[2]=2],cnt[1]=1;
f(4)=8
cnt[4]=12,cnt[3]=5,cnt[2]=2,cnt[1]=1;
此意次类推,注意cnt的变化分别是1,2,5,12,28,64,144……
怎么推出来的呢? 有神马规律呢?! 不列出几个数来观察是很难找到规律的,TND不自己拿笔写写我自己也推不出这几个数字!
然后对这几个数字我疯狂的研究……
终于:
发现,cnt[n]=2*cnt[n-1]+2^(n-3);
推到这一步我当时就相当兴奋呐,想到先init(),然后就……
可是数据太大了,数组开不了,根本没法存啊!
怎么办呢?对于每个数,每次重新递推一边就好了,但是从头递推一遍貌似这个复杂度也蛮高的,怎么办呢,继续化简…… 最后得出一个cnt[n]和cnt[3]的关系来…… 直接利用一次快速幂就OK了!!
cnt[n]=(n+2)*2^(n-3)
注意n>3
这里的n和题中所给的n,k什么关系呢?! 在f(n)中n是倒数第1个,n-1是倒数第2个,k是倒数第(n-k+1)个,即cnt[n']=cnt[n-k+1]
当然,这个题还有个坑,就是n有可能比k小…… 这时输出0!
贴代码:
#include<cstdio>
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
const int MOD=1000000007;
int n,k,t;
long long pow(long long x,long long p)
{
///返回x的p次方
long long ans=1;
while(p>1)
{
if(p%2)ans=(ans*x)%MOD;///处理奇数
x=(x*x)%MOD;
p/=2;
}
ans=(ans*x)%MOD;
return ans;
}
int a[5];
int main()
{
// int b;
// while(cin>>b)
// {
// cout<<pow(2,b)<<endl;
// }
cin>>t;
while(t--)
{
a[1]=1;
a[2]=2;
a[3]=5;
scanf("%d%d",&n,&k);
int ord=n-k+1;
if(ord<=0)printf("%d\n",0);///注意题目没有说n比k小
else if(ord<=3)printf("%d\n",a[ord]);
else
{
long long ans=(ord+2)%MOD;
ans=ans*pow(2,ord-3)%MOD;
printf("%d\n",ans);
}
}
return 0;
}
2013 多校训练 hdu 4602 Partition 解题报告