bzoj 2818: Gcd GCD(a,b) = 素数
2818: Gcd
Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 1566 Solved: 691
[Submit][Status]
Description
给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
数对(x,y)有多少对.
Input
一个整数N
Output
如题
Sample Input
Sample Output
HINT
hint
对于样例(2,2),(2,4),(3,3),(4,2)
1<=N<=10^7
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std; typedef long long LL;
const int maxn = 1e7+;
bool s[maxn];
int prime[maxn],len = ;
int mu[maxn];
int g[maxn];
int sum1[maxn];
void init()
{
memset(s,true,sizeof(s));
mu[] = ;
for(int i=; i<maxn; i++)
{
if(s[i] == true)
{
prime[++len] = i;
mu[i] = -;
g[i] = ;
}
for(int j=; j<=len && (long long)prime[j]*i<maxn; j++)
{
s[i*prime[j]] = false;
if(i%prime[j]!=)
{
mu[i*prime[j]] = -mu[i];
g[i*prime[j]] = mu[i] - g[i];
}
else
{
mu[i*prime[j]] = ;
g[i*prime[j]] = mu[i];
break;
}
}
}
for(int i=; i<maxn; i++)
sum1[i] = sum1[i-]+g[i];
} int main()
{
int a;
init();
while(scanf("%d",&a)>)
{
LL sum = ;
for(int i=,la = ; i<=a; i = la+)
{
la = a/(a/i);
sum = sum + (long long)(sum1[la] - sum1[i-])*(a/i)*(a/i);
}
printf("%lld\n",sum);
}
return ;
}
spoj
4491. 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;
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std; typedef long long LL;
const int maxn = 1e7+;
bool s[maxn];
int prime[maxn],len = ;
int mu[maxn];
int g[maxn];
int sum1[maxn];
void init()
{
memset(s,true,sizeof(s));
mu[] = ;
for(int i=;i<maxn;i++)
{
if(s[i] == true)
{
prime[++len] = i;
mu[i] = -;
g[i] = ;
}
for(int j=;j<=len && (long long)prime[j]*i<maxn;j++)
{
s[i*prime[j]] = false;
if(i%prime[j]!=)
{
mu[i*prime[j]] = -mu[i];
g[i*prime[j]] = mu[i] - g[i];
}
else
{
mu[i*prime[j]] = ;
g[i*prime[j]] = mu[i];
break;
}
}
}
for(int i=;i<maxn;i++)
sum1[i] = sum1[i-]+g[i];
} int main()
{
int T,a,b;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&a,&b);
if(a>b) swap(a,b);
LL sum = ;
for(int i=,la = ;i<=a;i = la+)
{
la = min(a/(a/i),b/(b/i));
sum = sum + (long long)(sum1[la] - sum1[i-])*(a/i)*(b/i);
}
printf("%lld\n",sum);
}
return ;
}
bzoj 2818: Gcd GCD(a,b) = 素数的更多相关文章
- BZOJ 2818: Gcd(欧拉函数)
GCDDescription 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 ...
- 【BZOJ 2818】Gcd - 筛法求素数&phi()
题目描述 给定整数,求且为素数的数对有多少对. 分析 首先筛出所有的素数. 我们考虑枚举素数p,统计满足的个数,等价于统计的个数,即统计以内满足互质的有序数对个数. 不难发现,也就是说,我们只要预处理 ...
- 【BZOJ 2818】 GCD
[题目链接] 点击打开链接 [算法] 线性筛出不大于N的所有素数,枚举gcd(x,y)(设为p),问题转化为求(x,y)=p的个数 设x=x'p, y=y'p,那么有(x,y)=1且 ...
- 【BZOJ 2818】gcd 欧拉筛
枚举小于n的质数,然后再枚举小于n/这个质数的Φ的和,乘2再加1即可.乘2是因为xy互换是另一组解,加1是x==y==1时的一组解.至于求和我们只需处理前缀和就可以啦,注意Φ(1)的值不能包含在前缀和 ...
- BZOJ 2818 GCD 素数筛+欧拉函数+前缀和
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2818 题意:给定整数N,求1<=x,y<=n且Gcd(x,y)为素数的数对( ...
- 【BZOJ】【2818】Gcd
欧拉函数/莫比乌斯函数 嗯……跟2190很像的一道题,在上道题的基础上我们很容易就想到先求出gcd(x,y)==1的组,然后再让x*=prime[i],y*=prime[i]这样它们的最大公约数就是p ...
- bzoj 2818 GCD 数论 欧拉函数
bzoj[2818]Gcd Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Samp ...
- BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4436 Solved: 1957[Submit][Status][Discuss ...
- BZOJ 2818: Gcd
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4443 Solved: 1960[Submit][Status][Discuss ...
随机推荐
- zjuoj 3603 Draw Something Cheat
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3603 Draw Something Cheat Time Limit: 2 ...
- SQL 面向对象(委托)
委托:也称为代理,事件也是一种委托:定义在类的最外面 1.定义委托关键字:delegate函数签名:签名和函数保持一致定义委托的时候要根据函数来定义public delegate int First( ...
- 通过struts.xml搭建、为属性注入值_2015.01.04
01:web.xml配置: <?xml version="1.0" encoding="UTF-8"?> <web-app version=& ...
- ios json parse
参考:http://wenxin2009.iteye.com/blog/1671691
- 在desk于webi中资料查询不一致
1,首先检查是否勾选重复列,重复列造成 2,缓存
- [Ubuntu] Error: The disk drive for /media/sda2 is not ready yet or not present
After updated Ubuntu, and reboot, I got these error The disk drive for /media/sda2 is not ready yet ...
- [ThinkPHP] 输出、模型的使用
# # ThinkPHP 3.1.2 输出和模型使用 # 讲师:赵桐正 微博:http://weibo.com/zhaotongzheng 本节课大纲: 一.ThinkPHP 3 的输出 ...
- C++中关于无法解析的外部符号问题LNK2019问题的总结
网上一般有很全面的解决方法,最近恰好本道长也遇到了这种问题,也恰好解决了,这种问题应该算作配置问题,而非程序本身问题,多数是因为接手了生疏的程序导致,此问题看上去很简单,但木有经验的话很 ...
- hadoop文件系统FileSystem详解 转自http://hi.baidu.com/270460591/item/0efacd8accb7a1d7ef083d05
Hadoop文件系统 基本的文件系统命令操作, 通过hadoop fs -help可以获取所有的命令的详细帮助文件. Java抽象类org.apache.hadoop.fs.FileSystem定义了 ...
- 一个容易被忽略的ReportingService超时问题
我们在使用Sql Server Reporting Service开发报表的时候,经常会遇到报表超时的问题,报表超时的原因有很多,也有很多地方可以设置报表的超时时间,比如在报表中的数据源(dataso ...