【cdoj 1544】当咸鱼也要按照基本法
2024-10-01 14:43:01
【题目链接】:http://acm.uestc.edu.cn/#/problem/show/1544
【题意】
【题解】
容斥原理题;
1..(2^m)-1枚举
设k为其二进制形式中1的个数;
k为奇数
ans+=2^(k-1)*(n/lcm(对应的k个数))
否则-=…..
如果不加那个系数的话,算的是这几个数的公倍数的个数…
【Number Of WA】
2
【反思】
没有注意到自己做的结果是什么…
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 15;
int n, m;
int a[N + 5];
int two[N + 5];
LL gcd(LL x, LL y) {
return y == 0 ? x : gcd(y, x%y);
}
LL lcm(LL x, LL y) {
return (x / gcd(x, y)*y);
}
void solve() {
cin >> n >> m;
rep1(i, 1, m)
cin >> a[i];
LL ans = 0;
rep1(i, 1, two[m] - 1) {
int num = 0; LL temp = 1;
rep1(j, 1, m)
if (i&(two[j-1])){
num++;
if (temp > n) continue;
temp = lcm(temp, a[j]);
}
if (num & 1)
ans += two[num-1]*(n / temp);
else
ans -= two[num-1]*(n / temp);
}
cout << ans << endl;
}
int main() {
//Open();
Close();
two[0] = 1;
rep1(i, 1, 15) two[i] = two[i - 1] * 2;
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
最新文章
- js运动框架之一条乱跑的虫子
- ubuntu gcc-5 安装
- Java中的TreeMap、Comparable、Comparator
- django中时区设置
- Oracle 12c 数据库中scott用户不存在的解决方法
- Oracle 联合主键
- OpenCV之mixChannels()函数使用说明
- linux下启动和关闭网卡命令
- 解决animate动画连续播放bug
- 二探ListView
- 6. Shell 流程控制
- R与并行计算(转)
- Even Parity uva11464 模拟
- IdentityServer4 实现 OAuth 2.0(密码模式 - HTTP Post 方式)
- javascript第三章--引用类型
- 《java入门第一季》正则表达式小案例
- Linux中断管理 (1)Linux中断管理机制
- js jquery 遍历 for,while,each,map,grep
- opencv+python-图片文本倾斜校正
- Javascript-关于break、continue、return语句