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

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:
210 10100 100
Output:
302791
 
 
 
 

一样的题,仅仅只是 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;
}
时间: 2024-10-06 22:01:31

SPOJ4491. Primes in GCD Table(gcd(a,b)=d素数,(1&lt;=a&lt;=n,1&lt;=b&lt;=m))加强版的相关文章

SPOJ PGCD - Primes in GCD Table (好题! 莫比乌斯反演+分块求和优化)

PGCD - Primes in GCD Table 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 (greate

Codeforces 338D GCD Table 中国剩余定理

题目链接:点击打开链接 给定n*m的矩阵,[i,j]的点值为gcd(i,j) 给定一个k长的序列,问是否能匹配上 矩阵的某一行的连续k个元素 思路: 我们要求出一个解(i,j) 使得 i<=n && j<=m 此时输出 YES 对于j j % b[0] = 0 j+1 % b[1] = 0 ··· j+l % b[l] = 0 根据定理:若 a == b (mod n) => (a+c) == b+c (mod n) 所以将上式变换为 j % b[0] = 0 j % b

Codeforces Round #323 (Div. 2) C. GCD Table

C. GCD Table The GCD table G of size n × n for an array of positive integers a of length n is defined by formula Let us remind you that the greatest common divisor (GCD) of two positive integers x and y is the greatest integer that is divisor of both

hdoj 2504 又见GCD 【GCD判定】

思路:一个一个的找,因为c不等于b 且b是(a, c)的最大公约数, 所以c是b的整数倍, 每找到一个c就判断与 a的最大公约数是不是b,不是的话,就继续 刚开始的时候 居然把gcd非递归形式忘了...也没想用递归形式.. 又见GCD Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10151    Accepted Submissio

UESTC 923 稳住GCD DP + GCD

定义:dp[i][j] 表示 在前i个数中,使整个gcd值为j时最少取的数个数. 则有方程: gg = gcd(a[i],j) gg == j : 添加这个数gcd不变,不添加,  dp[i][j] = dp[i-1][j] gg != j: t添加,更新答案,                dp[i][gg] = dp[i-1][j] + 1 最后答案为dp[n][g] (g为原始的所有数的gcd) 时间复杂度: O(n*max(a[i])) 代码: #include <iostream>

HDU 5726 GCD 区间GCD=k的个数

GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2742    Accepted Submission(s): 980 Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). There ar

* SPOJ PGCD Primes in GCD Table (需要自己推线性筛函数,好题)

题目大意: 给定n,m,求有多少组(a,b) 0<a<=n , 0<b<=m , 使得gcd(a,b)= p , p是一个素数 这里本来利用枚举一个个素数,然后利用莫比乌斯反演可以很方便得到答案,但是数据量过大,完全水不过去 题目分析过程(从别人地方抄来的) ans = sigma(p, sigma(d, μ(d) * (n/pd) * (m/pd))) Let s = pd, then ans = sigma(s, sigma(p, μ(s/p) * (n/s) * (m/s))

SPOJ - PGCD Primes in GCD Table(莫比乌斯反演)

http://www.spoj.com/problems/PGCD/en/ 题意: 给出a,b区间,求该区间内满足gcd(x,y)=质数的个数. 思路: 设f(n)为 gcd(x,y)=p的个数,那么F(n)为 p | gcd(x,y)的个数,显然可得F(n)=(x/p)*(y/p). 这道题目因为可以是不同的质数,所以需要枚举质数, 但是这样枚举太耗时,所以在这里令t=pk, 这样一来的话,我们只需要预处理u(t/p)的前缀和,之后像之前的题一样分块处理就可以了. 1 #include<ios

SPOJ-PGCD Primes in GCD Table

题目链接:https://vjudge.net/problem/SPOJ-PGCD 题目大意: 给定 \(N\) 和 \(M\),求满足 \((1 \le x \le N), (1 \le y \le M)\),且 \(gcd(x,y)\) 为素数的 \((x,y)\) 的对数. 知识点: 莫比乌斯反演 解题思路: 设 \(g(p)\) 表示满足 \((1 \le x \le N), (1 \le y \le M)\),且 \(gcd(x,y) = p\) 的 \((x,y)\) 的对数.直接求