BZOJ 3462 DZY Loves Math II ——动态规划 组合数
好题。
首先发现$p$是互质的数。
然后我们要求$\sum_{i=1}^{k} pi*xi=n$的方案数。
然后由于$p$不相同,可以而$S$比较小,都是$S$的质因数
可以考虑围绕$S$进行动态规划。
然后发现有时候许多情况是多余的。因为一整个$S$只能由一些相同的$p$组合而成。
所以这些部分可以用组合数计算,剩下的部分可以用背包处理出来。
需要滚动数组,而且需要前缀和转移。
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define int long long
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
#define maxn 2000005
#define md 1000000007
int pri[10],f[2][maxn<<3],s,q,top=0;
int Dp()
{
int now=0,pre=1;
memset(f[now],0,sizeof f[now]);
f[now][0]=1;
F(i,1,top)
{
now^=1;pre^=1;memset(f[now],0,sizeof f[now]);
int up=s/pri[i]-1;
F(l,0,pri[i]-1)
{
int presum=0;
for (int j=0;j<=(s*top-l)/pri[i];j++)
{
presum+=f[pre][j*pri[i]+l];presum%=md;
if (j>=up+1) presum-=f[pre][(j-up-1)*pri[i]+l];
f[now][j*pri[i]+l]=presum;
}
}
}
return now;
} int ksm(int a,int b)
{
int ret=1;
for (;b;b>>=1,a=a*a%md) if (b&1) ret=ret*a%md;
return ret;
} int C(int n,int m)
{
n=n+1; m=m-1;
n=n+m-1;
int ret=1;
for(int i=n;i>=n-m+1;i--)
ret=ret*(i%md)%md;
F(i,1,m) ret=ret*ksm(i,md-2)%md;
return ret;
} signed main()
{
scanf("%lld%lld",&s,&q);int x=s;
F(i,2,sqrt(s))
{
if (s%i==0) s/=i,pri[++top]=i;
if (s%i==0)
{
while(q--) printf("0\n");
return 0;
}
}
if (s>1) pri[++top]=s; s=x;
int now=Dp();
while(q--)
{
int ret=0;
int n;scanf("%lld",&n);
F(i,1,top) n-=pri[i];
if (n<0) {printf("0\n"); continue;}
int m=n/s,k=n-m*s;
F(i,0,min(top,m))
ret=(ret+f[now][i*s+k]*C(top+m-i-top,top%md)%md)%md;
printf("%lld\n",(ret+md)%md);
}
}
BZOJ 3462 DZY Loves Math II ——动态规划 组合数的更多相关文章
- bzoj 3462: DZY Loves Math II
3462: DZY Loves Math II Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 211 Solved: 103[Submit][Sta ...
- ●BZOJ 3309 DZY Loves Math
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3309 题解: 莫比乌斯反演,线筛 化一化式子: f(x)表示x的质因子分解中的最大幂指数 $ ...
- BZOJ 3561 DZY Loves Math VI
BZOJ 3561 DZY Loves Math VI 求\(\sum_{i=1}^{n}\sum_{j=1}^{m}\text{lcm}(i,j)^{\gcd(i,j)}\),钦定\(n\leq m ...
- BZOJ 3309: DZY Loves Math
3309: DZY Loves Math Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 761 Solved: 401[Submit][Status ...
- BZOJ 3512: DZY Loves Math IV [杜教筛]
3512: DZY Loves Math IV 题意:求\(\sum_{i=1}^n \sum_{j=1}^m \varphi(ij)\),\(n \le 10^5, m \le 10^9\) n较小 ...
- bzoj 3309 DZY Loves Math 莫比乌斯反演
DZY Loves Math Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1303 Solved: 819[Submit][Status][Dis ...
- BZOJ3462 DZY Loves Math II 【多重背包 + 组合数】
题目 输入格式 第一行,两个正整数 S 和 q,q 表示询问数量. 接下来 q 行,每行一个正整数 n. 输出格式 输出共 q 行,分别为每个询问的答案. 输入样例 30 3 9 29 1000000 ...
- BZOJ3462 DZY Loves Math II(动态规划+组合数学)
容易发现这是一个有各种玄妙性质的完全背包计数. 对于每个质数,将其选取个数写成ax+b的形式,其中x=S/pi,0<b<x.那么可以枚举b的部分提供了多少贡献,多重背包计算,a的部分直接组 ...
- DZY Loves Math II:多重背包dp+组合数
Description Input 第一行,两个正整数 S 和 q,q 表示询问数量.接下来 q 行,每行一个正整数 n. Output 输出共 q 行,分别为每个询问的答案. Sample Inpu ...
随机推荐
- empty、isset、is
直接上代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <?php $a=0; $b='0'; $c=0.0; ...
- CUDA高性能编程中文实战11章例子中多设备的例子编译提示问题
提示的问题如下: error : argument of type "void *(*)(void *)" is incompatible with parameter of ty ...
- 【SQL】连接 —— 内连接、外连接、左连接、右连接、交叉连接
连接 · 内连接 · 外连接 · 左连接 · 右连接 · 全连接 · 交叉连接 · 匹配符号(+) 连接 根据表之间的关系,呈现跨表查询的结果. 外连接 内连接 左连接 右连接 全 ...
- 黑马基础阶段测试题:创建Phone(手机)类,Phone类中包含以下内容:
package com.swift; public class Phone { private String pinpai; private int dianliang; public String ...
- 关于小程序button控件上下边框的显示和隐藏问题
问题: 小程序的button控件上下有一条淡灰色的边框,在空件上加上了样式 border:(none/0); 都没办法让button上下的的边框隐藏: 代码如下 <button class=&q ...
- 当GetWindowText获取不到标题时可以用SendMessage
GetWindowText所有父窗口标题基本可以获取到, 但是当获取父窗口下的子窗口控件标题文本时有时候就没那么好用了, 这个时候可以通过SendMessage发送消息来获取,也很简单,C/C++代码 ...
- cf550C. Divisibility by Eight(结论)
题意 给出长度为$n$的字符串,判断是否能删除一些数后被$8$整除 Sol 神仙题啊Orz 结论: 若数字的后三位能被$8$整除,则该数字能被$8$整除 证明 设$x = 10000 * a_i + ...
- 第六篇:python中numpy.zeros(np.zeros)的使用方法
用法:zeros(shape, dtype=float, order='C') 返回:返回来一个给定形状和类型的用0填充的数组: 参数:shape:形状 dtype:数据类型,可选参数,默认numpy ...
- 绘制矩形:描边矩形imagerectangle()、填充矩形imagefilledrectangle()
<?php //1. 绘制图像资源(创建一个画布) $image = imagecreatetruecolor(500, 300); //2. 先分配一个绿色 $green = imagecol ...
- Redis之List类型操作
接口: package com.net.test.redis.base.dao; import java.util.List; /** * @author *** * @Time:2017年8月10日 ...