1. 程式人生 > >SPOJ4491. Primes in GCD Table(gcd(a,b)=d素數,(1<=a<=n,1<=b<=m))加強版

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

function ted solid result writing set silver %d ron

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))加強版