SPOJ4491. Primes in GCD Table(gcd(a,b)=d素数,(1<=a<=n,1<=b<=m))加强版
SPOJ4491. Primes in GCD TableProblem 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<=a<=n,1<=b<=m))加强版的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- CF582A GCD Table
A. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- HDU 5726 GCD 区间GCD=k的个数
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- 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 ...
- SPOJ - PGCD Primes in GCD Table(莫比乌斯反演)
http://www.spoj.com/problems/PGCD/en/ 题意: 给出a,b区间,求该区间内满足gcd(x,y)=质数的个数. 思路: 设f(n)为 gcd(x,y)=p的个数,那么 ...
随机推荐
- git还原本地提交的某个历史记录
转载地址:http://jingyan.baidu.com/article/e4511cf33479812b855eaf67.html 1.以还原index2.html文件为例,打开index2.ht ...
- android插件式开发资料整理
1.DL : Apk动态载入框架 2.android中的动态载入机制
- 用Promise对象封装JQuery的AJAX过程
let jqPostAjaxPromise = function(param){ return new Promise(function(resolve, reject){ $.ajax({ url: ...
- ajax利用php上传图片
<script type="text/javascript"> window.onload = function(){ document.getElementById( ...
- Java设计模式——代理模式实现及原理
简介 Java编程的目标是实现现实不能完成的,优化现实能够完成的,是一种虚拟技术.生活中的方方面面都可以虚拟到代码中.代理模式所讲的就是现实生活中的这么一个概念:中介. 代理模式的定义:给某一个对象提 ...
- POJ 3723 Conscription MST
http://poj.org/problem?id=3723 题目大意: 需要征募女兵N人,男兵M人,没征募一个人需要花费10000美元,但是如果已经征募的人中有一些关系亲密的人,那么可以少花一些钱, ...
- Instant Client 配置
Instant Client Download 选择 Instant Client for Microsoft Windows (32-bit) 由于PL/SQL Developer 不支持64b ...
- django-rest-framework框架 第一篇
本课件是为了教学任务自己写的学习django-rest-framework框架. 方便自己授课,也成为学生的复习教程. 本课程学习后:具有REST编程思维:并可以通过django及专业的django- ...
- Css fixed和absolute定位差别
fixed:固定定位 absolute:绝对定位 差别非常easy: 1.没有滚动栏的情况下没有差异 2.在有滚动栏的情况下.fixed定位不会随滚动栏移动而移动.而absolute则会随滚动栏移动 ...
- pcb过孔盖油
pcb的过孔应该盖油,,这样,两个距离比较紧的过孔就不会在焊接的时候短路了,尤其是手工焊接小件的时候.