Gym 100548F Color (数论容斥原理+组合数)
题意:给定 m 种颜色,把 n 盆花排成一直线的花涂色。要求相邻花的颜色不相同,且使用的颜色恰好是k种。问一共有几种涂色方法。
析:首先是先从 m 种颜色中选出 k 种颜色,然后下面用的容斥原理,当时没想出来,如果是只用一种颜色,那么肯定不行,如果用两种颜色,可以有这么方法,
2 * (2-1) ^ (n-1)种,如果是只用 i 种那么就是 i * (i-1) ^ (n-1)。然后依次求就好。再就是求组合数的时候,由于太大,不能用递推,所以要用逆元。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <ctime>
#include <cstdlib>
#define debug puts("+++++")
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 5;
const LL mod = 1e9 + 7;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
inline int lcm(int a, int b){ return a * b / gcd(a, b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL f[maxn];
LL cm[maxn], ck[maxn]; void init(){
f[1] = 1;
for(int i = 2; i < maxn; ++i)
f[i] = (mod - mod/i) * f[mod%i] % mod;
} LL quick_pow(LL a, LL n){
LL ans = 1LL;
while(n){
if(n & 1) ans = ans * a % mod;
a = a * a % mod;
n >>= 1;
}
return ans;
} LL solve(LL n, LL m, LL k){
ck[0] = cm[0] = 1;
for(int i = 1; i <= k; ++i){
cm[i] = cm[i-1] * (m - i + 1) % mod * f[i] % mod;
ck[i] = ck[i-1] * (k - i + 1) % mod * f[i] % mod;
}
LL ans = 0;
LL cnt = 1;
for(int i = k; i >= 1; --i, cnt = -cnt)
ans = (mod + ans + ck[i] * i % mod * quick_pow(i-1, n-1) * cnt % mod) % mod;
return ans * cm[k] % mod;
} int main(){
init();
int T; cin >> T;
LL n, m, k;
for(int kase = 1; kase <= T; ++kase){
scanf("%I64d %I64d %I64d", &n, &m, &k);
printf("Case #%d: %I64d\n", kase, solve(n, m, k));
}
return 0;
}
Gym 100548F Color (数论容斥原理+组合数)的更多相关文章
- 2019.02.09 codeforces gym 100548F. Color(容斥原理)
传送门 题意简述:对n个排成一排的物品涂色,有m种颜色可选. 要求相邻的物品颜色不相同,且总共恰好有K种颜色,问所有可行的方案数.(n,m≤1e9,k≤1e6n,m\le1e9,k\le1e6n,m≤ ...
- Gym 100548F Color 2014-2015 ACM-ICPC, Asia Xian Regional Contest (容斥原理+大数取模)
题意:有N朵花,在M种颜色中选择恰好k种不同的颜色,将这N朵花染色,要求相邻的两朵花颜色不相同. 分析:若限制改为选择不超过k种颜色将N朵花朵染色,则方案数\(f(N,k) = k*(k-1)^{N- ...
- Gym 100548F Color 给花染色 容斥+组合数学+逆元 铜牌题
Problem F. ColorDescriptionRecently, Mr. Big recieved n flowers from his fans. He wants to recolor th ...
- Codeforces 100548F - Color (组合数+容斥)
题目链接:http://codeforces.com/gym/100548/attachments 有n个物品 m种颜色,要求你只用k种颜色,且相邻物品的颜色不能相同,问你有多少种方案. 从m种颜色选 ...
- 【BZOJ 2005】【NOI 2010】能量采集 数论+容斥原理
这题设$f(i)$为$gcd(i,j)=x$的个数,根据容斥原理,我们只需减掉$f(i×2),f(i×3)\cdots$即可 那么这道题:$$ans=\sum_{i=1}^n(f(i)×((i-1)× ...
- HDU 2841 Visible Trees 数论+容斥原理
H - Visible Trees Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- xdoj-1149(多重集合+容斥原理+组合数取模)
#include <iostream> #include <algorithm> #include <cstdio> using namespace std; ty ...
- Bzoj4710 分特产(容斥原理+组合数)
题面 Bzoj 题解 考虑容斥原理,所有人都有特产的方案数等于: 至少零个人没有特产\(-\)至少一个人没有特产\(+\)至少两个人有特产\(-...\) 接着考虑其中一种情况怎么求(假设现在至少有\ ...
- 【BZOJ2839】集合计数 容斥原理+组合数
Description 一个有N个元素的集合有2^N个不同子集(包含空集),现在要在这2^N个集合中取出若干集合(至少一个),使得 它们的交集的元素个数为K,求取法的方案数,答案模1000000007 ...
随机推荐
- 【HDOJ6342】Expression in Memories(模拟)
题意: 给定一个由0123456789+* ?组成的表达式,其中?可以被改为任意其它字符,问修改问号后是否有方案使得表达式合法 len<=5e2,sumlen<=1e5 思路: #incl ...
- 微服务架构的基础框架选择:Spring Cloud还是Dubbo?
本文转自:http://mt.sohu.com/20160803/n462486707.shtml 最近一段时间不论互联网还是传统行业,凡是涉及信息技术范畴的圈子几乎都在讨论 微服务架构 .近期也看到 ...
- 压力测试webbench(转)
最近app需要搭建后台,故此研究一下,靠谱的后台服务器..网传nginx 能达到的并发数量比apache 高. LAMP or LNMP ? 根据需求测试结果来进行选择. 首先是安装LNMP测试完后 ...
- 重装JDK后Tomcat和Eclipse的配置
比如JDK之前是1.8.0_31的,升级之后变成了1.8.0_131之后,Tomcat需要做如下配置: 对于Eclipse中之前配置的Tomcat需要删除后重新添加一个.
- Solidworks drwdot文件如何打开,如何制作Solidworks工程图模板
1 直接把这个文件拖放进Solidworks窗口 2 文件-保存图纸格式,另存为模板(slddrt文件) 3 搜索"Solidworks工程图如何使用,替换图纸格式模板文件.doc& ...
- webpack-Modules(模块)
模块(Modules) 在模块化编程中,开发者将程序分解成离散功能块(discrete chunks of functionality),并称之为模块. 每个模块具有比完整程序更小的接触面,使得校验. ...
- hdu 4950 Monster(数学题,多校8)
题目链接:pid=4950http://acm.hdu.edu.cn/showproblem.php?pid=4950">http://acm.hdu.edu.cn/showprobl ...
- Android开发:怎样隐藏自己的app应用
本文主要介绍怎样通过改动AndroidManifest.xml清单文件来达到隐藏自身应用的目的,不是隐藏第三方应用.为了不浪费大家时间.特此说明. 转载请注明作者xiong_it和链接:http:// ...
- nagios 安装配置(包含nrpe端)全 (三)
四.系统的配置: 1.介绍 在配置过程中涉及到的几个定义有:主机.主机组,服务.服务组.联系人.联系人组,监控时间.监控命令等. 最重要的有四点: 第一:定义监控哪些主机.主机组.服务和服务组: 第二 ...
- Scrum 常见错误实践 之 形式化的站会
站会作为一个团队最容易实施的敏捷实践,为广大team leader和老板们所喜欢,但大部分程序员却很抵触.其主要原因就是很多时候站会都流于形式,没能帮助团队成员解决问题.改进效率. 一种常见的情况就是 ...