题意:求区间1<=i<=b与区间1<=j<=d之间满足gcd(i,j) = k 的数对 (i,j) 个数。(i,j)与(j,i) 算一个。

分析:gcd(i,j)=k可以转化为gcd(i/k,j/k)=1。枚举每个1<=i<=b/k 的 i,用容斥原理统计区间[1,d]中与其互素的个数。需要预处理筛出2~1e5中每个数的质因子。

*注意当k=0时,数对不存在。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const int maxn =1e5+;
  5. vector<int> p[maxn];
  6. bool is[maxn];
  7.  
  8. void pre()
  9. {
  10. for(int i=;i<maxn;i+=) {
  11. p[i].clear();
  12. p[i].push_back();
  13. }
  14. for(int i=;i<maxn;i+=){
  15. if(is[i]) continue;
  16. for(int j=i;j<maxn;j+=i){
  17. is[j] = true;
  18. p[j].push_back(i);
  19. }
  20. }
  21. }
  22.  
  23. int main()
  24. {
  25. #ifndef ONLINE_JUDGE
  26. freopen("in.txt","r",stdin);
  27. freopen("out.txt","w",stdout);
  28. #endif
  29. pre();
  30. int T,cas=; scanf("%d",&T);
  31. while(T--){
  32. int a,b,c,d,k;
  33. scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
  34. if(k==){
  35. printf("Case %d: 0\n",cas++);
  36. continue;
  37. }
  38. b/=k,d/=k;
  39. if(b>d) swap(b,d);
  40. LL ans=;
  41. for(int i=;i<=d;++i){
  42. int tot = min(i,b);
  43. ans += tot;
  44. int up = <<p[i].size(),len = p[i].size();
  45. for(int j=;j<up;++j){
  46. int cnt=,ji=;
  47.  
  48. for(int k=;k<len;++k){
  49. if(j&(<<k)){
  50. cnt++;
  51. ji *=p[i][k];
  52. }
  53. }
  54.  
  55. if(cnt&) ans -= tot/ji;
  56. else ans +=tot/ji;
  57. }
  58. }
  59. printf("Case %d: %lld\n",cas++,ans);
  60. }
  61. return ;
  62. }

HDU - 1695 GCD (容斥+枚举)的更多相关文章

  1. HDU 1695 GCD 容斥

    GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...

  2. 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, ...

  3. hdu 5514 Frogs(容斥)

    Frogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  4. HDU 5213 分块 容斥

    给出n个数,给出m个询问,询问 区间[l,r] [u,v],在两个区间内分别取一个数,两个的和为k的对数数量. $k<=2*N$,$n <= 30000$ 发现可以容斥简化一个询问.一个询 ...

  5. HDU 2588 思维 容斥

    求满足$1<=X<=N ,(X,N)>=M$的个数,其中$N, M (2<=N<=1000000000, 1<=M<=N)$. 首先,假定$(x, n)=m$ ...

  6. hdu 1695 GCD 欧拉函数 + 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K]  和 [L ...

  7. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. 数论 + 容斥 - HDU 1695 GCD

    problem's Link mean 给定五个数a,b,c,d,k,从1~a中选一个数x,1~b中选一个数y,使得gcd(x,y)=k. 求满足条件的pair(x,y)数. analyse 由于b, ...

  9. HDU 1695 GCD(容斥定理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

随机推荐

  1. ifconfig配置网络时,出现“SIOCSIFADDR: No such device”

    最近刚学习linux,参考教学视频,试着使用ifconfig命令来设置网卡参数,命令为“ifconfig eth0 192.168.11.2”. 但结果显示“SIOCSIFADDR: No such ...

  2. 对PHP输入输出流学习和认识

    PHP输入和输出流是通过php://来访问的,它允许访问 PHP 的输入输出流.标准输入输出和错误描述符, 内存中.磁盘备份的临时文件流以及可以操作其他读取写入文件资源的过滤器. php://stdi ...

  3. linux之挂载硬盘

    sudo gedit /etc/fstab中添加 UUID=190534e2-d8ae-4928-94b7-0f4d4209a3ab     /data     ext4     defaults   ...

  4. ubuntu 终端$换行

    编辑~/.bashrc文件 sudo gedit  ~/.bashrc 红色部分为添加的 \n if [ "$color_prompt" = yes ]; then    PS1= ...

  5. 使用StringTokenizer分解字符串

    Java切割字符串.一般使用substring.split.StringTokenizer来处理,前两种是String对象的方法,使用字符串能够直接处理,本文介绍下StringTokenizer的使用 ...

  6. 如何交换a,b的数值——一个简单的问题就证明现在的你依然弱爆了

    How? int c = a; a = b; b = c; 这样会浪费多一点内存去存放c,so还有吗? a ^= b; b ^= a; a ^= b; ^:异或. 答案碉堡了, 不过估计除了有可能在面 ...

  7. oralce函数

    1.trunc函数处理数字和日期TRUNC(NUMBER[,DECIMAL]) 数字格式TRUNC(DATE[,FOMAT]) 日期格式2.round函数(四舍五入)ROUND(NUMBER[,DEC ...

  8. MATLAB使用fft求取给定音频信号的频率

    一段10s立体声音频,采样率位8000Hz,已知频率为1000Hz clc; clear; [data, Fs] = audioread('1khz_stereo_8000.wav'); fs=Fs; ...

  9. src与href的异同

    相同点: 在跨域中,src,href,这些发送的请求都是get请求: 不同点: 1, 概念:href (Hypertext Reference)指定网络资源的位置: 理解:href 用作 " ...

  10. 转载:Eslint 规则说明

    原文: http://blog.csdn.net/helpzp2008/article/details/51507428 ,//禁止使用alert confirm prompt ,//禁止使用数组构造 ...