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 ...
随机推荐
- 转:python webdriver API 之简单对象的定位
对象(元素)的定位和操作是自动化测试的核心部分,其中操作又是建立在定位的基础上的,因此元素定位就显得非常重要. (本书中用到的对象与元素同为一个事物)一个对象就像是一个人,他会有各种的特征(属性) , ...
- php4.3.4.4、apache2.0.4.8、mysql 4.0.26、window7 配置过程
apache的安装不需要过程,直接默认安装,下一步 下一步就Ok了. php4的安装: 1 将php-4.0.4-Win32.zip(最新版本4.0.4)解压缩到硬盘的一个目录中,例如解压到E:php ...
- 解决Android抽屉被击穿问题
1,创建一个抽屉DrawerLayout,在V4包下android.support.v4.widget.DrawerLayout,在要设置抽屉的布局中设置android:layout_gravity= ...
- Bishop的大作《模式识别与机器学习》Ready to read!
久仰Bishop的大作“Pattern Recognition and Machine Learning”已久,在我的硬盘里已经驻扎一年有余,怎奈惧其页数浩瀚,始终未敢入手.近日看文献,屡屡引用之.不 ...
- ajax“显示弹窗详情”和“删除”功能练习
1.查看详细信息,以弹窗的形式显示,使用ajax 2.批量删除 “查询”功能可以参考前面的文章,这里只讲解ajax“显示弹窗详情”和“删除”功能 第一:在body中的代码 <title>a ...
- Install the 64bit library in Ubuntu13.10
After installed Ubuntu13.10, and i want to run a 32bit software, in the pass, you just run sudo apt- ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON Cast 使用方式
zw版[转发·台湾nvp系列Delphi例程]HALCON Cast 使用方式 procedure TForm1.Button1Click(Sender: TObject);var img, img1 ...
- 使用uiautomatorviewer和uiautomator来做android的UI测试
来自:http://university.utest.com 作者:Angelos Nakulas (All Authored Courses) 译者:Elaine00 目录 简介 什 ...
- Linux修改系统以及pip更新源
Linux修改系统以及pip更新源 时间:2015-08-01来源:csdn 作者:henulwj 修改系统更新源 你是否跟我一样在刚看时接触Linux时被系统更新源问题搞得晕头转向,不同的Linux ...
- innodb内部的并发线程
1. innodb_thread_concurrency innodb有一系列的计数器来统计和控制内部的工作线程.其中最重要的一个是innodb_thread_concurrency,和它相关的inn ...