hdu 1695 GCD 容斥+欧拉函数
题目链接
求 $ x\in[1, a] , y \in [1, b] $ 内 \(gcd(x, y) = k\)的(x, y)的对数。
问题等价于$ x\in[1, a/k] , y \in [1, b/k] $ 内 \(gcd(x, y) = 1\) 的(x, y)的对数。
假设a < b, 那么[1, a/k]这部分可以用欧拉函数算。 设 \(i\in (a/k, b/k]\), (a/k, b/k]这部分可以用容斥算, 用a/k减去[1, a/k]里面和i不互质的数的个数。
具体看代码。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1e5+5;
ll phi[maxn];
vector <int> v[maxn];
void init() {
phi[1] = 1;
for(int i = 2; i<=100000; i++) {
if(!phi[i]) {
for(int j = i; j<=100000; j+=i ) {
if(!phi[j])
phi[j] = j;
phi[j] = phi[j]/i*(i-1);
v[j].pb(i); //筛出j的素因子
}
}
phi[i] += phi[i-1]; //维护前缀和
}
}
int cal(int n, int b) { //算[1, b]中和n互质的数的个数
int len = v[n].size();
int sum = 1<<len;
int ret = 0;
for(int i = 1; i < sum; i++) {
int tmp = 1, cnt = 0;
for(int j = 0; j<len; j++) {
if((1<<j)&i) {
cnt++;
tmp *= v[n][j];
}
}
if(cnt & 1)
ret += b/tmp;
else
ret -= b/tmp;
}
return b-ret;
}
int main()
{
int cnt = 1, t, n, m, a, b, k;
cin>>t;
init();
while(t--) {
scanf("%d%d%d%d%d", &a, &a, &b, &b, &k);
printf("Case %d: ", cnt++);
if(a>b)
swap(a, b);
if(k == 0 || k>a||k>b) {
puts("0");
continue;
}
a /= k;
b /= k;
ll ans = phi[a];
for(int i = a+1; i<=b; i++) {
ans += cal(i, a);
}
cout<<ans<<endl;
}
return 0;
}
hdu 1695 GCD 容斥+欧拉函数的更多相关文章
- HDU 1695 GCD (容斥原理+欧拉函数)
题目链接 题意 : 从[a,b]中找一个x,[c,d]中找一个y,要求GCD(x,y)= k.求满足这样条件的(x,y)的对数.(3,5)和(5,3)视为一组样例 . 思路 :要求满足GCD(x,y) ...
- HDU 3970 Harmonious Set 容斥欧拉函数
pid=3970">链接 题解:www.cygmasot.com/index.php/2015/08/17/hdu_3970 给定n 求连续整数[0,n), 中随意选一些数使得选出的 ...
- HDU 1695 GCD 容斥
GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...
- HDU - 1695 GCD (容斥+枚举)
题意:求区间1<=i<=b与区间1<=j<=d之间满足gcd(i,j) = k 的数对 (i,j) 个数.(i,j)与(j,i) 算一个. 分析:gcd(i,j)=k可以转化为 ...
- GCD nyoj 1007 (欧拉函数+欧几里得)
GCD nyoj 1007 (欧拉函数+欧几里得) GCD 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 The greatest common divisor ...
- HDU1695:GCD(容斥原理+欧拉函数+质因数分解)好题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题目解析: Given 5 integers: a, b, c, d, k, you're to ...
- HDU 4483 Lattice triangle(欧拉函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4483 题意:给出一个(n+1)*(n+1)的格子.在这个格子中存在多少个三角形? 思路:反着想,所有情 ...
- UVA 11424 GCD - Extreme (I) (欧拉函数+筛法)
题目:给出n,求gcd(1,2)+gcd(1,3)+gcd(2,3)+gcd(1,4)+gcd(2,4)+gcd(3,4)+...+gcd(1,n)+gcd(2,n)+...+gcd(n-1,n) 此 ...
- UVA 11426 GCD - Extreme (II) (欧拉函数+筛法)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...
随机推荐
- 推荐书目 - C++学习资料
前言 在本文的前半部分我我会谈谈 我看过的书,和我个人的一些理解 ,并且会提供 C++标准委员会相关链接 和 C++第三方轮子/库总结 .本文的后半部分翻译了来自 The Definitive C++ ...
- GCD实现简单的单例类-Singletion
什么是单例模式 1.单例模式是一个类在系统中只有一个实例对象.通过全局的一个入口点对这个实例对象进行访问.在 iOS 开发中,单例模式是非常有用的一种设计模式.如 下图,是一个简单单例模式的 UML ...
- iOS学习心得——UINavigationController
UINavigationController和UItableviewController一样也是iOS开发中常用的控件之一,今天就来学习一下它的常见用法. 有人说tab ...
- TCP/IP详解之:TCP
第17章 TCP:传输控制协议 TCP提供了一种可靠的面向连接的字节流运输层服务 TCP的服务 尽管TCP和UDP都使用相同的网络层(IP),TCP却向应用层提供与UDP完全不同的服务. TCP通过下 ...
- Lazarus中TreeView导出XML以及XML导入TreeView
本来说是要给自己的某程序加一个xml导出功能,但是自己也没接触过xml,加之delphi和lazarus的xml部分还都不一样,折腾好久(整一天)才解决问题.. 如下是作为导出功能的组件部分: uni ...
- switch 与 whille相互套用
一直自以为还比较了解C++,这两天写个小工具结果出现了个bug,查了几个小时.现在才发现我这么水. switch是C++后来推出了,目的在于提高代码结构清晰度. 但是switch与while连用时是有 ...
- tornado 使用过程中提示‘no module name ioloop’
千万不要将程序文件命名为tornado.py 不然永远不会运行,因为运行后会在tornado.py里查找ioloop信息,永远找不到...
- 各种HelloWorld
http://blog.csdn.net/whxaing2011/article/details/20736759 ES总结: http://www.cnblogs.com/sunxucool/p/3 ...
- Qt 多线程与数据库操作需要注意的几点问题(QSqlDatabase对象只能在当前线程里使用)
彻底抛弃MFC, 全面应用Qt 已经不少时间了.除了自己看书按步就班做了十几个验证性的应用,还正式做了3个比较大的行业应用,总体感觉很好.Native C++ 下, Qt 基本是我用过的最简便的界面库 ...
- firemonkey打开子窗体(匿名回调函数)
procedure TForm1.Button1Click(Sender: TObject);varChildForm: TForm2;beginChildForm := TForm2.Create( ...