https://scut.online/p/113

终于想懂了这个容斥,

华工4月23号校赛,
考虑总的所有情况,设1---n里面含有质数的个数为all,需要固定m个质数。那么有

totSum = C(all, m) * (n - m)!,就是在all个质数里面,任意选m个出来固定,剩下的全排。

但是算多了,因为还有一些质数(不在那m个之内)也会被固定,

而且,考虑样例

5 1,

1 2 3 4 5


这个时候,先考虑任选m个出来固定,题目就是任选1个出来固定。剩下的全排

比如固定了2,剩下的全排,会产生,只固定了2,固定了2、3,固定了2、5,固定了2、3、5

比如固定了3,剩下的全排,会产生,只固定了3,固定了2、3,固定了3、5,固定了2、3、5

比如固定了5,剩下的全排,会产生,只固定了5,固定了2、5,固定了3、5,固定了2、3、5

那么只有第一列相加的才是正确答案。后面的要减去。

减去固定了两个的时候,

这个时候前面一个已经固定了,再枚举一个来固定,就是固定2个。所以固定(3, 2)和(2, 3)是不同的方案。

固定了(2, 3)时,会有,只固定2、3和固定了2、3、5

固定了(3, 2)时,会有,只固定3、2和固定了3、2、5

.....

等等。

减去这些后,会发现结果是ans - 3 * (固定了2、3、5),加回来就是了。

就是一个容斥,以后容斥要实打实写出来,毕竟不是大神。

哭~~~~

比赛没想到。刚才也想了很久,光想是不行的。要模拟。

#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> bool check(int val) {
int en = (int)sqrt(val);
for (int i = ; i <= en; ++i) {
if (val % i == ) return false;
}
return true;
}
const int MOD = 1e9 + ;
LL fac[];
LL quick_pow(LL a, LL b, LL MOD) {
LL base = a % MOD;
LL ans = ;
while (b) {
if (b & ) {
ans = ans * base % MOD;
}
base = base * base % MOD; b >>= ;
}
return ans;
} LL C(LL n, LL m, LL MOD) {
if (n < m) return ;
if (n == m) return % MOD;
LL ans1 = ;
LL ans2 = ;
LL mx = max(n - m, m);
LL mi = n - mx;
for (int i = ; i <= mi; ++i) {
ans1 = ans1 * (mx + i) %MOD;
ans2 = ans2 * i % MOD;
}
return (ans1 * quick_pow(ans2, MOD - , MOD) % MOD);
}
int n, m;
void work() {
fac[] = ;
for (int i = ; i <= ; ++i) {
fac[i] = fac[i - ] * i % MOD;
}
int all = ;
for (int i = ; i <= n; ++i) {
all += check(i);
}
LL ans = C(all, m, MOD) * fac[n - m] % MOD;
// cout << ans << endl;
for (int i = ; i <= all - m; ++i) {
if (i & ) {
ans = (ans + MOD - C(all, m, MOD) * C(all - m, i, MOD) % MOD * fac[n - (m + i)] % MOD) % MOD;
} else ans = (ans + C(all, m, MOD) * C(all - m, i, MOD) % MOD * fac[n - (m + i)] % MOD) % MOD;
}
cout << ans % MOD << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
#endif
scanf("%d%d", &n, &m);
work();
return ;
}

D、Homework of PE 容斥原理的更多相关文章

  1. PE Checksum Algorithm的较简实现

    这篇BLOG是我很早以前写的,因为现在搬移到CNBLOGS了,经过整理后重新发出来. 工作之前的几年一直都在搞计算机安全/病毒相关的东西(纯学习,不作恶),其中PE文件格式是必须知识.有些PE文件,比 ...

  2. 原创 C++应用程序在Windows下的编译、链接:第二部分COFF/PE文件结构

    2.1概述 在windows操作系统下,可执行文件的存储格式是PE格式:在Linux操作系统下,可执行文件的存储格式的WLF格式.它们都是COFF格式文件的变种,都是从COFF格式的文件演化而来的. ...

  3. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

  4. hdu2848 Visible Trees (容斥原理)

    题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...

  5. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  6. BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3028  Solved: 1460[Submit][Sta ...

  7. bzoj 4320: ShangHai2006 Homework

    4320: ShangHai2006 Homework Time Limit: 10 Sec Memory Limit: 128 MB Description 1:在人物集合 S 中加入一个新的程序员 ...

  8. 简单PE类代码

    三个文件分别是类定义文件pefile.h;类实现文件pefile.cpp;类调用文件petype.cpp. #ifndef PE_FILE_H #define PE_FILE_H #include & ...

  9. 获取pe文件的文件类型

    工程文件petype.cpp通过调用pefile类中的函数获取文件类型. 文件类型的判断通过5个监测点完成. 监测点1:dos头的e_magic 监测点2:nt头的Signature 监测点3:文件头 ...

随机推荐

  1. bzoj 2850: 巧克力王国 K-D树

    题目大意 http://www.lydsy.com/JudgeOnline/problem.php?id=2850 题解 对于每个人,我们发现它能够接受的巧克力中 如果对参数分别讨论,那么一定是一个连 ...

  2. 【Lintcode】076.Longest Increasing Subsequence

    题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...

  3. 【Lintcode】033.N-Queens

    题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...

  4. The Review Plan I-禁位排列和容斥原理

    The Review Plan I Time Limit: 5000ms Case Time Limit: 5000ms Memory Limit: 65536KB   64-bit integer ...

  5. MSTAR GAMMA

    1.读取系统GAMMA值 2.在此基础上微调 3.导出Gamma.txt->导入系统.“Gamma12BIT_256.c”或者“Gamma12BIT_1024.c”. Read 微调&写 ...

  6. SpringMVC之六:Controller详细介绍

    一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...

  7. SpringMVC之一:SpringMVC原理

    Spring MVC工作流程图   图一   图二  关键组件: DispatcherServlet:前端控制器,与大多数基于Java的Web框架一样, Spring MVC所有的请求都会通过一个前端 ...

  8. Algorithms & Data structures in C++& GO ( Lock Free Queue)

    https://github.com/xtaci/algorithms //已实现 ( Implemented ): Array shuffle https://github.com/xtaci/al ...

  9. JSON 生成 C# Model

    http://www.cnblogs.com/tianqiq/p/4309791.html

  10. CodeForces 287B Pipeline (水题)

    B. Pipeline time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...