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>
using namespace std;
long long n,m;
long long a[];
long long gcd(long long a,long long b)
{
if(!b){
return a;
}
return gcd(b,a%b);
}
long long lcm(long long a,long long b)
{
return a*b/gcd(a,b);
}
int vis[];
long long ans=;
long long pre[];
void dfs(register int dep,long long goal,long long now)
{
if(dep>goal){
if(goal%==){
ans-=(m/now);
}
else{
ans+=(m/now);
}
return;
}
for(register int i=pre[dep-]+;i<=n;i++){
if(!vis[i]){
pre[dep]=i;
vis[i]=;
dfs(dep+,goal,lcm(now,a[i]));
vis[i]=;
}
}
}
int main()
{
freopen("div.in","r",stdin);
freopen("div.out","w",stdout);
cin>>n>>m;
for(register int i=;i<=n;i++){
cin>>a[i];
ans+=(m/a[i]);
}
for(register int k=;k<=n;k++){
dfs(,k,);
}
cout<<ans<<endl;
}
ZOJ 2836 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) ...
- Number Puzzle
Number Puzzle Time Limit: 2 Seconds Memory Limit: 65536 KB Given a list of integers (A1, A2, .. ...
- ZOJ 1602 Multiplication Puzzle(区间DP)题解
题意:n个数字的串,每取出一个数字的代价为该数字和左右的乘积(1.n不能取),问最小代价 思路:dp[i][j]表示把i~j取到只剩 i.j 的最小代价. 代码: #include<set> ...
- ZOJ Monthly, March 2018 题解
[题目链接] A. ZOJ 4004 - Easy Number Game 首先肯定是选择值最小的 $2*m$ 进行操作,这些数在操作的时候每次取一个最大的和最小的相乘是最优的. #include & ...
- 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 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 ...
随机推荐
- 文件操作:fopen()
r 打开只读文件,该文件必须存在. r+ 打开可读写的文件,该文件必须存在. rb+ 读写打开一个二进制文件,只允许读写数据. rt+ 读写打开一个文本文件,允许读和写. w 打开只写文件, ...
- 获取URL值带参数跳转
//要跳转界面 api.openWin({ name: 'PayOrder', url: 'PayOrder.html?id=2', pageParam:{name:pr} }); //跳转后的界面 ...
- luoguP1041 传染病控制 x
P1041 传染病控制 题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这种传染 ...
- vue-cli中route和router的区别
在使用vue-router的时候经常分不清router和route的区别: 在函数式编程中: this.$router.push('/login') 或者Router.push() 在动态获取路由参数 ...
- 一个困扰很久的异常—java.lang.NoClassDefFoundError: com/google/gson/Gson
描述: 这个异常是在运行时抛出的,编译的时候没有任何问题,而且异常信息非常少,只有一句:java.lang.NoClassDefFoundError: com/google/gson/Gson 解决: ...
- @Configuration与@Bean
1,@Configuration与@Bean @Configuration: 告诉Spring这是一个配置类,配置类==配置文件. @Configuration==beans.xml @Bean: ...
- lyf基础作业
include <stdio.h> include <stdlib.h> int main (void) { FILE * fp; int a[10]; int max=0; ...
- 全排列函数next_permutation(a,a+n)
#include<iostream> #include<algorithm> using namespace std; int main(){ ]; int n; cin> ...
- vscode 配置go环境 可调试
1.go下载安装并配置环境变量 2.安装工具: 1)安装gocode go get -u -v github.com/nsf/gocode 2)安装godef go get -u -v githu ...
- Python学习笔记:第一次接触
用的是windows的IDLE(python 3) 对象的认识:先创建一个list对象(用方括号) a = ['xieziyang','chenmanru'] a 对list中对象的引用 a[0] # ...