1. /*
  2. HDU5514 Frogs
  3. http://acm.hdu.edu.cn/showproblem.php?pid=5514
  4. 容斥原理
  5. *
  6. *
  7. */
  8. #include <cstdio>
  9. #include <cmath>
  10. #include <algorithm>
  11. //#define test
  12. using namespace std;
  13. const long long Nmax=1e5;
  14. long long n,m,a[Nmax];
  15. long long book[Nmax];
  16. long long p[Nmax];
  17. int cnt;
  18. long long num[Nmax];
  19. long long gcd(long long a,long long b)
  20. {
  21. if(b==0LL)
  22. return a;
  23. return gcd(b,a%b);
  24. }
  25.  
  26. int main()
  27. {
  28. long long t;
  29. #ifdef test
  30. while()
  31. {
  32. long long a,b;
  33. scanf("%lld%lld",&a,&b);
  34. printf("%lld\n",gcd(a,b));
  35. }
  36. #endif
  37. scanf("%lld",&t);
  38. for(long long ttt=;ttt<=t;ttt++)
  39. {
  40. scanf("%lld%lld",&n,&m);
  41. int flag=;
  42. for(long long i=;i<=n;i++)
  43. {
  44. scanf("%lld",&a[i]);
  45. a[i]=gcd(a[i],m);
  46. if(a[i]==)
  47. flag=;
  48. }
  49. if(flag)
  50. {
  51. long long ans=(m-1LL)*m/2LL;
  52. printf("Case #%lld: ",ttt);
  53. printf("%lld\n",ans);
  54. continue;
  55. }
  56. long long ans=0LL;
  57. cnt=;
  58. for(int i=;i*i<=m;i++)
  59. {
  60. if(m%i)
  61. continue;
  62. p[++cnt]=i;
  63. if(i*i!=m)
  64. p[++cnt]=m/i;
  65. }
  66. sort(p+,p++cnt);
  67. for(int i=;i<=cnt;i++)
  68. book[i]=num[i]=;
  69. for(int i=;i<=n;i++)
  70. {
  71. for(int j=;j<=cnt;j++)
  72. if(p[j]%a[i]==)
  73. book[j]=;
  74. }
  75. for(int i=;i<=cnt;i++)
  76. {
  77. if(book[i]!=num[i])
  78. {
  79. long long tmp=m/p[i];
  80. ans+=tmp*(tmp-1LL)/2LL*p[i]*(book[i]-num[i]);
  81. tmp=book[i]-num[i];
  82. for(int j=i+;j<=cnt;j++)
  83. if(p[j]%p[i]==)
  84. num[j]+=tmp;
  85. }
  86. }
  87.  
  88. printf("Case #%lld: ",ttt);
  89. printf("%lld\n",ans);
  90. }
  91. return ;
  92. }

HDU5514 Frogs的更多相关文章

  1. 从HDU2588:GCD 到 HDU5514:Frogs (欧拉公式)

    The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the ...

  2. 【做题】hdu5514 Frogs——另类容斥

    题意是给出n个m的约数,问[0,m-1]中至少被其中一个约数整除的整数和.(n<=10000,m<=1000000000) 直接容斥的话,是2^n再拖个log的复杂度,加上当前的数大于m时 ...

  3. POJ 1659 Frogs' Neighborhood(Havel-Hakimi定理)

    题目链接: 传送门 Frogs' Neighborhood Time Limit: 5000MS     Memory Limit: 10000K Description 未名湖附近共有N个大小湖泊L ...

  4. CF# Educational Codeforces Round 3 F. Frogs and mosquitoes

    F. Frogs and mosquitoes time limit per test 2 seconds memory limit per test 512 megabytes input stan ...

  5. Frogs' Neighborhood

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 7920   Accepted: 33 ...

  6. HDU 5514 Frogs 容斥定理

    Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...

  7. poj 1659 Frogs' Neighborhood (DFS)

    http://poj.org/problem?id=1659 Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total S ...

  8. poj 1659 Frogs' Neighborhood (贪心 + 判断度数序列是否可图)

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6076   Accepted: 26 ...

  9. codeforces 609F. Frogs and mosquitoes 二分+线段树

    题目链接 F. Frogs and mosquitoes time limit per test 2 seconds memory limit per test 512 megabytes input ...

随机推荐

  1. OC中使用UI自己定义控件实现计算器的设计(版本号1简单的加减乘除,连加,连减,连除,连乘)

    OC中使用UI自己定义控件实现计算器的设计(版本号1简单的加减乘除,连加.连减,连除,连乘) #import <UIKit/UIKit.h> @interface ViewControll ...

  2. ES 断路器——本质上保护OOM提前抛出异常而已

    监控fielddata使用了多少内存以及是否有数据被驱逐是非常重要的.大量的数据被驱逐会导致严重的资源问题以及不好的性能. Fielddata使用可以通过下面的方式来监控: 对于单个索引使用 {ref ...

  3. Mysql慢查询和慢查询日志分析利器&ndash;mysqlsla

    1.安装mysqlsla Source code     wget http://hackmysql.com/scripts/mysqlsla-2.03.tar.gz tar zvxf mysqlsl ...

  4. php保存远程图片

    php获取远程图片并把它保存到本地 来源:   时间:2013-09-05 19:26:57   阅读数:45006 分享到:1 [导读] 在php中我们经常使用写一些简单的采集功能,这样可以自动把远 ...

  5. Queue 与List、LinkedList与 ArrayList 区别

    List 是一个接口,不能实例化,通过实例化ArrayList 或者LinkedList来调用:List list = new ArrayList(); |--List: 元素是有序的(怎么存的就怎么 ...

  6. Hadoop MapReduce编程 API入门系列之自定义多种输入格式数据类型和排序多种输出格式(十一)

    推荐 MapReduce分析明星微博数据 http://git.oschina.net/ljc520313/codeexample/tree/master/bigdata/hadoop/mapredu ...

  7. Several Ideas on Perl List Context

    According to Beginning Perl Book published by Tsinghua Pub., the list context appears when you are t ...

  8. arttemplate.js原生写法案例

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. mybatis 高级映射和spring整合之查询缓存(5)

    mybatis 高级映射和spring整合之查询缓存(5) 2.0 查询缓存 2.0.1 什么是查询缓存 mybatis提供缓存,用于减轻数据压力,提高数据库性能. mybatis提供一级缓存和二级缓 ...

  10. ComboBoxEdit 添加键值

    ComboBoxEdit combo = new ComboBoxEdit(); var coll = combo.Properties.Items; coll.BeginUpdate(); try ...