http://acm.uestc.edu.cn/#/problem/show/1544

考虑一下2、2、2这样的情况。答案应该是n / 2

如果只选一个的情况下,对答案的贡献是正的,但是这里有三个,也就是我们统计了3 * n / 2,统计多了。

那么对于任选两个数的情况,有三种,(2, 2) * 3,分别都是不同位置的2,

/**************************************/

我做的时候是发现,先讨论只有

2、2的情况,也就是只有两个数的时候,ans = 0,这个时候,先模拟上面的,只选一个,答案是2 * n / 2

那么枚举两个数的情况,应该就是要ans -= 2 * n / (lcm(2, 2))

要减2倍,不然答案不是0.

/**************************************/

那么上面也是,有三种(2, 2)的情况,ans -= 3 * 2 * n / (lcm(2, 2))

那么现在是-3 * n / (lcm(2, 2))

然后还有一种的就是枚举三个的情况,要使答案是n / 2,那么应该加上4 * n / (lcm(2, 2))

然后得到的规律是1 << (sel - 1),sel是选的数字个数。

完全是瞎比比的,数学不好。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
int n, has;
const int maxn = + ;
int arr[maxn];
LL ans;
LL LCM(LL a, LL b) {
return a / __gcd(a, b) * b;
}
void dfs(int cur, int sel, LL theLcm) {
if (theLcm > n) return;
if (cur == has + ) {
if (!sel) return;
if (sel & ) {
ans += ( << (sel - )) * (n / theLcm);
} else {
ans -= ( << (sel - )) * (n / theLcm);
}
return;
}
dfs(cur + , sel, theLcm);
dfs(cur + , sel + , LCM(theLcm, arr[cur]));
}
void work() {
ans = ;
scanf("%d%d", &n, &has);
for (int i = ; i <= has; ++i) {
scanf("%d", &arr[i]);
}
dfs(, , );
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}

还有就是数据中没有15个大质数这样的情况,数据比较弱。

UESTC - 1544 当咸鱼也要按照基本法 组合数学 容斥原理的更多相关文章

  1. 【容斥原理】CDOJ - 1544 - 当咸鱼也要按照基本法

    众所周知zhu是一个大厨,zhu一直有自己独特的咸鱼制作技巧. tang是一个咸鱼供应商,他告诉zhu在他那里面有NN条咸鱼(标号从1到N)可以被用来制作. 每条咸鱼都有一个咸鱼值KiKi,初始时所有 ...

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

    [题目链接]:http://acm.uestc.edu.cn/#/problem/show/1544 [题意] [题解] 容斥原理题; 1..(2^m)-1枚举 设k为其二进制形式中1的个数; k为奇 ...

  3. cdoj1325卿学姐与基本法

    地址:http://acm.uestc.edu.cn/#/problem/show/1325 题目: 卿学姐与基本法 Time Limit: 2000/1000MS (Java/Others)     ...

  4. B - 卿学姐与基本法 (离散化+成段更新+区间求和)

    卿学姐与基本法 Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit S ...

  5. ACM:UESTC - 649 括号配对问题 - stack

      UESTC - 649  括号配对问题 Time Limit: 1000MS   Memory Limit: 65535KB   64bit IO Format: %lld & %llu ...

  6. UESTC 1015 Lweb and pepper --前,后缀最值

    题意: n种食物,每种含花椒的概率为Pi,现在已经选择了[L,R]这个区间(下标)的食物,要再选一个,使总的食物只有一种含花椒的概率最大,问选哪个最好,相同的选下标小的. 解法: 就不写解法了.此处有 ...

  7. UESTC 1852 Traveling Cellsperson

    找规律水题... Traveling Cellsperson Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged ...

  8. UESTC 1851 Kings on a Chessboard

    状压DP... Kings on a Chessboard Time Limit: 10000ms Memory Limit: 65535KB This problem will be judged ...

  9. UESTC 30 最短路,floyd,水

    最短路 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Statu ...

随机推荐

  1. socket.io emit 常见用法

    io.on('connect', onConnect); function onConnect(socket){ // 只发给sender. sending to the client socket. ...

  2. BZOJ2120:数颜色(数状数组套主席树)(带修改的莫对)

    墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2. R P ...

  3. Oracle中的关键字

    NVL和COALESCE的区别: nvl(COMMISSION_PCT,0)如果第一个参数为null,则返回第二个参数如果第一个参数为非null,则返回第一个参数 COALESCE(EXPR1,EXP ...

  4. Java笔记(四)

    13. 集合框架: 集合中存储的都是对象的引用(地址) 迭代器:集合的取出元素的方式 import java.util.ArrayList; import java.util.Iterator; pu ...

  5. codevs 1497取余运算

    1497 取余运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamon   题目描述 Description 输入b,p,k的值,编程计算bp mod k的值. ...

  6. c++之cin/cin.get/cin.getline()详解

    C++输入过程中,是把输入加载到缓冲区中,然后对缓冲区中的字符进行读取.cin,cin,get(),cin.getline()三个函数虽然都能进行数据读取,但是它们对缓冲区内数据的处理方法是不同的(如 ...

  7. PTA-数据结构 Dijkstra 城市间紧急救援

    城市间紧急救援(25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标 ...

  8. pdf2swf 转换时报This file is too complex to render- SWF only supports 65536 shapes at once

    ERROR   ID Table overflow ERROR   This file is too complex to render- SWF only supports 65536 shapes ...

  9. 给source insight添加.cc的C++文件后缀识别(转载)

    转自:http://blog.chinaunix.net/uid-9950859-id-99172.html 今天在读mysql代码的时候,发现.cc结尾的文件都没有添加进来,google下了,发现原 ...

  10. STL-开篇

    基本概念 STL: Standard Template Library,标准模板库 定义: c++引入的一个标准类库 特点:1)数据结构和算法的 c++实现( 采用模板类和模板函数)2)数据的存储和算 ...