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_39_变量补充
#使用eval()函数计算字符串中的有效表达式,并返回结果 a='1+3' print(eval(a)) b='''{ '闵行': { '人民广场': { '炸鸡店': {}, }, } ''' pr ...
- cuda流测试=basic_single_stream
cuda流测试 /* * Copyright 1993-2010 NVIDIA Corporation. All rights reserved. * * NVIDIA Corporation and ...
- 方法 -------JavaScript
本文摘要:http://www.liaoxuefeng.com/ 在一个对象中绑定函数,称为这个对象的方法. 在JavaScript的中,对象的定义是这样的: var xiaoming = { nam ...
- React后台管理系统-登录页面
登录页面 <div className="col-md-4 col-md-offset-4"> <div className=&qu ...
- c++中 endl的意思?
endl是 end line的意思,表示此行结束,换行,就是回车
- Java传值分析
public class Example{String str=new String("good");char[] ch={'a','b','c'};public static v ...
- C/C++程序基础 (九)排序算法简述
排序算法 算法复杂度 算法简述 插入排序 N2 前方有序,依次将后方无序数据插入前方合适位置. 冒泡排序 N2 前方有序,从后方两两比较,将最小泡冒到前方. 选择排序 N2 前方有序,从后方选择最小的 ...
- PowerDesigner导入Excel模板生成实体
在Excel里整理好的表模型数据,可直接导入PowerDesigner.此功能通过PowerDesigner的脚本功能来实现,使用起来也简单.具体操作方法: 打开PowerDesign ...
- [mysql] Can't read from messagefile
系统:windows 重启mysql服务出现 Server] Can't read from messagefile 等错误时候, 应先执行 mysqld --initialize-insecure ...
- 【Python学习之八】ORM
ORM 什么是ORM呢? ORM全称是:Object-Relational Mapping.即对象-关系映射,就是把关系数据库的一行映射为一个对象,也就是一个类对应一个表.这样,写代码更简单,不用直接 ...