您的位置:首页 > 其它

1298 - One Theorem, One Year

2016-04-11 10:37 218 查看
1298 - One Theorem, One Year

PDF (English) Statistics Forum
Time Limit: 2 second(s)Memory Limit: 32 MB
A number is Almost-K-Prime if it has exactly K prime numbers (not necessarily distinct) in its prime factorization. For example, 12 = 2 * 2 * 3 is an Almost-3-Prime and 32 = 2 * 2 * 2 * 2 * 2 is an Almost-5-Prime number. A number X is called Almost-K-First-P-Prime if it satisfies the following criterions:

X is an Almost-K-Prime and

X has all and only the first P (P ≤ K) primes in its prime factorization.

For example, if K=3 and P=2, the numbers 18 = 2 * 3 * 3 and 12 = 2 * 2 * 3 satisfy the above criterions. And 630 = 2 * 3 * 3 * 5 * 7 is an example of Almost-5-First-4-Pime.

For a given K and P, your task is to calculate the summation of Φ(X) for all integers X such that X is an Almost-K-First-P-Prime.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case starts with a line containing two integers K (1 ≤ K ≤ 500) and P (1 ≤ P ≤ K).

Output

For each case, print the case number and the result modulo 1000000007.

Sample Input

Output for Sample Input

3

3 2

5 4

99 45

Case 1: 10

Case 2: 816

Case 3: 49939643

Note

In mathematics Φ(X) means the number of relatively prime numbers with respect to X which are smaller than X. Two numbers are relatively prime if their GCD (Greatest Common Divisor) is 1. For example, Φ(12) = 4, because the numbers that are relatively prime to 12 are: 1, 5, 7, 11.

For the first case, K = 3 and P = 2 we have only two such numbers which are Almost-3-First-2-Prime, 18=2*3*3 and 12=2*2*3. The result is therefore, Φ(12) + Φ(18) = 10.

Problem Setter: Samir Ahmed
Special Thanks: Jane Alam Jan
思路:DP;状态转移方程dp[i][j]=dp[i-1][j-1]+dp[i][j-1];i表示前i个素数,j表示由前i个素数构成数的素数因子的长度,dp[i][j]存的是符合这个要求的所有数的和;
状态解释:当前末尾的数放的是第i个素数,那么它的前一个数放的是它的前一个素数或者是他本身。
所以dp先打个表,再根据欧拉函数n*((1-1/p1)*(1-1/p2).....);因为dp[i][j]是那些素因子都相同数的和,再将(p1*p2*....)的表打好an,把(p1-1)*(p2-1)*....bn打好
所以K=i,P=j;求的直就为dp[j][i]*(an[j]/bn[j])%mod;然后把bn[i]用费马小定理转换成逆元所以最后就为dp[j][i]*(an[j]*bn[j])%mod。

#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<map>
#include<math.h>
using namespace std;
typedef  long long LL;
typedef unsigned long long ll;
bool prime[5000]= {0};
int su[600];
LL  dp[600][600];
LL ola[600];
LL ola1[600];
const LL mod=1e9+7;
LL quick(int n,int m);
int main(void)
{
int i,j,k,p,q;
for(i=2; i<=100; i++)
{
for(j=i; i*j<=5000; j++)
{
prime[i*j]=true;
}
}
int ans=1;
for(i=2; i<=5000; i++)
{
if(!prime[i])
{
su[ans++]=i;
}
}
memset(dp,0,sizeof(dp));
dp[0][0]=1;
dp[1][1]=2;
for(i=1; i<=500; i++)
{
for(j=i; j<=500; j++)
{
dp[i][j]=(((dp[i][j-1]+dp[i-1][j-1])%mod)*(su[i]))%mod;
}
}
ola[1]=su[1];
ola1[1]=su[1]-1;
for(i=2; i<=500; i++)
{
ola[i]=(su[i]*ola[i-1])%mod;
ola1[i]=(su[i]-1)*ola1[i-1]%mod;
}
for(i=1; i<=500; i++)
{
ola[i]=quick(ola[i],mod-2);
}
scanf("%d",&k);
int s;
for(s=1; s<=k; s++)
{
scanf("%d %d",&p,&q);
LL cnt=dp[q][p];
LL cns=ola[q];
LL bns=ola1[q];
LL sum=((cnt*cns)%mod*bns)%mod;
printf("Case %d: ",s);
printf("%lld\n",sum);
}
return 0;
}
LL quick(int n,int m)
{
LL ans=1;
LL N=n;
while(m)
{
if(m&1)
{
ans=(ans*N)%mod;
}
N=(N*N)%mod;
m/=2;
}
return ans;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: