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))加强版的更多相关文章

  1. SPOJ PGCD 4491. Primes in GCD Table && BZOJ 2820 YY的GCD (莫比乌斯反演)

    4491. Primes in GCD Table Problem code: PGCD Johnny has created a table which encodes the results of ...

  2. 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 define ...

  3. Codeforces Round #323 (Div. 1) A. GCD Table

    A. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

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

    C. GCD Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/583/problem/C ...

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

    题目链接:http://codeforces.com/contest/583/problem/C C. GCD Table time limit per test 2 seconds memory l ...

  6. CF582A GCD Table

    A. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. HDU 5726 GCD 区间GCD=k的个数

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  8. UVA 1642 Magical GCD(gcd的性质,递推)

    分析:对于区间[i,j],枚举j. 固定j以后,剩下的要比较M_gcd(k,j) = gcd(ak,...,aj)*(j-k+1)的大小, i≤k≤j. 此时M_gcd(k,j)可以看成一个二元组(g ...

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

    http://www.spoj.com/problems/PGCD/en/ 题意: 给出a,b区间,求该区间内满足gcd(x,y)=质数的个数. 思路: 设f(n)为 gcd(x,y)=p的个数,那么 ...

随机推荐

  1. CISP/CISA 每日一题 21

    CISSP 每日一题(答)What is the term that identifies data ona disk after the data has supposedly been erase ...

  2. Direct2D开发:MFC下从资源文件中加载位图

    转载请注明出处:http://www.cnblogs.com/ye-ming 0X01 概述: 相对于GDI处理界面,Direct2D有得天独厚的优势,下图就是Direct2D与GDI的效果对比,wi ...

  3. 4lession-输入函数

    接受字符串的方法 #!/usr/bin/python string = raw_input("\nplease inter you string:\n") print(string ...

  4. Instant Client 配置

    Instant Client Download 选择  Instant Client for Microsoft Windows (32-bit)  由于PL/SQL Developer 不支持64b ...

  5. (最新)使用爬虫刷CSDN博客访问量——亲测有效

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 1.概述 前言:前两天刚写了第一篇博客https://blog.csdn.net/qq_41782425/article/deta ...

  6. mahout測试朴素贝叶斯分类样例

    对于这个測试建议大家先理解原理,这里我画了例如以下的示意图 接下来就依照例如以下的细节来输入指令測试: 首先前提是Hadoop安装并启动,mahout已经安装了. <strong>< ...

  7. Android ServiceManager启动

    许久就想写篇关于servicemanager的文章,之前对服务启动顺序诸如zygote,systemserver.等启动顺序理解有点混乱,现做例如以下理解分析: 事实上init进程启动后,Servic ...

  8. 适用android的MVP:怎样组织展示层

    原文 MVP for Android:How to organize presentation layer http://antonioleiva.com/mvp-android/ 译文 MVP(Mo ...

  9. Caused by: java.lang.NoSuchMethodException: &lt;init&gt; [class android.content.Context, interface android

     在写自己定义的view时,有时会报下面错误: Caused by: java.lang.NoSuchMethodException: <init> [class android.co ...

  10. Eclipse "Could not create java virtual machine"的问题解决

    今天到了新的环境,需要重新搭建Android的开发环境,下载eclipse并安装了JDK1.6后,启动eclipse,发现出现了错误“Could not create Javavirtual mach ...