LightOJ1298 One Theorem, One Year(DP + 欧拉函数性质)
题目
Source
http://www.lightoj.com/volume_showproblem.php?problem=1298
Description
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
3
3 2
5 4
99 45
Sample Output
Case 1: 10
Case 2: 816
Case 3: 49939643
分析
题目大概说,定义,一个数为Almost-K-First-P-Prime当且仅当这个数由K个质因子组成,且这K个质因子包含且仅包含于前P个质数。给定k和p,求Σphi(AkFpP)。
首先要知道欧拉函数这几个性质:
- φ(p)=p-1(p是质数)
- φ(p*a)=(p-1)*φ(a)(p是质数且p不能整除a)
- φ(p*a)=p*φ(a)(p是质数且p|a)
然后,可以考虑用DP来解,利用上面的性质去转移。
一开始我这么想的,首先由于k个质因子中那p个是一定要有的,先把它们固定下来,即∏prime[1...p],其phi值为∏(prime[1...p]-1)。然后还剩下k-p个质因子要确定,有p个质因子可以选择,这其实就是完全背包问题了:p种物品体积都为1选了之后价值*prime[i],背包容量k-p,问所有选择方案的价值和。
不过这样会TLE的,数据有10000组,通过可以枚举p把所有情况预处理出来,O(P2K)的时间复杂度,可能会超时,不过已经爆内存了。
事实上还有更直接的预处理方式:
- dp[i][j]表示Σphi(Almost-i-First-j-Prime)
考虑这么转移:
- 如果prime[j]只出现一次,那么就是从dp[i-1][j-1](这个状态prime[j]不会出现,出现的是前j-1个质数)通过第i个质因子选择prime[j]转移:dp[i][j]+=dp[i-1][j-1]*(prime[j]-1)
- 如果prime[j]出现多于一次,那么就是从dp[i-1][j](这个状态prime[j]至少出现一次,再加上一次就大于1次了)转移了:dp[i][j]+=dp[i-1][j]*prime[j]
时间复杂度就是O(PK)
其实这种转移的分析方式觉得挺强的,分成等于1、大于等于1,这两个能分别求出来且互补的子问题。和排队购票那个经典题的转移一样。
下面是那两个算法的代码实现。
代码
O(P2K)
#include<cstdio>
#include<cstring>
using namespace std; int prime[555]; bool is_prime(int n){
for(long long i=2; i*i<=n; ++i){
if(n%i==0) return 0;
}
return 1;
} long long d[501][501][500]; void init(){
int n=0;
for(int i=2; n!=500; ++i){
if(is_prime(i)){
prime[++n]=i;
}
}
for(int p=1; p<=500; ++p){
d[p][0][0]=1;
for(int i=1; i<=p; ++i){
d[p][0][0]*=prime[i]-1;
d[p][0][0]%=1000000007;
}
for(int i=1; i<=p; ++i){
for(int j=0; j<500; ++j){
if(j) d[p][i][j]=d[p][i-1][j]+d[p][i][j-1]*prime[i];
else d[p][i][j]=d[p][i-1][j];
d[p][i][j]%=1000000007;
}
}
}
} int main(){
init();
int t,k,p;
scanf("%d",&t);
for(int cse=1; cse<=t; ++cse){
scanf("%d%d",&k,&p);
printf("Case %d: %lld\n",cse,d[p][p][k-p]);
}
return 0;
}
O(PK)
#include<cstdio>
#include<cstring>
using namespace std; int prime[555]; bool is_prime(int n){
for(long long i=2; i*i<=n; ++i){
if(n%i==0) return 0;
}
return 1;
} long long d[555][555]; void init(){
int n=0;
for(int i=2; n!=500; ++i){
if(is_prime(i)){
prime[++n]=i;
}
}
d[0][0]=1;
for(int i=1; i<=500; ++i){
for(int j=1; j<=i; ++j){
d[i][j]+=d[i-1][j-1]*(prime[j]-1);
if(i-1>=j) d[i][j]+=d[i-1][j]*prime[j];
d[i][j]%=1000000007;
}
}
} int main(){
init();
int t,k,p;
scanf("%d",&t);
for(int cse=1; cse<=t; ++cse){
scanf("%d%d",&k,&p);
printf("Case %d: %lld\n",cse,d[k][p]);
}
return 0;
}
LightOJ1298 One Theorem, One Year(DP + 欧拉函数性质)的更多相关文章
- lightOJ1370 欧拉函数性质
D - (例题)欧拉函数性质 Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768KB ...
- HDU 6390 GuGuFishtion(莫比乌斯反演 + 欧拉函数性质 + 积性函数)题解
题意: 给定\(n,m,p\),求 \[\sum_{a=1}^n\sum_{b=1}^m\frac{\varphi(ab)}{\varphi(a)\varphi(b)}\mod p \] 思路: 由欧 ...
- poj-2478 Farey Sequence(dp,欧拉函数)
题目链接: Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14230 Accepted: ...
- HDU2824【欧拉函数性质】
思路: 打表. 利用公式. 类似素数打表一样. #include<bits/stdc++.h> using namespace std; const int N=3e6+10; bool ...
- HDOJ 1787 GCD Again(欧拉函数)
GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树
https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...
- hdu2824 The Euler function(欧拉函数个数)
版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/36426357 题目链接:h ...
- LightOJ 1370- Bi-shoe and Phi-shoe (欧拉函数)
题目大意:一个竹竿长度为p,它的score值就是比p长度小且与且与p互质的数字总数,比如9有1,2,4,5,7,8这六个数那它的score就是6.给你T组数据,每组n个学生,每个学生都有一个幸运数字, ...
- lightoj1370欧拉函数/素数筛
这题有两种解法,1是根据欧拉函数性质:素数的欧拉函数值=素数-1(可根据欧拉定义看出)欧拉函数定义:小于x且与x互质的数的个数 #include<map> #include<set& ...
随机推荐
- javascript void运算符
参考链接:http://www.cnblogs.com/ziyunfei/archive/2012/09/23/2698607.html语法: void expr 作用:计算表达式expr,并返回un ...
- CUDA中Bank conflict冲突
转自:http://blog.csdn.net/smsmn/article/details/6336060 其实这两天一直不知道什么叫bank conflict冲突,这两天因为要看那个矩阵转置优化的问 ...
- fork与vfork的区别与联系
fork()与vfock()都是创建一个进程,那他们有什么区别呢?总结有以下三点区别: 1. fork ():子进程拷贝父进程的数据段,代码段 vfork ( ):子进程与父进程共享数据段 2. fo ...
- 19.状态者模式(State Pattern)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 无废话Android之activity的生命周期、activity的启动模式、activity横竖屏切换的生命周期、开启新的activity获取他的返回值、利用广播实现ip拨号、短信接收广播、短信监听器(6)
1.activity的生命周期 这七个方法定义了Activity的完整生命周期.实现这些方法可以帮助我们监视其中的三个嵌套生命周期循环: (1)Activity的完整生命周期 自第一次调用onCrea ...
- 【翻译十】java-固定锁和同步
Intrinsic Locks and Synchronization Synchronization is built around an internal entity known as the ...
- 【JAVA反射机制】
一.Class类 Java.lang.Object |-java.lang.Class<T> 构造方法:无. 常用方法: static Class<?> forName(Str ...
- C# SMTP邮件发送 分类: C# 2014-07-13 19:10 334人阅读 评论(1) 收藏
邮件发送在网站应用程序中经常会用到,包括您现在看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,做了一个客户端Demo,希望对有需要的童鞋有所帮助: 核心代码: ...
- Oracle 备份与恢复介绍
一.Oracle备份方式分类:Oracle有两类备份方式:(1)物理备份:是将实际组成数据库的操作系统文件从一处拷贝到另一处的备份过程,通常是从磁盘到磁带.物理备份又分为冷备份.热备份: (2)逻 ...
- wp8 入门到精通 高仿微信发信息 键盘不消失
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> < ...