【cdoj 1544】当咸鱼也要按照基本法

【题目链接】: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;
}
posted @   AWCXV  阅读(131)  评论(0编辑  收藏  举报
编辑推荐:
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
阅读排行:
· 为DeepSeek添加本地知识库
· 精选4款基于.NET开源、功能强大的通讯调试工具
· DeepSeek智能编程
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
点击右上角即可分享
微信分享提示