SPOJ4491. Primes in GCD Table(gcd(a,b)=d素数,(1<=a<=n,1<=b<=m))加强版
Posted llguanli
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SPOJ4491. Primes in GCD Table(gcd(a,b)=d素数,(1<=a<=n,1<=b<=m))加强版相关的知识,希望对你有一定的参考价值。
SPOJ4491. Primes in GCD TableProblem code: PGCD |
Johnny has created a table which encodes the results of some operation -- a function of two arguments. But instead of a boring multiplication table of the sort you learn by heart at prep-school, he has created a GCD (greatest common divisor) table! So he now has a table (of height a and width b), indexed from (1,1) to (a,b), and with the value of field (i,j) equal to gcd(i,j). He wants to know how many times he has used prime numbers when writing the table.
Input
First, t ≤ 10, the number of test cases. Each test case consists of two integers, 1 ≤ a,b < 107.
Output
For each test case write one number - the number of prime numbers Johnny wrote in that test case.
Example
Input:
2
10 10
100 100
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> using namespace std; const int maxn=1e7+5; typedef long long LL; LL pri[maxn],pnum; LL mu[maxn]; LL g[maxn]; LL sum[maxn]; bool vis[maxn]; void mobius(int N) { LL i,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]; } } int main() { mobius(10000000); int T; scanf("%d",&T); while(T--) { LL n,m; scanf("%lld%lld",&n,&m); if(n>m) swap(n,m); LL t,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); } return 0; }
以上是关于SPOJ4491. Primes in GCD Table(gcd(a,b)=d素数,(1<=a<=n,1<=b<=m))加强版的主要内容,如果未能解决你的问题,请参考以下文章