这题很容易转化到一个容斥计数问题。而用指数复杂度的枚举计数法显然会挂,只能考虑别的方法。

首先将a[i]用gcd(a[i], m)替换,排序去重后得到一组m的约数,而m不超过1e9,因此m的所有约数最多在1000左右。

假设数组a只含有2,3两个元素,那么显然答案应该是f(2) + f(3) - f(6),f(i)表示只有i的情况下的答案。

2和3的系数是1,6的系数是-1,其余系数为0。

根据容斥原理:对于某个特定的约数,若存在数组a的一个子集其lcm为该约数,那么若集合元素个数为偶数,贡献-1,若为奇数,贡献1。

而最终的答案一定由所有约数合成,其系数取值{-1,0,1}。

因此我们考虑数组a的前m个数,其对应的系数分布是可以确定的。

加入第m+1个数时,显然新的集合必然由原集合的子集并上新元素得到,分别考虑m的所有约数k,其对应的系数为op(k)

若op(k)为-1,则说明偶数子集的个数比奇数子集的个数多1,若我们添如新元素x,则恰好使得奇数子集个数比偶数多1,但此时贡献

不是对k的,而是对lcm(k, x),用公式表示就是:

op(lcm(k, x)) += op(k) * (-1).

其余情形类似。

最终答案是ans = sigma(op(i) * f(i)), i | m.

这样我们只需最多进行1000次遍历就可使得元素个数加一,总复杂度约为O(1e6)。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <ctime>
#include <cassert>
#define lson (u << 1)
#define rson (u << 1 | 1)
#define cls(i, j) memset(i, j, sizeof i)
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef __int64 ll;
const double eps = 1e-;
const double pi = acos(-1.0);
const int maxn = 1e4 + ;
const int maxm = 4e4 + ;
const int inf = 0x3f3f3f3f;
const ll linf = 0x3fffffffffffffff;
const ll mod = 1e9 + ; map<ll, int> mapi;
int n;
ll a[maxn], m;
int prime[maxm], k;
bool vis[maxm];
ll fact[maxn], nf;
int cnt[maxn];
ll table[maxn], nt;
int op[maxn];
int op1[maxn];
ll ans; ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); } void init(){
cls(vis, );
k = ;
for(int i = ; i < maxm; i++){
if(vis[i]) continue;
prime[k++] = i;
for(int j = i * ; j < maxm; j += i) vis[j] = ;
}
} void dfs(int next, ll num){
if(next >= nf){
table[nt++] = num;
mapi[num] = nt - ;
return;
}
ll tem = num;
for(int i = ; i <= cnt[next]; i++){
dfs(next + , tem);
tem *= fact[next];
}
} ll f(ll num){
ll tem = (m - ) / num;
return tem * (tem + ) / * num;
} void factorize(){
nf = ;
ll m1 = m;
int mid = (int)sqrt(m);
for(int i = ; prime[i] <= mid; i++){
if(m % prime[i]) continue;
fact[nf++] = prime[i];
cnt[nf - ] = ;
while(m % prime[i] == ) ++cnt[nf - ], m /= prime[i];
mid = (int)sqrt(m);
}
if(m != ) fact[nf++] = m, cnt[nf - ] = ;
m = m1;
} ll solve(){
mapi.clear();
int n1 = ;
for(int i = ; i < n; i++){
ll tem = a[i] % m;
if(!tem) continue;
a[n1++] = gcd(m, tem);
}
n = n1;
sort(a, a + n);
n = unique(a, a + n) - a;
cls(vis, );
for(int i = ; i < n; i++){
for(int j = i + ; j < n; j++){
if(a[i] % a[j] == ) vis[i] = ;
else if(a[j] % a[i] == ) vis[j] = ;
}
}
n1 = ;
for(int i = ; i < n; i++) if(!vis[i]) a[n1++] = a[i];
n = n1;
sort(a, a + n);
factorize();
nt = ;
dfs(, );
cls(op, );
ans = ;
for(int i = ; i < n; i++){
cls(op1, );
for(int j = ; j < nt; j++){
ll tem = a[i] / gcd(a[i], table[j]) * table[j];
if(tem >= m) continue;
op1[mapi[tem]] += -op[j];
}
++op1[mapi[a[i]]];
for(int j = ; j < nt; j++) op[j] += op1[j];
}
// for(int i = 0; i < nt; i++) printf("%d ", op[i]);
// puts("");
for(int i = ; i < nt; i++) ans += op[i] * f(table[i]);
return ans;
} int main(){
// freopen("in.txt", "r", stdin);
int T, kase = ;
init();
scanf("%d", &T);
while(T--){
scanf("%d%I64d", &n, &m);
for(int i = ; i < n; i++) scanf("%I64d", &a[i]);
ll ans = solve();
printf("Case #%d: %I64d\n", ++kase, ans);
}
return ;
}

