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的个数,那么 ...
随机推荐
- BZOJ4044: [Cerc2014] Virus synthesis(回文树+DP)
Description Viruses are usually bad for your health. How about fighting them with... other viruses? ...
- 【Codeforces Round #451 (Div. 2) D】Alarm Clock
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 尺取法+二分. 类似滑动窗口. 即左端点为l,右端点为r. 维护a[r]-a[l]+1总是小于等于m的就好. (大于m就右移左端点) ...
- Spring入门--控制反转(IOC)与依赖注入(DI)
1.控制反转(Inversion of Control)与依赖注入(Dependency Injection) 控制反转即IoC (Inversion of Control).它把传统上由程序 ...
- BFS模版程序
本文转自q=bfs&u=cnyali&t=blog">http://so.csdn.net/so/search/s.do?q=bfs&u=cnyali& ...
- 有趣的Ruby-学习笔记4
Ruby块 块.在我看来就是插入一段可变的函数 block_name{ statement1 statement2 .......... } 看起来不知道是什么,只是别急,继续往下看. 块函数通过yi ...
- Android中实现整个视图切换的左右滑动效果
Android中提供了一个Gallary,可以实现图片或者文本的左右滑动效果. 如何让整个视图都能实现左右滑动,达到类似于Gallary的效果呢?可以直接用一个开源的ViewFlow来实现. 项目 ...
- (错误记录)git push 报错 403
在push的时候遇到错误: RPC failed; HTTP curl The requested URL returned error: Forbidden 如果是自己创建的项目的话,可以在网上找到 ...
- MyBatis学习总结(14)——Mybatis使用技巧总结
1. 区分 #{} 和 ${}的不同应用场景 1)#{} 会生成预编译SQL,会正确的处理数据的类型,而${}仅仅是文本替换. 对于SQL: select * from student where x ...
- mycat 之datanode datahost writehost readhost 区别(转)
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> &l ...
- call.apply.冒充对象继承
call方法:让调用对象执行,然后第一参数是谁.调用对象的this就改变,指向谁,后边跟参数,依次对应传入 apply方法:让调用对象执行,然后第一参数是谁.调用对象的this就改变指向是谁,后边跟参 ...