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数组的所有的 ...
随机推荐
- android中ListView_SimpleAdapter
1.首先看下main_activity.xml.其实里面就放了一个ListView. <LinearLayout xmlns:android="http://schemas.andro ...
- 带百分号的数据转json
今天处理前后数据传输的时候遇到了问题. 前台拼参数是这样写的 '&rowData=' + JSON.stringify(rowData); 后台解析出的对象却总是null,而且没有抛Parse ...
- 越狱Season 1-Episode 2: Allen
Season 1, Episode 2: Allen [Previously, on Prison Break] previously: 以前(预先) 前情回顾 -Judge: I find it i ...
- 渴切-开源中文css框架
渴切:是国内优秀的开源css框架. 渴切是一个开源中文 (X)HTML/CSS 框架 ,它的目的是减少你的css开发时间.它提供一个可靠的css基础去创建你的项目,能够用于网站的快速设计,通过重设和重 ...
- 利用CSS的@font-face属性 在网页中嵌入字体
字体使用是网页设计中不可或缺的一部分.网页是文字的载体,我们希望在网页中使用某一特定字体,但是该字体并非主流操作系统的内置字体,这样用户在浏览页面的时候就有可能看不到真实的设计. 美工设计师最常做的办 ...
- 针对主流浏览器的CSS-HACK写法及IE常用条件注释
一.通用区分方式: IE6.IE7能识别*,标准浏览器(如FF)不能识别*:IE6能识别*,但不能识别 !important:IE7能识别*,也能识别 !important:IE8能识别\0,不能识别 ...
- 异步IO/数据库/队列/缓存
同步与异步的性能区别 mport gevent def task(pid): """ Some non-deterministic task ""& ...
- 利用redis写webshell
redis和mongodb我之所见 最近自己在做一些个人的小创作.小项目,其中用到了mongodb和redis,最初可能对这二者没有深入的认识.都是所谓的“非关系型数据库”,有什么区别么? 实际上,在 ...
- Entity Framework调用表值函数实现全文检索?
CREATE FUNCTION [dbo].[udf_BookContentSearch](@keywords NVARCHAR(1000))RETURNS @BookPageDetail TABLE ...
- Visual Studio 文件没发布出来
解决办法是选择文件打开属性窗口找到生成操作,选项选择"内容",重新发布,OK,问题解决.