bzoj 3462: DZY Loves Math II
3462: DZY Loves Math II
Time Limit: 20 Sec Memory Limit: 512 MB
Submit: 211 Solved: 103
[Submit][Status][Discuss]
Description

Input
第一行,两个正整数 S 和 q,q 表示询问数量。
接下来 q 行,每行一个正整数 n。
Output
输出共 q 行,分别为每个询问的答案。
Sample Input
9
29
1000000000000000000
Sample Output
9
450000036
HINT
感谢the Loser协助更正数据
对于100%的数据,2<=S<=2*10^6,1<=n<=10^18,1<=q<=10^5
/**************************************************************
Problem: 3462
User: JYYHH
Language: C++
Result: Accepted
Time:5736 ms
Memory:141916 kb
****************************************************************/ #include<bits/stdc++.h>
#define ll long long
#define maxn 2000005
using namespace std;
const int ha=1000000007;
ll n;
int d[15],num=0,S,q,now;
int inv[20],f[2][maxn*9];
int zssum=0,ans; inline int add(int x,int y){
x+=y;
if(x>=ha) return x-ha;
else return x;
} inline void dp(){
f[0][0]=1;
now=0;
int pre=now,tp=S*num-zssum; for(int i=1;i<=num;i++){
now^=1; // memset(f[now],0,sizeof(f[now])); for(int j=0;j<d[i];j++){
int tot=0;
for(int u=j;u<=tp;u+=d[i]){
tot=add(tot,f[pre][u]);
if(u-j>=S) tot=add(tot,ha-f[pre][u-S]); f[now][u]=tot;
}
} pre=now;
}
} inline int C(ll x,int y){
int an=1;
for(int i=1;i<=y;i++) an=an*((ll)(x-i+1)%ha)%ha*(ll)inv[i]%ha;
return an;
} int main(){
inv[1]=1;
for(int i=2;i<=8;i++) inv[i]=-inv[ha%i]*(ll)(ha/i)%ha+ha; scanf("%d%d",&S,&q); int U=S;
for(int i=2;i*(ll)i<=U;i++) if(!(U%i)){
d[++num]=i,U/=i,zssum+=i;
if(!(U%i)){
while(q--) puts("0");
return 0;
}
}
if(U!=1) d[++num]=U,zssum+=U; dp(); while(q--){
ans=0; scanf("%lld",&n); if(n<zssum){
puts("0");
continue;
} n-=zssum; int tp=min((ll)num,n/S);
ll tt=n/S,lef=n-tt*S;
for(int i=0;i<=tp;i++) ans=add(ans,C(tt-i+num-1,num-1)*(ll)f[now][i*S+lef]%ha); printf("%d\n",ans);
} return 0;
}
bzoj 3462: DZY Loves Math II的更多相关文章
- BZOJ 3462 DZY Loves Math II ——动态规划 组合数
好题. 首先发现$p$是互质的数. 然后我们要求$\sum_{i=1}^{k} pi*xi=n$的方案数. 然后由于$p$不相同,可以而$S$比较小,都是$S$的质因数 可以考虑围绕$S$进行动态规划 ...
- ●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 ...
- BZOJ 3569 DZY Loves Chinese II
Description 神校XJ之学霸兮,Dzy皇考曰JC. 摄提贞于孟陬兮,惟庚寅Dzy以降. 纷Dzy既有此内美兮,又重之以修能. 遂降临于OI界,欲以神力而凌♂辱众生. 今Dzy有一魞歄图,其上 ...
- bzoj 3560 DZY Loves Math V - 线性筛 - 扩展欧几里得算法
给定n个正整数a1,a2,…,an,求 的值(答案模10^9+7). Input 第一行一个正整数n. 接下来n行,每行一个正整数,分别为a1,a2,…,an. Output 仅一行答案. Sampl ...
- BZOJ 3569 DZY Loves Chinese II 树上差分+线性基
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3569 Description 神校XJ之学霸兮,Dzy皇考曰JC. 摄提贞于孟陬兮,惟庚寅 ...
随机推荐
- [OpeCV] highgui头文件
通过HighGUI(high-level graphical user interface)可以实现打开窗口.显示图像.读出和写入图像文件,处理鼠标.光标.键盘事件. 而HighGUI主要分成“视频输 ...
- PHP页面跳转总结
一.使用php内置函数:header()函数 <?php$url='./test.php'; header("Location:$url"); ?> 注意Locatio ...
- HDU 3111 Sudoku ( Dancing Links 精确覆盖模型 )
推荐两篇学DLX的博文: http://bbs.9ria.com/thread-130295-1-1.html(这篇对DLX的工作过程演示的很详细) http://yzmduncan.iteye.co ...
- 201621123033 《Java程序设计》第3周学习总结
第三周作业 1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联系.步骤如下: 1 ...
- npm & npm config
npm command show npm config https://docs.npmjs.com/cli/config https://docs.npmjs.com/cli/ls https:// ...
- LeetCode -- Implement Stacks using Queue
Question: Implement the following operations of a queue using stacks. push(x) -- Push element x to t ...
- HDU 4910 HDOJ Problem about GCD BestCoder #3 第四题
首先 m = 1 时 ans = 0对于 m > 1 的 情况 由于 1 到 m-1 中所有和m互质的数字,在 对m的乘法取模 运算上形成了群 ai = ( 1<=a<m & ...
- BZOJ3609 [Heoi2014]人人尽说江南好 【博弈】
题目链接 BZOJ3609 题解 我们假设最后合成若干个\(m\),和\(n \mod m\),此时合成次数是最多的,也唯一确定胜利者 可以发现,在轮流操作的情况下,胜利者一定可以将终态变为这个状态 ...
- ES6特性以及代码demo
块级作用域let if(true){ let fruit = ‘apple’; } consoloe.log(fruit);//会报错,因为let只在if{ }的作用域有效,也就是块级作用域 恒量co ...
- Computer(hdu 2196)
题意:给出一棵树,求出每个点与距离它最远的点的距离. /* 树形DP 先把无根树转为有根树,对于一个节点i来说,与它相距最远的点有两种可能,一是在它的子树中,二是不在,我们分别用f[i][0]和f[i ...