codeforces 439 E. Devu and Birthday Celebration 组合数学 容斥定理
题意:
q个询问,每一个询问给出2个数sum,n
1 <= q <= 10^5, 1 <= n <= sum <= 10^5
对于每一个询问,求满足下列条件的数组的方案数
1.数组有n个元素,ai >= 1
2.sigma(ai) = sum
3.gcd(ai) = 1
solution:
这道题的做法类似bzoj2005能量采集
f(d) 表示gcd(ai) = d 的方案数
h(d) 表示d|gcd(ai)的方案数
令ai = bi * d
则有sigma(bi) = sum / n
d | gcd(ai)
还要满足bi >= 1
则显然有h(d) = C(sum / d - 1,n - 1)
h(d) = f(d) + f(2d) + ... + f(d_max)
这里的d满足:
1.d是sum 的约数
2.sum / d >= n
则f(d) = h(d) - sigma(f(j)) ,2d <=j<=sum/n
倒序遍历d
ans = f(1)
由于询问的次数太多,每次询问后,可以把(sum,n)放入map中,记录下来
//File Name: cf439E.cpp
//Author: long
//Mail: 736726758@qq.com
//Created Time: 2016年02月17日 星期三 14时58分16秒 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstdlib>
#include <vector> #define LL long long
#define pb push_back using namespace std; const int MAXN = 1e5+;
const int MOD = 1e9+; LL f[MAXN];
LL jie[MAXN];
bool is[MAXN];
vector<int> dive;
map< pair<int,int>,int > rem; void init()
{
jie[] = ;
for(int i=;i<MAXN;i++){
jie[i] = jie[i-] * i % MOD;
}
rem.clear();
} void get_dive(int sum,int n)
{
int e = (int)sqrt(sum + 0.0);
dive.clear();
int j;
for(int i=;i<=e;i++){
if(sum % i == ){
if(sum / i >= n)
dive.pb(i);
j = sum / i;
if(j != i && sum / j >= n)
dive.pb(j);
}
}
sort(dive.begin(),dive.end());
for(int i=;i<dive.size();i++){
is[dive[i]] = true;
}
} LL qp(LL x,LL y)
{
LL res = 1LL;
while(y){
if(y & )
res = res * x % MOD;
x = x * x % MOD;
y >>= ;
}
return res;
} LL comb(int x ,int y)
{
if(y < || y > x)
return ;
if(y == || y == x)
return ;
return jie[x] * qp(jie[y] * jie[x-y] % MOD,MOD - ) % MOD;
} void solve(int sum,int n)
{
map< pair<int,int>,int >::iterator it;
it = rem.find(make_pair(sum,n));
if(it != rem.end()){
printf("%d\n",(int)(it->second));
return ;
}
memset(f,,sizeof f);
memset(is,false,sizeof is);
get_dive(sum,n);
int ma = dive.size();
for(int i=ma-;i>=;i--){
int d = dive[i];
f[d] = comb(sum / d - ,n - );
for(int j=*d;j<=dive[ma-];j+=d){
if(is[j]){
f[d] = ((f[d] - f[j] + MOD) % MOD + MOD) % MOD;
}
}
}
printf("%d\n",(int)f[]);
rem[make_pair(sum,n)] = f[];
return ;
} int main()
{
init();
int test;
scanf("%d",&test);
while(test--){
int sum,n;
scanf("%d %d",&sum,&n);
solve(sum,n);
}
return ;
}
codeforces 439 E. Devu and Birthday Celebration 组合数学 容斥定理的更多相关文章
- CF(439E - Devu and Birthday Celebration)莫比乌斯容斥
题意:将n个糖果插入f-1个挡板分成f分(a1,a2,a3...af). 问有多少种分法能够使得gcd(a1,a2,a3...af)=1; 解法.莫比乌斯容斥,首先按1为单位分,这时候有C(n-1,f ...
- Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理
B. Pasha and Phone Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/pr ...
- Codeforces 493 E.Devu and Birthday Celebration
\(>Codeforces \space 493\ E.Devu\ and\ Birthday\ Celebration<\) 题目大意 : 有 \(q\) 组询问,每次有 \(n\) 小 ...
- HDU 6397 Character Encoding (组合数学 + 容斥)
题意: 析:首先很容易可以看出来使用FFT是能够做的,但是时间上一定会TLE的,可以使用公式化简,最后能够化简到最简单的模式. 其实考虑使用组合数学,如果这个 xi 没有限制,那么就是求 x1 + x ...
- codeforces#1228E. Another Filling the Grid(容斥定理,思维)
题目链接: https://codeforces.com/contest/1228/problem/E 题意: 给n*n的矩阵填数,使得每行和每列最小值都是1 矩阵中可以填1到$k$的数 数据范围: ...
- [CSP-S模拟测试]:多维网格(组合数学+容斥)
题目传送门(内部题138) 输入格式 输入数据第一行为两个整数$d,n$. 第二行$d$个非负整数$a_1,a_2,...,a_d$. 接下来$n$行,每行$d$个整数,表示一个坏点的坐标.数 ...
- [BZOJ2839]:集合计数(组合数学+容斥)
题目传送门 题目描述 .(是质数喔~) 输入格式 一行两个整数N,K. 输出格式 一行为答案. 样例 样例输入: 3 2 样例输出: 样例说明 假设原集合为{A,B,C} 则满足条件的方案为:{AB, ...
- Codeforces 439 A. Devu, the Singer and Churu, the Joker
这是本人第一次写代码,难免有点瑕疵还请见谅 A. Devu, the Singer and Churu, the Joker time limit per test 1 second memory l ...
- codeforces#439 D. Devu and his Brother (二分)
题意:给出a数组和b数组,他们的长度最大1e5,元素范围是1到1e9,问你让a数组最小的数比b数组最大的数要大需要的最少改变次数是多少.每次改变可以让一个数加一或减一 分析:枚举a数组和b数组的所有的 ...
随机推荐
- 增量更新项目时的备份MyBak
在增量更新项目时,做好备份十分重要,这里提供一个方法备份java Web所更新的文件. 把更新包放在指定目录,配好如下webappFolder.updateFolder以及bakeupFolder的路 ...
- 黑马程序员——JAVA基础之常用DOS命令和环境变量的配置
------- android培训.java培训.期待与您交流! ---------- 1.常用dos命令: dir 显示当前文件下目录 ...
- python部分模块的安装
scrapy http://www.cnblogs.com/txw1958/archive/2012/07/12/scrapy_installation_introduce.html pyHook - ...
- ws318 配置
http://www.192ly.com/router-settings/huawei/ws318-sz.html
- OpenJudge计算概论-寻找山顶
/*===================================== 寻找山顶 总时间限制: 1000ms 内存限制: 65536kB 描述 在一个m×n的山地上,已知每个地块的平均高程,请 ...
- unity 4 Please check your configuration file and verify this type name.
The problem is in you config file. You are mixing two concepts with some incorrect syntax. The <a ...
- 【转】C#综合揭秘——通过修改注册表建立Windows自定义协议
引言 本文主要介绍注册表的概念与其相关根项的功能,以及浏览器如何通过连接调用自定义协议并与客户端进行数据通信.文中讲及如何通过C#程序.手动修改.安装项目等不同方式对注册表进行修改.其中通过安装项目对 ...
- C#.NET vs2010中使用IrisSkin4.dll轻松实现WinForm窗体换肤功能
IrisSkin2.dll是一款很不错的免费皮肤控件,利用它可以轻松的实现WinForm窗体换肤 然而IrisSkin2.dll只能在.NET Faremwork 4.0以及之前的版本使用,所以要在V ...
- Splashscreen
Splashscreen Enables developers to show/hide the application's splash screen. Methods show hide Perm ...
- CryptoAPI与openssl数字签名与验证交互
昨天写过了RSA非对称加密解密的交互方式, 其实数字签名也是RSA非对称加密,只不过用私钥加密的,再加上个hash摘要 CryptoAPI与openssl RSA非对称加密解密(PKCS1 PADDI ...