[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 ...
随机推荐
- The architecture of LTE network.
3GPP定义的LTE网络架构结构变得扁平化,无线RNC/BSC 消失,只有eNodeB.控制面使用MME进行处理,用户面使用SGW和PGW进行处理.相比GSM和UMTS,在逻辑接口上定义了S1/X2逻 ...
- 在emacs里用w3m浏览网页
给w3m配置个~/.emacs老是暴错误,在配置里把这个注掉就OK了;(require 'mime-w3m) ;;支持w3m (add-to-list 'load-path "/usr/sh ...
- 在win7上建立本地FTP站点详细步骤
一.安装FTP组件点击:控制面板—>程序和功能—>打开或关闭Windows功能. 勾选“FTP服务器”及“FTP服务”“FTP扩展性”,点击“确定”,安装FTP组件. 勾选Web管理工具的 ...
- Mysql编辑工具中使用(Navicat查询结果显示行号)
Mysql编辑工具中使用(Navicat查询结果显示行号) as rownum,a.roleId ) t where a.roleId='admin';
- Nginx简单性能调优
Nginx默认没有开启利用多核CPU (忍不住吐槽,然怪总感觉服务器性能没充分发挥), 我们可以通过增加worker_cpu_affinity配置参数来充分利用多核CPU.CPU是任务处理,计算最关键 ...
- mybatis + postgresql 遇到的问题
org.postgresql.util.PSQLException: ERROR: relation "circlefence" does not exist 这个问题是数据库表 ...
- SharpDeveloeper开发ASP.NET MVC汗流浃背
今天好不容易休息了一天,上网狂了一圈,突然想起了以前的一个轻量级的开发工具"SharpDeveloper",于是就下载试着来开发一下ASP.NET,但是老魏没有想到的是,虽然官方提 ...
- Delphi与Qt在Windows下使用共享内存进程间通信
Delphi部分 type TGuardInfo=record Lock: Integer; end; PGuardInfo = ^TGuardInfo; TGuardShareMem=c ...
- 在系统方法中调用navigationController的标准写法
在系统方法中调用navigationController的标准写法 -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animat ...
- JAVA简单学习
枚举类型:Size s=Size.SMALL; //从字串转换为枚举 枚举可用于for,switch语句中 运行EnumTest.java 枚举类型是引用类型,不属于原始数据类型,可以使用“==”和e ...