首页 > 其他 > 详细

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

时间:2017-07-30 16:50:55      阅读:360      评论:0      收藏:0      [点我收藏+]

SPOJ4491. Primes in GCD Table

 

Problem 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&lt;=a&lt;=n,1&lt;=b&lt;=m))加强版

原文:http://www.cnblogs.com/llguanli/p/7259425.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!