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

本文介绍了解决SPOJ平台上的PGCD问题的方法,该问题是关于计算一个由最大公约数构成的表格中素数出现次数的问题。通过构建一个高效的算法来遍历指定范围内的所有整数对,并统计当两个数的最大公约数为素数的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/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;
}


 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值