Mophues

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327670/327670 K (Java/Others)
Total Submission(s): 1922    Accepted Submission(s): 791

Problem Description
As we know, any positive integer C ( C >= 2 ) can be written as the multiply of some prime numbers:
    C = p1×p2× p3× ... × pk
which p1, p2 ... pk are all prime numbers.For example, if C = 24, then:
    24 = 2 × 2 × 2 × 3
    here, p1 = p2 = p3 = 2, p4 = 3, k = 4

Given two integers P and C. if k<=P( k is the number of C's prime factors), we call C a lucky number of P.

Now, XXX needs to count the number of pairs (a, b), which 1<=a<=n , 1<=b<=m, and gcd(a,b) is a lucky number of a given P ( "gcd" means "greatest common divisor").

Please note that we define 1 as lucky number of any non-negative integers because 1 has no prime factor.

 
Input
The first line of input is an integer Q meaning that there are Q test cases.
Then Q lines follow, each line is a test case and each test case contains three non-negative numbers: n, m and P (n, m, P <= 5×105. Q <=5000).
 
Output
For each test case, print the number of pairs (a, b), which 1<=a<=n , 1<=b<=m, and gcd(a,b) is a lucky number of P.
 
Sample Input
2
10 10 0
10 10 1
 
Sample Output
63
93
 
http://blog.csdn.net/wh2124335/article/details/11846661 转载自此
 
记录一下自己的思路
未简化过的代码核心应该是这样的
 
   for(int i=;i<=n;++i)//枚举每个因子
if(d[i]<=k)//如果因子的素数质因子小于等于k
for(int j=i;j<=n;j+=i) ans+=u(j/i)*(n/i)*(m/i)//枚举F(i);

利用的是第二个,然后可以发现,对于每个数字i,他的倍数j的系数都要加上u[j/i],可以与处理出来U(N),其中U(i)就是u[i/第一个因子]+u[i/第二个因子]+....(这里的U先不考虑素因子个数限制)

那么上述式子就可以化简成为

for(int i=;i<=n;++i) ans+=U(i)*(n/i)*(m/i);//直接枚举

然后U(i)考虑素因子个数限制的话,那么显然预处理也是可以搞出来的,详细见代码,代码里的cnt[N][19]就是U考虑限制的。

然后就是普通的分块操作,为了简化时间,因为W=(n/i)*(m/i),i倘若在一定范围内,这个W是不变的,所以可以加速。

所以最后就是这样了

for(int i=,last=i;i<=n;i=last+){
last=min(n/(n/i),m/(m/i));
ans+=(ll)(cnt[last][k]-cnt[i-][k])*(n/i)*(m/i);
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
using namespace std;
const int maxn = ;
typedef long long ll;
int mu[maxn],sum[maxn],num[maxn];
ll cnt[maxn][];
bool flag[maxn];
vector<int>prime;
void init(){
mu[]=;
for(int i=;i<maxn;i++){
if(!flag[i]){
prime.push_back(i);
mu[i]=-;
num[i]=;
}
for(int j=;j<prime.size()&&i*prime[j]<maxn;j++){
flag[i*prime[j]]=true;
num[i*prime[j]]=num[i]+;
if(i%prime[j])mu[i*prime[j]]=-mu[i];
else {mu[i*prime[j]]=;break;}
}
}
for(int i=;i<maxn;i++){
for(int j=i;j<maxn;j+=i){
cnt[j][num[i]]+=mu[j/i];
}
}
for(int i=;i<maxn;i++){
for(int j=;j<;j++){
cnt[i][j]+=cnt[i][j-];
}
}
for(int i=;i<maxn;i++){
for(int j=;j<;j++){
cnt[i][j]+=cnt[i-][j];
}
}
}
int main(){
init();
int q;
scanf("%d",&q);
while(q--){
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
k=min(k,);
ll ans=;
if(n>m)swap(n,m);
for(int i=,last=i;i<=n;i=last+){
last=min(n/(n/i),m/(m/i));
ans+=(ll)(cnt[last][k]-cnt[i-][k])*(n/i)*(m/i);
}
//printf("%lld\n",ans);
printf("%I64d\n",ans);
}
}
 

最新文章

  1. 区别和详解:jQuery extend()和jQuery.fn.extend()
  2. 【Origin】jquery.barddialog.js
  3. 如何开发 Sublime Text 2 的插件
  4. 公共语言运行库(CLR)和中间语言(IL)(一)
  5. Sharepoint 2010 Workflow 发布
  6. 手机端Zepto框架,利用swipejs插件做banner轮播图
  7. android——ObjectAnimator动画
  8. 写一个Windows上的守护进程(1)开篇
  9. golang之interface(接口)与 reflect 机制
  10. Canvas drawImage API
  11. python从开始到放弃的途中一直很菜的day13
  12. array_column函数
  13. 微信小程序之内嵌网页(webview)
  14. node.js中stream流中可读流和可写流的使用
  15. Hadoop-调优剖析
  16. Python的程序入口 __name__属性
  17. 【HTML5】HTML5的自学路线
  18. Linux文件删除,但是df之后磁盘空间没有释放
  19. linux增加自己的可执行目录 $PATH
  20. (转)MySQL 主从复制搭建,基于日志(binlog

热门文章

  1. linux rpm包
  2. 【Linux题目】第四关
  3. mac OS git关联github
  4. Mybatis自动生成插件对数据库类型为text的处理
  5. Linux下进程的创建(system(); fork(); exec*())
  6. Ubuntu上安装配置Java环境
  7. Leetcode2 两数相加 Python
  8. P3119 [USACO15JAN]Grass Cownoisseur G
  9. python——append后的列表为什么打印出来为空
  10. js递归实现方式