再数17
(c17.pas/.c/.cpp)
【问题描述】
之前,小cot发邮件问小cos一个区间所有整数里有多少个“17”,小cos在你的帮助下回答了这个奇怪的问题,然而得到的回信只有“你对了:-)”这几个字……小cos耿耿于怀,始终弄不明白小cot的意图。
终于,小cos决定主动问小cot一些问题。他先写了封邮件,问“17”的含义,回信写道:“我喜欢……17这个数字~”。小cos当然不能接受这样的回答,继续追问她为什么要自己数17,可回信又是简单的一句话:“我喜欢你……数17~”。
小cos急了,狠敲键盘,发了一连串问号过去。几分钟后,对方回道:“想要知道为什么,你再回答我一个问题——
“给你一个有n个正整数的数列{an}。一个正整数x若满足在数列{an}中存在一个正整数ai,使x≡17(mod ai),那么x就是一个‘cost数’。请问1到m的正整数中,有多少个‘cost数’?”
小cos看得一头雾水,只好又来请教你……
【输入】
输入文件名为c17.in,共2行。
第一行两个正整数n和m,意义见问题描述。
第二行n个正整数,分别为数列{an}中的n个数。
【输出】
输出文件名为c17.out。
输出一个整数,表示1到m中“cost数”的个数。
【输入输出样例】
c17.in
3 100
18 22 23
c17.out
11
【数据范围】
对于20%的数据,有n≤20,m≤100,000;
另有40%的数据,n≤3,m<2^31;
对于100%的数据,有n≤30,m<2^31,17<ai<2^31。
(注:“≡”为同余符号,a≡b (mod k) 即a mod k = b mod k)
容斥原理求所有能够整除的数的个数
但是!!!
ai不一定是质数!!!!
这时候要考虑他们的 lcm (CRT也是!!)
注意剪枝! 大的先算!
并且剪枝不可以用位向量生成status!!
应该用dfs
还要特判输出为 0 的情况!!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
typedef long long LL;
const int INF=0x3f3f3f3f;
const int maxn = 35;
int n,m;
int a[maxn];
LL gcd(LL a,LL b)
{
if(b==0) return a;
return gcd(b,a%b);
}
LL lcm(LL a,LL b)
{
return a/gcd(a,b)*b;
}
int ans;
void dfs(int k,int tot,int cur)
{
if(k ==n+1)
{
if(!tot) return;
if(tot&1) ans += (m-17)/cur;
else ans -= (m-17)/cur;
return;
}
dfs(k+1,tot,cur);
LL tmp = lcm((LL)cur,a[k]);
if(tmp <= (LL)(m-17)) dfs(k+1,tot+1,(int)tmp);
}
int main()
{
freopen("c17.in","r",stdin);
freopen("c17.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",a+i);
sort(a+1,a+n+1,greater<int>()); // structure greater <int>
if(a[1] + 17 > m)
{
printf("0");
return 0;
}
dfs(1,0,1);
printf("%d",ans+1);
return 0;
}