CF 757E Bash Plays with Functions——积性函数+dp+质因数分解
题目:http://codeforces.com/contest/757/problem/E
f0[n]=2^m,其中m是n的质因子个数(种类数)。大概是一种质因数只能放在 d 或 n/d 两者之一。
然后应该发现因为 f0 是积性的,所以 fr 也是积性的!因为是卷积得来的。
这样就能把每个质因数分开。对于每种质因数考虑 fr 的转移,则 f [ r ][ p^k ] = sigma(i:0~k) ( f [ r-1 ][ p^i ] ) 。
应该发现 f0 里每种质因数的值只和其次数有关,从转移可得出 f [ k ] 里的各种质因数的值也只和其次数有关!所以 dp 状态里只要记录次数就行。
学习到了质因数分解的更好而同样简单的方法。就是预处理mindiv,然后每次除以自己的mindiv。
先写了自己的原始方法,T了;于是怒写了个pollar rho,结果T得更快,难道是写错了?
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N=1e6,M=,mod=1e9+;
int q,r,n,dp[N+][M+],s[M+],ans,mindiv[N+],cnt,pri[N+];
bool vis[N+];
int rdn()
{
int ret=;bool fx=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')fx=;ch=getchar();}
while(ch>=''&&ch<='') ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return fx?ret:-ret;
}
void upd(int &x){x-=(x>=mod?mod:);}
void init()
{
mindiv[]=;//
for(int i=;i<=N;i++)
{
if(!vis[i])pri[++cnt]=i,mindiv[i]=i;
for(int j=;j<=cnt&&(ll)i*pri[j]<=N;j++)
{
int d=i*pri[j];
vis[d]=; mindiv[d]=pri[j];
if(i%pri[j]==)break;
}
}
}
int pw(int x,int k,int md)
{
int ret=;while(k){if(k&)ret=(ll)ret*x%md;x=(ll)x*x%md;k>>=;}return ret;
}
bool MR(int x)
{
if(x==)return true;
int s=,u=x-,t=;
while((u&)==)u>>=,t++;
while(s--)
{
int a=rand()%(x-)+;//2~x-1
a=pw(a,u,x);
for(int i=,d;i<=t;i++)
{
d=(ll)a*a%x;
if(d==&&a!=&&a!=x-)return false;
a=d;
}
if(a!=)return false;
}
return true;
}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int Pl_rho(int x,int c)
{
int x0=rand()%x,y=x,t=,k=;
while()
{
x0=((ll)x0*x0+c)%(x+);
int g=gcd(abs(x0-y),x);
if(g!=&&g!=x)return g;
if(x0==y)return x;
if(++k==t)t<<=,y=x0;
}
}
void fd_fc(int x)
{
if(x<)return;
if(MR(x))
{
int ret=;
while(n%x==)n/=x,ret++;
ans=(ll)ans*dp[r][ret]%mod;
return;
}
int p=x;
while(p==x)p=Pl_rho(p,rand()%(x-)+);//1~x-1
fd_fc(p); fd_fc(x/p);
}
int main()
{
dp[][]=;s[]=; init();
for(int i=;i<=M;i++)dp[][i]=,s[i]=s[i-]+;
for(int i=;i<=N;i++)
for(int j=;j<=M;j++)
dp[i][j]=s[j],s[j]=s[j-]+dp[i][j],upd(s[j]);
q=rdn();
while(q--)
{
r=rdn(); n=rdn(); ans=;
//fd_fc(n);
while(n!=)
{
int i=mindiv[n],d=;
while(n%i==)n/=i,d++;
ans=(ll)ans*dp[r][d]%mod;
}
/*
for(int i=mindiv[n],d;(ll)i*i<=n;i++)
if(n%i==0)
{
d=0;
while(n%i==0)d++,n/=i;
ans=(ll)ans*dp[r][d]%mod;
}
if(n>1)ans=(ll)ans*dp[r][1]%mod;
*/
printf("%d\n",ans);
}
return ;
}
CF 757E Bash Plays with Functions——积性函数+dp+质因数分解的更多相关文章
- CF 757 E Bash Plays with Functions —— 积性函数与质因数分解
题目:http://codeforces.com/contest/757/problem/E 首先,f0(n)=2m,其中 m 是 n 的质因数的种类数: 而且 因为这个函数和1卷积,所以是一个积性函 ...
- Codeforces757E.Bash Plays With Functions(积性函数 DP)
题目链接 \(Description\) q次询问,每次给定r,n,求\(F_r(n)\). \[ f_0(n)=\sum_{u\times v=n}[(u,v)=1]\\ f_{r+1}(n)=\s ...
- Codeforces E. Bash Plays with Functions(积性函数DP)
链接 codeforces 题解 结论:\(f_0(n)=2^{n的质因子个数}\)= 根据性质可知\(f_0()\)是一个积性函数 对于\(f_{r+1}()\)化一下式子 对于 \[f_{r+1} ...
- Bash Plays with Functions CodeForces - 757E (积性函数dp)
大意: 定义函数$f_r(n)$, $f_0(n)$为pq=n且gcd(p,q)=1的有序对(p,q)个数. $r \ge 1$时, $f_r(n)=\sum\limits_{uv=n}\frac{f ...
- Makoto and a Blackboard CodeForces - 1097D (积性函数dp)
大意: 初始一个数字$n$, 每次操作随机变为$n$的一个因子, 求$k$次操作后的期望值. 设$n$经过$k$次操作后期望为$f_k(n)$. 就有$f_0(n)=n$, $f_k(n)=\frac ...
- [Codeforces 757E] Bash Plays with Functions (数论)
题目链接: http://codeforces.com/contest/757/problem/E?csrf_token=f6c272cce871728ac1c239c34006ae90 题目: 题解 ...
- D. Makoto and a Blackboard(积性函数+DP)
题目链接:http://codeforces.com/contest/1097/problem/D 题目大意:给你n和k,每一次可以选取n的因子代替n,然后问你k次操作之后,每个因子的期望. 具体思路 ...
- Problem : 这个题如果不是签到题 Asm.Def就女装(积性函数dp
https://oj.neu.edu.cn/problem/1460 思路:若n=(p1^a1)*(p2^a2)...(pn^an),则f(n,0)=a1*a2*...*an,显然f(n,0)是积性函 ...
- 【codeforces 757E】Bash Plays with Functions
[题目链接]:http://codeforces.com/problemset/problem/757/E [题意] 给你q个询问; 每个询问包含r和n; 让你输出f[r][n]; 这里f[0][n] ...
随机推荐
- Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划
Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of ...
- HRBUST2030(dfs)
成语接龙 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %lld , %llu Java class nam ...
- python(25)- 面向对象补充Ⅰ
一.如何使用类 1.实例化:创建对象 类名加括号就是实例化,会自动触发__init__函数的运行,可以用它来为每个实例定制自己的特征. 例子一 x=int(10) print(x) python中一切 ...
- .net 反射访问私有变量和私有方法 如何创建C# Closure ? C# 批量生成随机密码,必须包含数字和字母,并用加密算法加密 C#中的foreach和yield 数组为什么可以使用linq查询 C#中的 具名参数 和 可选参数 显示实现接口 异步CTP(Async CTP)为什么那样工作? C#多线程基础,适合新手了解 C#加快Bitmap的访问速度 C#实现对图片文件的压
以下为本次实践代码: using System; using System.Collections.Generic; using System.ComponentModel; using System ...
- 利用CH341A编程器刷新BIOS,恢复BIOS,妈妈再也不用担心BIOS刷坏了
前几天,修电脑主析就捣鼓刷BIOS,结果刷完黑屏开不了机,立刻意识到完了,BIOS刷错了.就从网上查资料,各种方法试了个遍,什么用处都没有.终于功夫不负有心人,找到了编码器,知道了怎么用.下面看看具体 ...
- Centos7-搭建hdfs启动时报java.net.BindException: Problem binding to [node01:9000] java.net.BindException异常
今天用阿里的服务器搭了个伪分布式的HDFS,格式化后启动hdfs,发现只有dataNode启动了,查看启动日志发现异常: 2019-01-22 15:54:50,507 FATAL org.apach ...
- JavaScript读书笔记(1)
从今天开启每天看书记笔记模式,<JavaScript高级程序设计(第3版)> 1. Javascript最初是为了解决输入验证器的问题,现在已经发展成一门复杂的语言: 2. 语言标准为E ...
- Python 元祖、列表、字典、文件(转载)
转自http://yangsq.iteye.com/category/20857 python的元组.列表.字典数据类型是很python(there python is a adjective)的数据 ...
- 1367: [Baltic2004]sequence
1367: [Baltic2004]sequence Time Limit: 20 Sec Memory Limit: 64 MB Submit: 1090 Solved: 432 [Submit ...
- In Git, there are two main ways to integrate changes from one branch into another: the merge and the rebase.
https://git-scm.com/book/en/v2/Git-Branching-Rebasing