[ZOJ 2836] 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
入门容斥原理,见代码
#include <iostream>
#include <cstdio>
using namespace std;
#define ll long long
#define N 10 int n,m;
int a[N];
int gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
int lcm(ll a,ll b){
return a/gcd(a,b)*b;
}
////写法1/////
int solve()
{
int res=;
for(int i=;i<(<<n);i++)
{
int cnt=;
ll LCM=;
for(int j=;j<n;j++)
{
if(i&(<<j)) cnt++,LCM=lcm(LCM,a[j]);
}
if(cnt&) res+=m/LCM;
else res-=m/LCM;
}
return res;
}
////写法2/////
int ans;
void dfs(int pos,int LCM,int cnt)
{
if(cnt)
{
if(cnt&) ans+=m/LCM;
else ans-=m/LCM;
}
for(int i=pos+;i<n;i++)
dfs(i,lcm(LCM,a[i]),cnt+);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<n;i++) scanf("%d",&a[i]);
//printf("%d\n",solve());
ans=;
dfs(-,,);
printf("%d\n",ans);
}
return ;
}
[ZOJ 2836] Number Puzzle的更多相关文章
- 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> ...
- ACM HDU 1755 -- A Number Puzzle
A Number Puzzle Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Number Puzzle
Number Puzzle Time Limit: 2 Seconds Memory Limit: 65536 KB Given a list of integers (A1, A2, .. ...
- ZOJ 3435 Ideal Puzzle Bobble
ZOJ Problem Set - 3435 Ideal Puzzle Bobble Time Limit: 2 Seconds Memory Limit: 65536 KB Have yo ...
- [ZOJ]3541 Last Puzzle (区间DP)
ZOJ 3541 题目大意:有n个按钮,第i个按钮在按下ti 时间后回自动弹起,每个开关的位置是di,问什么策略按开关可以使所有的开关同时处于按下状态 Description There is one ...
- ZOJ 1602 Multiplication Puzzle(区间DP)题解
题意:n个数字的串,每取出一个数字的代价为该数字和左右的乘积(1.n不能取),问最小代价 思路:dp[i][j]表示把i~j取到只剩 i.j 的最小代价. 代码: #include<set> ...
- ZOJ 3908 Number Game ZOJ Monthly, October 2015 - F
Number Game Time Limit: 2 Seconds Memory Limit: 65536 KB The bored Bob is playing a number game ...
- zoj 2836 容斥原理
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2836 #include <cstdio> #incl ...
- zoj Beautiful Number(打表)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2829 题目描述: Mike is very lucky, as ...
随机推荐
- 采集/自动登录啊都可以用这两个方法实现 asp.net
/// <summary> /// 通过get方式发送xmlHttp请求,并获得响应数据 /// </summary> /// <param name="Url ...
- oracle分页与rownum
Oracle分页(limit方式的运用) Oracle不支持类似于 MySQL 中的 limit. 但你还是可以rownum来限制返回的结果集的行数. 第一种 select * from a_matr ...
- 种子填充找连通块 floodfill
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- MVC学习系列——参考
C#进阶系列——WebApi接口传参不再困惑:传参详解 http://www.cnblogs.com/landeanfen/p/5337072.html
- Xcode文档下载与安装路径
https://developer.apple.com/library/downloads/docset-index.dvtdownloadableindex ~/Library/Developer/ ...
- 【BZOJ 1911】 [Apio2010]特别行动队
Description Input Output Sample Input 4 -1 10 -20 2 2 3 4 Sample Output 9 HINT 转移方程 f[i]=max(f[j]+ ...
- ExtJs 4.2.1 报错:Uncaught TypeError: Cannot call method 'getItems' of null
做项目的时候遇到这个问题,搞了一上午终于解决了,让我们看看是什么问题: buttons: [ { text: '保存', icon: '../../../Images/extjs/disk.png', ...
- window live writer的曲折安装过程
之前一直使用windows live writer2012写日志,由于之前重装了系统,所以需要重新安装writer,本以为是一个很简单的过程,你就是安装个软件吗.... 然而事实是... ...
- @Autowired获取配置文件中被注入实例的两种方式
一.说明 二.那么在JavaBean中如何通过@Autowired获取该实例呢?有两种方式: 1.直接获取 @RunWith(SpringJUnit4ClassRunner.class) @Conte ...
- case class inheritance
Scala 禁止case class inheritance case class Person(name: String, age: Int) case class FootballPlayer(n ...