HDU5514 Frogs
- /*
- HDU5514 Frogs
- http://acm.hdu.edu.cn/showproblem.php?pid=5514
- 容斥原理
- *
- *
- */
- #include <cstdio>
- #include <cmath>
- #include <algorithm>
- //#define test
- using namespace std;
- const long long Nmax=1e5;
- long long n,m,a[Nmax];
- long long book[Nmax];
- long long p[Nmax];
- int cnt;
- long long num[Nmax];
- long long gcd(long long a,long long b)
- {
- if(b==0LL)
- return a;
- return gcd(b,a%b);
- }
- int main()
- {
- long long t;
- #ifdef test
- while()
- {
- long long a,b;
- scanf("%lld%lld",&a,&b);
- printf("%lld\n",gcd(a,b));
- }
- #endif
- scanf("%lld",&t);
- for(long long ttt=;ttt<=t;ttt++)
- {
- scanf("%lld%lld",&n,&m);
- int flag=;
- for(long long i=;i<=n;i++)
- {
- scanf("%lld",&a[i]);
- a[i]=gcd(a[i],m);
- if(a[i]==)
- flag=;
- }
- if(flag)
- {
- long long ans=(m-1LL)*m/2LL;
- printf("Case #%lld: ",ttt);
- printf("%lld\n",ans);
- continue;
- }
- long long ans=0LL;
- cnt=;
- for(int i=;i*i<=m;i++)
- {
- if(m%i)
- continue;
- p[++cnt]=i;
- if(i*i!=m)
- p[++cnt]=m/i;
- }
- sort(p+,p++cnt);
- for(int i=;i<=cnt;i++)
- book[i]=num[i]=;
- for(int i=;i<=n;i++)
- {
- for(int j=;j<=cnt;j++)
- if(p[j]%a[i]==)
- book[j]=;
- }
- for(int i=;i<=cnt;i++)
- {
- if(book[i]!=num[i])
- {
- long long tmp=m/p[i];
- ans+=tmp*(tmp-1LL)/2LL*p[i]*(book[i]-num[i]);
- tmp=book[i]-num[i];
- for(int j=i+;j<=cnt;j++)
- if(p[j]%p[i]==)
- num[j]+=tmp;
- }
- }
- printf("Case #%lld: ",ttt);
- printf("%lld\n",ans);
- }
- return ;
- }
HDU5514 Frogs的更多相关文章
- 从HDU2588:GCD 到 HDU5514:Frogs (欧拉公式)
The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the ...
- 【做题】hdu5514 Frogs——另类容斥
题意是给出n个m的约数,问[0,m-1]中至少被其中一个约数整除的整数和.(n<=10000,m<=1000000000) 直接容斥的话,是2^n再拖个log的复杂度,加上当前的数大于m时 ...
- POJ 1659 Frogs' Neighborhood(Havel-Hakimi定理)
题目链接: 传送门 Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Description 未名湖附近共有N个大小湖泊L ...
- 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 ...
- Frogs' Neighborhood
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 7920 Accepted: 33 ...
- HDU 5514 Frogs 容斥定理
Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...
- poj 1659 Frogs' Neighborhood (DFS)
http://poj.org/problem?id=1659 Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total S ...
- poj 1659 Frogs' Neighborhood (贪心 + 判断度数序列是否可图)
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 6076 Accepted: 26 ...
- codeforces 609F. Frogs and mosquitoes 二分+线段树
题目链接 F. Frogs and mosquitoes time limit per test 2 seconds memory limit per test 512 megabytes input ...
随机推荐
- OC中使用UI自己定义控件实现计算器的设计(版本号1简单的加减乘除,连加,连减,连除,连乘)
OC中使用UI自己定义控件实现计算器的设计(版本号1简单的加减乘除,连加.连减,连除,连乘) #import <UIKit/UIKit.h> @interface ViewControll ...
- ES 断路器——本质上保护OOM提前抛出异常而已
监控fielddata使用了多少内存以及是否有数据被驱逐是非常重要的.大量的数据被驱逐会导致严重的资源问题以及不好的性能. Fielddata使用可以通过下面的方式来监控: 对于单个索引使用 {ref ...
- Mysql慢查询和慢查询日志分析利器–mysqlsla
1.安装mysqlsla Source code wget http://hackmysql.com/scripts/mysqlsla-2.03.tar.gz tar zvxf mysqlsl ...
- php保存远程图片
php获取远程图片并把它保存到本地 来源: 时间:2013-09-05 19:26:57 阅读数:45006 分享到:1 [导读] 在php中我们经常使用写一些简单的采集功能,这样可以自动把远 ...
- Queue 与List、LinkedList与 ArrayList 区别
List 是一个接口,不能实例化,通过实例化ArrayList 或者LinkedList来调用:List list = new ArrayList(); |--List: 元素是有序的(怎么存的就怎么 ...
- Hadoop MapReduce编程 API入门系列之自定义多种输入格式数据类型和排序多种输出格式(十一)
推荐 MapReduce分析明星微博数据 http://git.oschina.net/ljc520313/codeexample/tree/master/bigdata/hadoop/mapredu ...
- Several Ideas on Perl List Context
According to Beginning Perl Book published by Tsinghua Pub., the list context appears when you are t ...
- arttemplate.js原生写法案例
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- mybatis 高级映射和spring整合之查询缓存(5)
mybatis 高级映射和spring整合之查询缓存(5) 2.0 查询缓存 2.0.1 什么是查询缓存 mybatis提供缓存,用于减轻数据压力,提高数据库性能. mybatis提供一级缓存和二级缓 ...
- ComboBoxEdit 添加键值
ComboBoxEdit combo = new ComboBoxEdit(); var coll = combo.Properties.Items; coll.BeginUpdate(); try ...