您的位置:首页 > 其它

SPOJ4491. Primes in GCD Table(gcd(a,b)=d素数,(1<=a<=n,1<=b<=m))加强版

2015-08-22 12:52 393 查看


SPOJ4491.PrimesinGCDTable



Problemcode:PGCD

Johnnyhascreatedatablewhichencodestheresultsofsomeoperation--afunctionoftwoarguments.Butinsteadofaboringmultiplicationtableofthesortyoulearnbyheartatprep-school,hehascreatedaGCD(greatestcommondivisor)table!
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;

链接:http://www.spoj.com/problems/PGCD/

转载请注明出处:寻找&星空の孩子



详解:http://download.csdn.net/detail/u010579068/9034969







#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: