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的个数,那么 ...
随机推荐
- HDU4825 Xor Sum(贪心+Trie树)
Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...
- 使用Python开发轻量级的Web框架以及基于WSGI的服务器来实现一个网站页面
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶项目说明 二丶数据准备 三丶使用网络TCP开发一个基于WSGI协议的Web服务器 四丶使用python3开发一个轻量级的 ...
- vim学习4
分频 参考 http://coolshell.cn/articles/1679.htm
- [React] Call setState with null to Avoid Triggering an Update in React 16
Sometimes it’s desired to decide within an updater function if an update to re-render should be trig ...
- 请使劲回答一个关于UNIX/Linux自己主动扩展stack的问题
有本事就出来,没本事就当鳖! 假设让我回答关于进程栈,线程栈的问题,仅仅要问题不笼统,仅仅要问题明白.我会一五一十地回答,正确率上九成,然而,可悲的是,问题往往他妈的都不是非常明白,因此,游戏到此结束 ...
- sqlite3-查看数据库
在做android开发的时候,有时候我们需要查看系统下的数据库,这时候我们可以使用下面的方法 1.数据库存放位置 data/data/package/databases/abc.db 2.导出数据库 ...
- FreeMarker template error: The following has evaluated to null or missing
使用freemarker前端分页,报错: FreeMarker template error: The following has evaluated to null or missing 后端直接赋 ...
- Stack overflow 编译能通过,运行时出现Stack overflow
Stack overflow 编译能通过,运行时出现Stack overflow 大家都知道,Windows程序的内存机制大概是这样的,全局变量(局部的静态变量本质也属于此范围)存储于堆内存,该段内存 ...
- 洛谷—— P2234 [HNOI2002]营业额统计
https://www.luogu.org/problem/show?pid=2234 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业 ...
- Traveler Nobita (zoj 3456 最小生成树)
Traveler Nobita Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita used a time machin ...