1298 - One Theorem, One Year
2016-04-11 10:37
218 查看
1298 - One Theorem, One Year
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.
Each case starts with a line containing two integers K (1 ≤ K ≤ 500) and P (1 ≤ P ≤ K).
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。
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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; }
相关文章推荐
- Local declaration of 'XXX' hides instance variable
- Discuz!在线中文分词服务
- Java单链表反转 详细过程
- springMVC两种实现多文件上传及效率比较
- TQ2440开发板交叉编译工具安装之后的环境变量设置
- 添加网络打印机的步骤(xp和win2008+win7)
- 初学软件工程,对《现代软件工程―构建之法》这本书的疑问
- 【沟通之道】头脑风暴-女人的心思你别猜
- django-cms细节记录|篇一
- PHP PDO ACCESS 留言板-数据库设计
- Android .mk文件语法解析
- 超简单理解Handler 机制
- mysql初始数据库用途
- 定制ios7中的导航栏和状态栏
- 明尼苏达推荐系统导论(第一课 欢迎来到RS)
- Secure CRT修改文件夹的颜色
- php字符串函数详解
- opencv 人脸识别 (二)训练和识别
- 各位爸爸妈妈,你们懂得
- RecyclerView 的使用(1)