hdu5514Frogs(2015ACM-ICPC沈阳赛区F题)的更多相关文章

  1. 2015ACM/ICPC亚洲区长春站 F hdu 5533 Almost Sorted Array

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  2. HDU 5901 Count primes (1e11内的素数个数) -2016 ICPC沈阳赛区网络赛

    题目链接 题意:求[1,n]有多少个素数,1<=n<=10^11.时限为6000ms. 官方题解:一个模板题, 具体方法参考wiki或者Four Divisors. 题解:给出两种代码. ...

  3. hdu 4461 第37届ACM/ICPC杭州赛区I题

    题意:给两个人一些棋子,每个棋子有其对应的power,若b没有或者c没有,或者二者都没有,那么他的total power就会减1,total power最少是1,求最后谁能赢 如果b或c出现的话,fl ...

  4. zoj 3662 第37届ACM/ICPC长春赛区H题(DP)

    题目:给出K个数,使得这K个数的和为N,LCM为M,问有多少种 f[i][j][k]表示选i个数,总和为j,最小公倍数为k memery卡的比较紧,注意不要开太大,按照题目数据开 这种类型的dp也是第 ...

  5. hdu 4463 第37届ACM/ICPC杭州赛区K题 最小生成树

    题意:给坐标系上的一些点,其中有两个点已经连了一条边,求最小生成树的值 将已连接的两点权值置为0,这样一定能加入最小生成树里 最后的结果加上这两点的距离即为所求 #include<cstdio& ...

  6. 2015 ICPC 沈阳站M题

    M - Meeting Time Limit:6000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit ...

  7. HDU 5532 / 2015ACM/ICPC亚洲区长春站 F.Almost Sorted Array

    Almost Sorted Array Problem Description We are all familiar with sorting algorithms: quick sort, mer ...

  8. 2016 ICPC大连站---F题 Detachment

    题意:输入一个x,将x拆分成一些小的数(这些数不能相同,即x=a1+a2+......   ai!=aj when i!=j),然后这些数相乘得到一个成积(s=a1*a2*......),求最大的乘积 ...

  9. HDU 5894 hannnnah_j’s Biological Test (组合数学) -2016 ICPC沈阳赛区网络赛

    题目链接 #include <map> #include <queue> #include <math.h> #include <stdio.h> #i ...

随机推荐

  1. linux:/etc/rc.local 不能自动启动问题

    前段时间安装LNMP环境,配置/etc/rc.local的时候配置了启动mysql.nginx.php以及关闭防火墙,可结果重启了七八次还是自启动不了后来终于找到原因了 看下图: /etc/rc.lo ...

  2. linux:磁碟与档案系统管理

    档案系统特性:为什么磁碟分割完需要格式化(format)才能使用吗? 答:因为每种作业系统所设定的档案属性和权限并不相同,为了存放这些档案所需的资料(所以需要格式化成作业系统能够利用的档案系统格式fi ...

  3. Leetcode: Evaluate Division

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  4. Leetcode: Longest Substring with At Least K Repeating Characters

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  5. JS练习题 显示登入者相关好友

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. ofbiz进击 第二节。 control 理解与创建

    首先要说的是,学习ofbiz,要去http://ofbiz.apache.org/官网里面,去看右边菜单里   Management Apps  的例子,然后找到类似的页面,去看调用的源码方法. co ...

  7. javascript 一些常用的验证

    只能输入数字          onkeyup="this.value=this.value.replace(/[^\d]/g,'')" onafterpaste="th ...

  8. CSS_03_01_CSS组合选择器

    CSS组合选择器 第01步:创建css:with.css @charset "utf-8"; /* 组合选择器,用","隔开 */ .a,.b,div span ...

  9. git 命令--上传代码

    创建密钥命令: ssh-keygen -C 'your@email.address' -t rsa 找到生成的密钥文件id_rsa.pub 地址:C:\Documents and Settings\A ...

  10. 夺命雷公狗---node.js---4net模块(上)

    node.js为我们提供了一个net模块,主要是为了提供了一些底层通信的小工具,包含了创建服务器/客户端方法,引入方式也很简单: var net = require('net'); net模块也为我们 ...