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 ...
随机推荐
- python_62_装饰器5
import time def timer(func): #timer(test1) func=test1 def deco(*args,**kwargs): start_time=time.time ...
- PAT (Advanced Level) Practise - 1094. The Largest Generation (25)
http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...
- ElasticSearch High Level REST API【3】Scroll 滚屏
ES中提供了 FROM/SIZE 分页,但这种分页有性能瓶颈. Scroll会以间隔时间滚屏的方式返回全部的查询数据,可以作为数据量很大的情况下,分页的一个替代方案 完整的示例如下: public v ...
- jupyter notebook(三)——IOPub_data_rate_limit报错
一.问题 运行jupyter notebook,然后运行python代码,读取文件处理时,会报错.发现时IO读取时错误.应该是IO速率问题. 下面是问题报错: IOPub data rate exce ...
- MySQL存储引擎MyISAM与InnoDB的区别比较
使用MySQL当然会接触到MySQL的存储引擎,在新建数据库和新建数据表的时候都会看到. MySQL默认的存储引擎是MyISAM,其他常用的就是InnoDB了. 至于到底用哪种存储引擎比较好?这个问题 ...
- JZOJ 4732. 【NOIP2016提高A组模拟8.23】函数
4732. [NOIP2016提高A组模拟8.23]函数 (Standard IO) Time Limits: 1500 ms Memory Limits: 262144 KB Detailed ...
- SoapUI(一)之webservice测试
webservice测试需要具备的条件: 1.了解业务需求:如从客户端发送一个post请求给服务器,服务器将响应传给客户端. 2.需要一个明确的wsdl地址: 如天气预报的接口链接:http://ww ...
- HDU 6053 ( TrickGCD ) 分块+容斥
TrickGCD Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- GIt-恢复进度
继续暂存区未完成的实践 经过了前面的实践,现在DEMO版本库应该处于master分支上,看看是不是这样. $ cd /path/to/my/workspace/demo $ git status -s ...
- 爬虫工程师常用的 Chrome 插件
做多了爬虫都知道,写一个爬虫大部分时间不是在代码上,而是在分析网页上,所有有一套好用的工具可以极大节省劳动力,这里把平时积累的一些 Chrome 插件分享出来,均来自本人和同事推荐,并不定时更新,欢迎 ...