/*
1
126 223092870
210 330 390 462 510 546 570 690 714 770 798 858 910 966 1122 1155 1190 1254 1326 1330 1365 1430 1482 1518 1610 1785 1794 1870 1938 1995 2002 2090 2145 2210 2346 2415 2470 2530 2618 2622 2805 2926 2990 3003 3094 3135 3230 3315 3458 3542 3705 3795 3910 3927 4186 4370 4389 4485 4522 4641 4845 4862 5005 5187 5313 5434 5474 5865 6118 6279 6545 6555 6578 6783 7106 7293 7315 7735 8151 8211 8398 8602 8645 8855 9177 9614 9867 10166 10465 10659 11305 11362 12155 12597 12903 13585 13685 14421 14858 15249 15295 16445 17017 17043 17765 19019 20995 21505 22287 23023 24035 24871 25415 28405 29393 30107 33649 35581 37145 39767 46189 52003 55913 62491 81719 96577
*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
#define N 10000
#define LL long long
#define U unsigned
using namespace std;
int cas=,T;
int n,m,a[N+],d[N+],dn,v[N+],c[N+];
int gcd(int a,int b)
{
return b==?a:gcd(b,a%b);
}
void fac()
{
dn=;
int x=sqrt(m)+0.5;
for(int i=;i<=x;i++)
{
if(m%i==)
{
d[dn++]=i;
d[dn++]=m/i;
}
}
sort(d,d+dn);
}
int main()
{
//freopen("1.in","w",stdout);
//freopen("1.in","r",stdin);
//freopen("out1","w",stdout);
scanf("%d",&T);
while(T--)
{
memset(v,,sizeof(v));//要走的次数
memset(c,,sizeof(c));//已经走的次数
scanf("%d%d",&n,&m);
set<int>g;
for(int i=;i<n;i++) { scanf("%d",a+i);g.insert(gcd(m,a[i]%m)); }
fac();
for(set<int>::iterator it=g.begin();it!=g.end();it++) //初始化,*it的倍数约数都要走一次
{
for(int j=;j<dn;j++) if(d[j]% *it==) v[j]=;
}
LL ans=;
//for(int i=0;i<dn;i++) printf("%d ",d[i]);printf("\n");
for(int i=;i<dn;i++)
{
if(v[i])
{
int dif=v[i]-c[i]; //d[i]本来要走的次数-已经走了的次数=现在要走的次数
ans+=(LL)m*(m/d[i]-)/*dif;
for(int j=i+;j<dn;j++) if(d[j]%d[i]==) c[j]+=dif;//d[i]走过之后d[i]的倍数也已经走了dif次,所以c[j]+=dif
}
}
printf("Case #%d: %lld\n",cas++,ans);
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return ;
}

hdu5514 非2的次幂容斥原理的更多相关文章

  1. Gym - 100625F Count Ways 快速幂+容斥原理

    题意:n*m的格子,中间有若干点不能走,问从左上角到右下角有多少种走法. 思路:CountWay(i,j) 表示从 i 点到 j 点的种数.然后用容斥原理加加减减解决 #pragma comment( ...

  2. 快速幂 递归&&非递归 模板

    一.递归版快速幂 inline int qpow(int x,int y,int p){ if(y==0) return 1; int z=qpow(x,y>>1,p); z=1ll*z* ...

  3. 深入浅出HTTP协议(WEB开发和面试必备)

    1. 基础概念篇   a.简介 HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.它的发展是万维网协会(World Wide Web Consortium)和 ...

  4. 深入理解http/https协议

    深入理解HTTP协议(转) http协议学习系列 1. 基础概念篇 1.1 介绍 HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.它的发展是万维网协会(Wo ...

  5. HTTP请求 GET与POST是怎么实现?

    1.HTTP请求格式: <request line> <headers> <blank line> [<request-body>] 在HTTP请求中, ...

  6. http中get和post的区别

    HTTP定义了与服务器交互的不同方法,最基本的方法是 GET 和 POST. HTTP-GET和HTTP-POST是使用HTTP的标准协议动词,用于编码和传送变量名/变量值对参数,并且使用相关的请求语 ...

  7. 转载和积累系列 - 深入理解HTTP协议

    深入理解HTTP协议 1. 基础概念篇 1.1 介绍 HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.它的发展是万维网协会(World Wide Web C ...

  8. http get vs post

    http get vs post GET与POST方法有以下区别:(1) 在客户端,Get方式在通过URL提交数据,数据在URL中可以看到:POST方式,数据放置在HTML HEADER内提交.(2) ...

  9. Http请求中POST与GET的区别——前端面试

    一.原理区别 Http定义了与服务器交互的方法,其中最基本的四种是:GET,POST,PUT,DELETE,正对应着对资源的查,改,增,删.URL的全称是资源描述符,我们可以这样认为,一个URL地址, ...

随机推荐

  1. 关于ABP——领域服务的思考

    我在刚接触ABP的时候一直有一个疑问--有了应用服务,为什么还需要领域服务呢? 领域服务和应用服务对比 领域服务 应用服务 返回值 Entity DTO 被表现层调用 不可以(非强制) 可以 在ABP ...

  2. Windows平台下安装Eclipse插件,开发Hadoop应用

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  3. 关于C/C++中的“auto”

    C/C++ 98标准 C++03标准 意思完全一样:auto被解释为一个自动存储变量的关键字,也就是申明一块临时的变量内存.auto的出现意味着,当前变量的作用域为当前函数或代码段的局部变量,意味着当 ...

  4. js判断移动端与pc端

    这里介绍下使用device.js插件来判断移动端设备 地址:https://github.com/matthewhudson/device.js 示例: if(device.mobile()){ wi ...

  5. Java Web学习笔记--JSP for循环

    JSP for循环 <%@ page language="java" contentType="text/html; charset=UTF-8" %&g ...

  6. 读《不要告诉我你懂margin(海玉的博客)》有感

    原文来自海玉的博客:http://www.hicss.net/do-not-tell-me-you-understand-margin/ [个人想法] 1."这个问题发生的原因是根据规范,一 ...

  7. 为知笔记markdown插件安装

    Wiz.Editor.md 是一个基于 Editor.md 构建的为知笔记 Markdown 插件. 主要特性 多种样式主题 支持实时预览 支持代码高亮 支持搜索替换 支持ToC目录 Tex数学公式 ...

  8. console.dir()和console.log()的区别

    console.log()可以取代alert()或document.write(),在网页脚本中使用console.log(data)时,会在控制台打印出数据. console.dir()可以显示一个 ...

  9. JS函数调用

        function SayHello(word) { console.log(word); }   function execute(Somefunction,value) { Somefunc ...

  10. php7.0 和 php7.1新特性

    PHP7.1 新特性 1.可为空(Nullable)类型 类型现在允许为空,当启用这个特性时,传入的参数或者函数返回的结果要么是给定的类型,要么是 null .可以通过在类型前面加上一个问号来使之成为 ...