SPOJ4491. Primes in GCD Table(gcd(a,b)=d素数,(1<=a<=n,1<=b<=m))加强版
2015-08-22 12:52
393 查看
|
Sohenowhasatable(ofheightaandwidthb),
indexedfrom(1,1)to(a,b),andwiththevalue
offield(i,j)equaltogcd(i,j).
Hewantstoknowhowmanytimeshehasusedprimenumberswhenwritingthetable.
Input
First,t≤10,thenumberoftestcases.Eachtestcaseconsistsoftwointegers,1≤a,b<107.
Output
Foreachtestcasewriteonenumber-thenumberofprimenumbersJohnnywroteinthattestcase.Example
Input:
2 1010 100100
Output:
30 2791
一样的题,只不过GCD(x,y)=素数.1<=x<=a;1<=y<=b;
链接:
转载请注明出处:
详解:
#include<stdio.h>
#include<string.h>
#include<algorithm>
usingnamespacestd;
constintmaxn=1e7+5;
typedeflonglongLL;
LLpri[maxn],pnum;
LLmu[maxn];
LLg[maxn];
LLsum[maxn];
boolvis[maxn];
voidmobius(intN)
{
LLi,j;
pnum=0;
memset(vis,false,sizeof(vis));
vis[1]=true;
mu[1]=1;
for(i=2;i<=N;i++)
{
if(!vis[i])//pri
{
pri[pnum++]=i;
mu[i]=-1;
g[i]=1;
}
for(j=0;j<pnum&&i*pri[j]<=N;j++)
{
vis[i*pri[j]]=true;
if(i%pri[j])
{
mu[i*pri[j]]=-mu[i];
g[i*pri[j]]=mu[i]-g[i];
}
else
{
mu[i*pri[j]]=0;
g[i*pri[j]]=mu[i];
break;//think...
}
}
}
sum[0]=0;
for(i=1;i<=N;i++)
{
sum[i]=sum[i-1]+g[i];
}
}
intmain()
{
mobius(10000000);
intT;
scanf("%d",&T);
while(T--)
{
LLn,m;
scanf("%lld%lld",&n,&m);
if(n>m)swap(n,m);
LLt,last,ans=0;
for(t=1;t<=n;t=last+1)
{
last=min(n/(n/t),m/(m/t));
ans+=(n/t)*(m/t)*(sum[last]-sum[t-1]);
}
printf("%lld\n",ans);
}
return0;
}
相关文章推荐
- json 转换,手机获取验证码
- julia/pyplot 画图添加标签和标题
- Codeforces 478B Random Teams
- Sparsity and Some Basics of L1 Regularization (学习Free Mind知识整理)
- c#操作XML
- 文摘
- ISO/IEC 9899:2011 条款6.3——转换
- 查询结果一键删除并实时更新
- 有12个乒乓球,其中有一个不合规格,但不知是轻是重。要求用天平称三次,把这个坏球找出来。
- SQLServer复制表
- lightOJ 1317 Throwing Balls into the Baskets
- SQLServer错误:过程 sp_addextendedproperty,第 xxx 行对象无效。'dbo.xxx.xxx' 不允许有扩展属性,或对象不存在。
- 日志分析概述
- C++ Primer 5e chapter 11
- RMI(远程方法调用)实现远程操作电脑的步骤
- hdu 1145: 不容易系列2
- 第 5 章 第二个 activity
- C#实现实体类与字符串互相转换的方法
- WPA/WAP2wifi 密码破解笔记
- leetcode 153 —— Find Minimum in Rotated Sorted Array