题意:

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 组合数学 容斥定理的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Codeforces 493 E.Devu and Birthday Celebration

    \(>Codeforces \space 493\ E.Devu\ and\ Birthday\ Celebration<\) 题目大意 : 有 \(q\) 组询问,每次有 \(n\) 小 ...

  4. HDU 6397 Character Encoding (组合数学 + 容斥)

    题意: 析:首先很容易可以看出来使用FFT是能够做的,但是时间上一定会TLE的,可以使用公式化简,最后能够化简到最简单的模式. 其实考虑使用组合数学,如果这个 xi 没有限制,那么就是求 x1 + x ...

  5. codeforces#1228E. Another Filling the Grid(容斥定理,思维)

    题目链接: https://codeforces.com/contest/1228/problem/E 题意: 给n*n的矩阵填数,使得每行和每列最小值都是1 矩阵中可以填1到$k$的数 数据范围: ...

  6. [CSP-S模拟测试]:多维网格(组合数学+容斥)

    题目传送门(内部题138) 输入格式 输入数据第一行为两个整数$d,n$. 第二行$d$个非负整数$a_1,a_2,...,a_d$.     接下来$n$行,每行$d$个整数,表示一个坏点的坐标.数 ...

  7. [BZOJ2839]:集合计数(组合数学+容斥)

    题目传送门 题目描述 .(是质数喔~) 输入格式 一行两个整数N,K. 输出格式 一行为答案. 样例 样例输入: 3 2 样例输出: 样例说明 假设原集合为{A,B,C} 则满足条件的方案为:{AB, ...

  8. 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 ...

  9. codeforces#439 D. Devu and his Brother (二分)

    题意:给出a数组和b数组,他们的长度最大1e5,元素范围是1到1e9,问你让a数组最小的数比b数组最大的数要大需要的最少改变次数是多少.每次改变可以让一个数加一或减一 分析:枚举a数组和b数组的所有的 ...

随机推荐

  1. Codeforces Round #127 (Div. 2)

    A. LLPS 长度最大10,暴力枚举即可. B. Brand New Easy Problem 枚举\(n\)的全排列,按题意求最小的\(x\),即逆序对个数. C. Clear Symmetry ...

  2. NOI 2004 郁闷的出纳员(平衡树)

    题目描述 OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的工资 ...

  3. kuangbin_ShortPath E (POJ 1860)

    第一次做判环 然后RE了五次 死在了奇怪的点 memset(vis, 0, sizeof dis); memset(dis, 0, sizeof vis); 什么鬼?? 什么鬼?? 其实代码本身还是不 ...

  4. jQuery对下拉框Select操作总结

    jQuery对下拉框Select操作总结 转自网络,留做备用 jQuery获取Select元素,并选择的Text和Value: 1. $("#select_id").change( ...

  5. Latex 编译错误: ! pdfTeX error (ext4): \pdfendlink ended up in different nesting level than \pd fstartlink. 解决方法

    最近写 AAAI 的文章,下载了其模板,但是蛋疼的是,总是提示错误,加上参考文献总是出错: 如下: ! pdfTeX error (ext4): \pdfendlink ended up in dif ...

  6. Linux-lsof命令

    lsof,List Open Files 列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件(lsof强大原因).所以 ...

  7. C++重载,重写,重定义

    1.重载:重载是在一个类中,函数名一样,参数类型或参数个数不同的一系列函数需要注意的是,与返回类型无关. class Test { public: void show(); void show(int ...

  8. HTTPS(SSL)详解以及PHP调用方法

    HTTPS 详解 1. 两个加密秘钥的概念 (1) 对称加密 即加密的秘钥和解密的秘钥一样 (2) 非对称加密 即加密的秘钥和解密的秘钥不一样, 分别称为公钥 和 私钥,  公钥完全公开  私钥解密者 ...

  9. SpringMVC常用注解,返回方式,路径匹配形式,验证

    常用注解元素 @Controller 标注在Bean的类定义处 @RequestMapping 真正让Bean具备 Spring MVC Controller 功能的是 @RequestMapping ...

  10. Rational Rose 安装及破解方法

    Rational Rose 的安装与破解:  所需材料: 1.“Rational rose 2003.rar”安装包压缩文件                 2.“license.rar”压缩文件(一 ...