1298 - One Theorem, One Year
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:

  1. X is an Almost-K-Prime and
  2. 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

  1. 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.
  2. 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。
 1 #include<math.h>
2 #include<stdlib.h>
3 #include<stdio.h>
4 #include <algorithm>
5 #include<iostream>
6 #include<string.h>
7 #include<vector>
8 #include<map>
9 #include<math.h>
10 using namespace std;
11 typedef long long LL;
12 typedef unsigned long long ll;
13 bool prime[5000]= {0};
14 int su[600];
15 LL dp[600][600];
16 LL ola[600];
17 LL ola1[600];
18 const LL mod=1e9+7;
19 LL quick(int n,int m);
20 int main(void)
21 {
22 int i,j,k,p,q;
23 for(i=2; i<=100; i++)
24 {
25 for(j=i; i*j<=5000; j++)
26 {
27 prime[i*j]=true;
28 }
29 }
30 int ans=1;
31 for(i=2; i<=5000; i++)
32 {
33 if(!prime[i])
34 {
35 su[ans++]=i;
36 }
37 }
38 memset(dp,0,sizeof(dp));
39 dp[0][0]=1;
40 dp[1][1]=2;
41 for(i=1; i<=500; i++)
42 {
43 for(j=i; j<=500; j++)
44 {
45 dp[i][j]=(((dp[i][j-1]+dp[i-1][j-1])%mod)*(su[i]))%mod;
46 }
47 }
48 ola[1]=su[1];
49 ola1[1]=su[1]-1;
50 for(i=2; i<=500; i++)
51 {
52 ola[i]=(su[i]*ola[i-1])%mod;
53 ola1[i]=(su[i]-1)*ola1[i-1]%mod;
54 }
55 for(i=1; i<=500; i++)
56 {
57 ola[i]=quick(ola[i],mod-2);
58 }
59 scanf("%d",&k);
60 int s;
61 for(s=1; s<=k; s++)
62 {
63 scanf("%d %d",&p,&q);
64 LL cnt=dp[q][p];
65 LL cns=ola[q];
66 LL bns=ola1[q];
67 LL sum=((cnt*cns)%mod*bns)%mod;
68 printf("Case %d: ",s);
69 printf("%lld\n",sum);
70 }
71 return 0;
72 }
73 LL quick(int n,int m)
74 {
75 LL ans=1;
76 LL N=n;
77 while(m)
78 {
79 if(m&1)
80 {
81 ans=(ans*N)%mod;
82 }
83 N=(N*N)%mod;
84 m/=2;
85 }
86 return ans;
87 }

1298 - One Theorem, One Year的更多相关文章

  1. LightOj 1298 - One Theorem, One Year(DP + 欧拉)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1298 题意:给你两个数 n, p,表示一个数是由前 k 个素数组成的,共有 n 个素数 ...

  2. Parseval's theorem 帕塞瓦尔定理

    Source: wiki: Parseval's theorem As for signal processing, the power within certain frequency band = ...

  3. 利用Cayley-Hamilton theorem 优化矩阵线性递推

    平时有关线性递推的题,很多都可以利用矩阵乘法来解决. 时间复杂度一般是O(K3logn)因此对矩阵的规模限制比较大. 下面介绍一种利用利用Cayley-Hamilton theorem加速矩阵乘法的方 ...

  4. Kernel Methods (6) The Representer Theorem

    The Representer Theorem, 表示定理. 给定: 非空样本空间: \(\chi\) \(m\)个样本:\(\{(x_1, y_1), \dots, (x_m, y_m)\}, x_ ...

  5. 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Section 4 The Central Limit Theorem

    Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...

  6. LightOJ1298 One Theorem, One Year(DP + 欧拉函数性质)

    题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1298 Description A number is Almost- ...

  7. 生成树的个数——基尔霍夫定理(Matrix-Tree Theorem)

    树有很多种形态,给定结点个数,求生成不同形态二叉树的个数,显然要用到Catalan数列. 那如果给定一个图(Graph)\(G=(V,E)\),要求其最小生成树G',最好的方法莫过于Prim或Krus ...

  8. uva 11178 - Morley's Theorem

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. Dirichlet's Theorem on Arithmetic Progressions 分类: POJ 2015-06-12 21:07 7人阅读 评论(0) 收藏

    Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

随机推荐

  1. 日常Java 2021/10/21

    Java Iterator(迭代器) 如果需要使用iterator类需要从java.util包中引入它 Java Iterator不是一个集合,它是一种访问集合的方法,用于迭代ArrayList和Ha ...

  2. Spark(六)【RDD的血缘依赖】

    RDD依赖关系 1. RDD血缘关系 ​ RDD只支持粗粒度转换,即在大量记录上执行的单个操作.将创建RDD的一系列Lineage(血统)记录下来,以便恢复丢失的分区.RDD的Lineage会记录RD ...

  3. 文件读写以及NMEA码中GPS信息的提取

    首先先了解下什么是NMEA码,这里有很好的解释,就不直接搬运了 http://www.gpsbaby.com/wz/nmea.html 首先要找到包含GPS信息的文本行,即字符串GPGGA所在行 $G ...

  4. iOS UIWebview 长按图片,保存到本地相册

    我们所要解决的问题如题目所示:ios中,长按Webview中的图片,将图片保存到本地相册.解决方案:对load的html网页,执行js注入,通过在webview中执行js代码,来响应点击事件,通过js ...

  5. 【Python】【Basic】【数据类型】运算符与深浅拷贝

    运算符   1.算数运算: 2.比较运算: 3.赋值运算: 4.逻辑运算: 5.成员运算: 三元运算 三元运算(三目运算),是对简单的条件语句的缩写. # 书写格式 result = 值1 if 条件 ...

  6. jenkins+Gitlab安装及初步使用

    安装包下载地址:https://packages.gitlab.com/gitlab/gitlab gitlab-cerpm 包国内下载地址: https://mirrors.tuna.tsinghu ...

  7. Spring Batch(0)——控制Step执行流程

    Conditional Flow in Spring Batch I just announced the new Learn Spring course, focused on the fundam ...

  8. Quartz使用AutoFac依赖注入问题小结

    theme: channing-cyan highlight: a11y-dark 背景 最近在做一个需求,就是在Job中捕捉异常,然后通过邮件或者消息的方式推送给指定人员,在需求实现的过程中遇到的一 ...

  9. (转)Zookeeper原理和作用

    本周末学习zookeeper,原理和安装配置 本文参考: http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/ http:/ ...

  10. vue-cli3 vue2 保留 webpack 支持 vite 成功实践

    大家好! 文本是为了提升开发效率及体验实践诞生的. 项目背景: 脚手架:vue-cli3,具体为 "@vue/cli-service": "^3.4.1" 库: ...