题意:

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. 穿越泥地(mud) (BFS)

    问题 C: 穿越泥地(mud) 时间限制: 1 Sec  内存限制: 128 MB提交: 16  解决: 10[提交][状态][讨论版] 题目描述 清早6:00,FJ就离开了他的屋子,开始了他的例行工 ...

  2. JAVA常用关键字

    Java 中常用关键字: 一一解释(先以印象注明含义,若有错误或未填写的待用到后补充.更新):(蓝色为不确定部分) abstract : 虚类 boolean : 类型定义——布尔型 break : ...

  3. checkbox 点击全选

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Linux 挂载新硬盘

    Linux 的硬盘识别 在 /dev/ 下建立相应的设备文件.如 sda 表示第一块 SCSI 硬盘 hda 表示第一块 IDE 硬盘(即连接在第一个 IDE 接口的 Master 口上) scd0 ...

  5. Linux 查看CPU信息、机器型号等硬件信息

    测试机器的硬件信息: 查看CPU信息(型号) # cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c       8  Intel(R) Xeo ...

  6. GBG java

    https://sourceware.org/gdb/onlinedocs/gdb/Sample-Session.html#Sample-Session http://m.blog.csdn.net/ ...

  7. NOSQL Benchmarks

    www.planetcassandra.org/nosql-performance-benchmarks/ http://www.planetcassandra.org/nosql-performan ...

  8. Linux-IP地址后边加个/8(16,24,32)是什么意思?

    是掩码的位数        A类IP地址的默认子网掩码为255.0.0.0(由于255相当于二进制的8位1,所以也缩写成“/8”,表示网络号占了8位);    B类的为255.255.0.0(/16) ...

  9. Andorid面试问题整理

    Acitivty的四中启动模式与特点. standard:默认的启动模式 singleTop:适合那种接受通知启动的页面,比如新闻客户端之类的,可能会给你推送好几次 ,但是每次都是打开同一张页面调用o ...

  10. 怎么用CorelDRAW插入、删除与重命名页面

    在绘图之前,页面的各种设置也是一项重要的工作,本文主要讲解如何在CorelDRAW中插入.删除.重命名页面等操作.在CorelDRAW的绘图工作中,常常需要在同一文档中添加多个空白页面,删除一些无用的 ...