HDU 1695 GCD (欧拉函数+容斥原理)
GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4272 Accepted Submission(s): 1492
Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same.
Yoiu can assume that a = c = 1 in all test cases.
Each case contains five integers: a, b, c, d, k, 0 < a <= b <= 100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as described above.
1 3 1 5 1
1 11014 1 14409 9
Case 2: 736427
For the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5).
题意: 在1~a, 1~b中挑出(x,y)满足gcd(x,y) = k , 求(x,y) 的对数 , a,b<=10^5
思路: gcd(x, y) == k 说明x,y都能被k整除, 但是能被k整除的未必gcd=k , 必须还要满足
互质关系. 问题就转化为了求1~a/k 和 1~b/k间互质对数的问题
可以把a设置为小的那个数, 那么以y>x来保持唯一性(题目要求, 比如[1,3] = [3,1] )
接下来份两种情况:
1. y <= a , 那么对数就是 1~a的欧拉函数的累计和(容易想到)
2. y >= a , 这个时候欧拉函数不能用了,怎么做? 可以用容斥原理,把y与1~a互质对数问题转换为
/* ***********************************************
Author :kuangbin
Created Time :2013/8/19 22:08:43
File Name :F:\2013ACM练习\专题学习\数学\HDU\HDU1695GCD.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; const int MAXN = ;
int prime[MAXN+];
void getPrime()
{
memset(prime,,sizeof(prime));
for(int i = ;i <= MAXN;i++)
{
if(!prime[i])prime[++prime[]] = i;
for(int j = ;j <= prime[] && prime[j] <= MAXN/i;j++)
{
prime[prime[j]*i] = ;
if(i%prime[j] == )break;
}
}
}
long long factor[][];
int fatCnt;
int getFactors(long long x)
{
fatCnt = ;
long long tmp = x;
for(int i = ; prime[i] <= tmp/prime[i];i++)
{
factor[fatCnt][] = ;
if(tmp%prime[i] == )
{
factor[fatCnt][] = prime[i];
while(tmp%prime[i] == )
{
factor[fatCnt][]++;
tmp /= prime[i];
}
fatCnt++;
}
}
if(tmp != )
{
factor[fatCnt][] = tmp;
factor[fatCnt++][] = ;
}
return fatCnt;
}
int euler[];
void getEuler()
{
memset(euler,,sizeof(euler));
euler[] = ;
for(int i = ;i <= ;i++)
if(!euler[i])
for(int j = i; j <= ;j += i)
{
if(!euler[j])
euler[j] = j;
euler[j] = euler[j]/i*(i-);
}
}
int calc(int n,int m)//n < m,求1-n内和m互质的数的个数
{
getFactors(m);
int ans = ;
for(int i = ;i < (<<fatCnt);i++)
{
int cnt = ;
int tmp = ;
for(int j = ;j < fatCnt;j++)
if(i&(<<j))
{
cnt++;
tmp *= factor[j][];
}
if(cnt&)ans += n/tmp;
else ans -= n/tmp;
}
return n - ans;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
getPrime();
int a,b,c,d;
int T;
int k;
scanf("%d",&T);
int iCase = ;
getEuler();
while(T--)
{
iCase++;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(k == || k > b || k > d)
{
printf("Case %d: 0\n",iCase);
continue;
}
if(b > d)swap(b,d);
b /= k;
d /= k;
long long ans = ;
for(int i = ;i <= b;i++)
ans += euler[i];
for(int i = b+;i <= d;i++)
ans += calc(b,i);
printf("Case %d: %I64d\n",iCase,ans);
} return ;
}
HDU 1695 GCD (欧拉函数+容斥原理)的更多相关文章
- hdu 1695 GCD (欧拉函数+容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1695 GCD 欧拉函数+容斥原理+质因数分解
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...
- HDU 1695 GCD (欧拉函数,容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HDU 1695 GCD 欧拉函数+容斥定理
输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和 ...
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 1695 GCD 欧拉函数 + 容斥
http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K] 和 [L ...
- HDU 2588 GCD (欧拉函数)
GCD Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status De ...
- [hdu1695] GCD ——欧拉函数+容斥原理
题目 给定两个区间[1, b], [1, d],统计数对的个数(x, y)满足: \(x \in [1, b]\), \(y \in [1, d]\) ; \(gcd(x, y) = k\) HDU1 ...
- HDU 1695 GCD(欧拉函数+容斥原理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...
随机推荐
- 别误用IsDigit与IsNumber函数
1.起因 最近发现程序中有一段控制TextBox数字输入的代码,相信大家都不会太陌生,如下: void int_KeyPress(object sender, KeyPressEventArgs e) ...
- spring报nested exception is java.lang.IllegalArgumentException: @EnableAsync annotation metadata was not injected错误
http://www.oschina.net/question/1539472_159699
- Treap树的基础知识
原文 其它较好的的介绍:堆排序 AVL树 树堆,在数据结构中也称Treap(事实上在国内OI界常称为Traep,与之同理的还有"Tarjan神犇发明的"Spaly),是指有一个随 ...
- MyKTV项目总结
今天和大伙分享一下我的KTV系统,我想大家都有自己独特的魅力,都有自己的风采,都有自己骄傲的一部分. 在这里我就抛砖引玉,聊聊我的KTV项目,希望大家能给出自己的建议.. 首先,我们先了解一下:当我们 ...
- Linux FTP配置文件说明
一.vsftpd说明: LINUX下实现FTP服务的软件很多,最常见的有vsftpd,Wu-ftpd和Proftp等.Red Hat Enterprise Linux中默认安装的是vsftpd. 访问 ...
- 【原创】.NET Core应用类型(Portable apps & Self-contained apps)
介绍 有许多种方式可以用来考虑构建应用的类型,通常类型用来描述一个特定的执行模型或者基于此的应用.举例说:控制台应用(Console Application).Web应用(Web Applicatio ...
- sqlserver 死锁原因及解决方法
其实所有的死锁最深层的原因就是一个:资源竞争 表现一: 一个用户A 访问表A(锁住了表A),然后又访问表B,另一个用户B 访问表B(锁住了表B),然后企图访问表A,这时用户A由于用户B已经锁住表B,它 ...
- Office版本问题0x80029C4A
说来奇怪,以前运行正常的程序(涉及excel表格输出),现在运行失败了,一调试,发现了如下问题: 无法将类型为"Microsoft.Office.Interop.Excel.Applicat ...
- 实验12:Problem F: 求平均年龄
Home Web Board ProblemSet Standing Status Statistics Problem F: 求平均年龄 Problem F: 求平均年龄 Time Limit: ...
- 独立博客开张!有关读书、GTD和IT方面的内容将发布在新网站上
2015年自己建个独立博客http://www.shenlongbin.com,以后与读书.GTD和IT技术有关的主题都放在个人博客中,2015年计划基本制定,请移步到这里. 感谢博客园提供了如此优秀 ...