Number Puzzle
Number Puzzle
Time Limit: 2 Seconds Memory Limit: 65536 KB
Given a list of integers (A1, A2, ..., An), and a positive integer M, please find the number of positive integers that are not greater than M and dividable by any integer from the given list.
Input
The input contains several test cases.
For each test case, there are two lines. The first line contains N (1 <= N <= 10) and M (1 <= M <= 200000000), and the second line contains A1, A2, ..., An(1 <= Ai <= 10, for i = 1, 2, ..., N).
Output
For each test case in the input, output the result in a single line.
Sample Input
3 2
2 3 7
3 6
2 3 7
Sample Output
1
4
分析:1~m中至少能被列表里其中一个数整除的数的个数;
容斥即可,注意列表中的数不一定互质,所以要求lcm;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+;
using namespace std;
inline ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,fac[];
int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
rep(i,,n-)scanf("%d",&fac[i]);
int ret=;
rep(i,,(<<n)-)
{
int now=,cnt=;
rep(j,,n-)
{
if(i&(<<j))
{
cnt++;
now=now*fac[j]/gcd(now,fac[j]);
}
}
if(cnt&)ret+=m/now;
else ret-=m/now;
}
printf("%d\n",ret);
}
return ;
}
Number Puzzle的更多相关文章
- [ZOJ 2836] Number Puzzle
Number Puzzle Time Limit: 2 Seconds Memory Limit: 65536 KB Given a list of integers (A1, A2, .. ...
- ACM HDU 1755 -- A Number Puzzle
A Number Puzzle Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1755 A Number Puzzle
这题枚举k节省时间 ;}
- HDOJ 1755 - A Number Puzzle 排列数字凑同余,状态压缩DP
dp [ x ] [ y ] [ z ] 表示二进制y所表示的组合对应的之和mod x余数为z的最小数... 如可用的数字为 1 2 3 4...那么 dp [ 7 ] [ 15 ] [ 2 ] = ...
- [全排列]--A Number Puzzle
标签: ACM Lele 最近上课的时候都很无聊,所以他发明了一个数字游戏来打发时间. 这个游戏是这样的,首先,他拿出几张纸片,分别写上0到9之间的任意数字(可重复写某个数字),然后,他叫同学随便写两 ...
- ZOJ 2836 Number Puzzle 题解
题面 lcm(x,y)=xy/gcd(x,y) lcm(x1,x2,···,xn)=lcm(lcm(x1,x2,···,xn-1),xn) #include <bits/stdc++.h> ...
- uva 227 Puzzle
Puzzle A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained ...
- Uva227.Puzzle
题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 例题 3-5 谜题 uva227 Puzzle
A children’s puzzle that was popular years ago consisted of a × frame which contained small squares ...
随机推荐
- Lucene 4.0 TieredMergePolicy
默认的merge policy. findMerges: 先将全部段依照扣除删除文档之后的字节数(bytesize * (1.0 - delRatio))降序排,对当中size() > 2.5G ...
- Java 获取随机日期
/** * 获取随机日期 * @param beginDate 起始日期 * @param endDate 结束日期 * @return */ public static Date randomDat ...
- 5200 fqy的难题----2的疯狂幂
5200 fqy的难题----2的疯狂幂 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description ...
- Color a Tree HDU - 6241
/* 十分巧妙的二分 题意选最少的点涂色 使得满足输入信息: 1 x的子树涂色数不少于y 2 x的子树外面涂色数不少于y 我们若是把2转化到子树内最多涂色多少 就可以维护这个最小和最大 如果我们二分出 ...
- LuoguP3621 [APIO2007]风铃
https://zybuluo.com/ysner/note/1140124 题面 题面复杂,戳我 解析 看着这道题... 似乎与[HNOI/AHOI2018]道路有不可言妙的相似之处. (题面吓人, ...
- 关于sublime代码格式化
就我接触到的,html,css,js,json,php语言来介绍. html,css,json,js这些,我感觉pretty是比较好用的,ctrl+shift+h快捷键,给人不一样的感受,不过这个插件 ...
- EasyUI TreeGrid 悬浮效果
/* 鼠标悬停扩展*/ (function (hasgrid) { if (hasgrid) { /** * 表格提示 */ $.extend($.fn.datagrid.methods, { too ...
- C - Fafa and his Company
Problem description Fafa owns a company that works on huge projects. There are n employees in Fafa's ...
- net .异步委托知识
以前在编程中,异步用的比较少,导致C# 一些基础的 东西用法都不怎么熟悉,经常要用的时候在去查找资料比较被动,而已没真正里面理解起来,始终感觉不是自己的知识 (题外话) 首先委托关键字 Delega ...
- swift-delegate(代理)或者block传值
1:delegate或者block传值 import UIKit class ViewController: UIViewController,TestDelegatePassValueDelegat ...