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:

  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 }
时间: 2024-10-16 00:53:35

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

计算理论中的莱斯定理(Rice&#39;s Theorem)——证明与应用

我们给出一个在探讨不可判定性时非常有用的结论--莱斯定理(Rice's Theorem).首先,我们来看前面讨论过的几个不可判定的例子: 这些都是由图灵机识别之语言的性质.而莱斯定理告诉我们,任何由图灵机识别之语言的非平凡性质(nontrivial property)都是不可判定的. 最后通过几个例子来探讨一下莱斯定理的应用.来看看下面这个语言能否使用莱斯定理来确定其可判定性. {<M> | M是一个TM,且L(M)可由一些拥有偶数个状态的图灵机识别} 首先来确定这是否是一个语言属性,显然是的

UVA 11178 Morley&#39;s Theorem 计算几何

计算几何: 最基本的计算几何,差积  旋转 Morley's Theorem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem D Morley's Theorem Input: Standard Input Output: Standard Output Morley's theorem states that that the line

1298 凸包周长

1298 凸包周长 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 给出平面上n个点,求出这n个点形成的凸包的周长. 凸包的定义:能覆盖住这个n个点的最小凸多边形. 输入描述 Input Description 第一行一个整数n,接下来n行,每行两个整数x和y,表示一个点的坐标. 数据范围 1 <= n <= 100000 -10000<=x,y<=10000 输出描述 Output De

[转]A plain english introduction to cap theorem

Kaushik Sathupadi Programmer. Creator. Co-Founder. Dad. See all my projects and blogs → A plain english introduction to CAP Theorem You’ll often hear about the CAP theorem which specifies some kind of an upper limit when designing distributed systems

poj 3006 Dirichlet&#39;s Theorem on Arithmetic Progressions

Description If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., a, a + d, a + 2d, a + 3d, a + 4d, ..., contains infinitely many prime numbers. This fact is known as Dirichlet's Theore

(素数求解)I - Dirichlet&#39;s Theorem on Arithmetic Progressions(1.5.5)

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., a, a + d, a + 2d, a + 3d, a 

[51nod]1298 圆与三角形

1298 圆与三角形 题目来源: HackerRank 给出圆的圆心和半径,以及三角形的三个顶点,问圆同三角形是否相交.相交输出"Yes",否则输出"No".(三角形的面积大于0). Input 第1行:一个数T,表示输入的测试数量(1 <= T <= 10000),之后每4行用来描述一组测试数据. 4-1:三个数,前两个数为圆心的坐标xc, yc,第3个数为圆的半径R.(-3000 <= xc, yc <= 3000, 1 <= R 

master theorem

The master theorem concerns recurrence relations of the form: {\displaystyle T(n)=a\;T\!\left({\frac {n}{b}}\right)+f(n)} where {\displaystyle a\in \mathbb {N} {\mbox{, }}1<b\in \mathbb {R} } In the application to the analysis of a recursive algorith

HDU 3689 Infinite monkey theorem [KMP DP]

Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1702 Accepted Submission(s): 882 Problem Description Could you imaging a monkey writing computer programs? Surely monkeys are