题目: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+质因数分解的更多相关文章

  1. CF 757 E Bash Plays with Functions —— 积性函数与质因数分解

    题目:http://codeforces.com/contest/757/problem/E 首先,f0(n)=2m,其中 m 是 n 的质因数的种类数: 而且 因为这个函数和1卷积,所以是一个积性函 ...

  2. 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 ...

  3. Codeforces E. Bash Plays with Functions(积性函数DP)

    链接 codeforces 题解 结论:\(f_0(n)=2^{n的质因子个数}\)= 根据性质可知\(f_0()\)是一个积性函数 对于\(f_{r+1}()\)化一下式子 对于 \[f_{r+1} ...

  4. 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 ...

  5. Makoto and a Blackboard CodeForces - 1097D (积性函数dp)

    大意: 初始一个数字$n$, 每次操作随机变为$n$的一个因子, 求$k$次操作后的期望值. 设$n$经过$k$次操作后期望为$f_k(n)$. 就有$f_0(n)=n$, $f_k(n)=\frac ...

  6. [Codeforces 757E] Bash Plays with Functions (数论)

    题目链接: http://codeforces.com/contest/757/problem/E?csrf_token=f6c272cce871728ac1c239c34006ae90 题目: 题解 ...

  7. D. Makoto and a Blackboard(积性函数+DP)

    题目链接:http://codeforces.com/contest/1097/problem/D 题目大意:给你n和k,每一次可以选取n的因子代替n,然后问你k次操作之后,每个因子的期望. 具体思路 ...

  8. 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)是积性函 ...

  9. 【codeforces 757E】Bash Plays with Functions

    [题目链接]:http://codeforces.com/problemset/problem/757/E [题意] 给你q个询问; 每个询问包含r和n; 让你输出f[r][n]; 这里f[0][n] ...

随机推荐

  1. mysql性能优化-慢查询分析、优化索引和配置 MySQL索引介绍

    MySQL索引介绍 聚集索引(Clustered Index)----叶子节点存放整行记录辅助索引(Secondary Index)----叶子节点存放row identifier-------Inn ...

  2. web前端面试系列 - js中的prototype

    js中一切皆为对象,其中函数也是一种对象, 而每个函数都有一个prototype属性,其值也是一个对象. 一.prototype的作用 1. 在多个实例对象之间共享数据和方法. 2. 通过原型链实现继 ...

  3. Android.mk: recipe commences before first target. Stop.

    [GUIDE] Setup Android Development Environment on Ubuntu 14.04 Trusty Tahr Hi All, This originally wa ...

  4. oracle死锁的检测查询及处理

    -- 死锁查询语句 SELECT bs.username "Blocking User", bs.username "DB User", ws.username ...

  5. 程序基石系列之C++多态的前提条件

    准备知识 C++中多态(polymorphism)有下面三个前提条件: 必须存在一个继承体系结构. 继承体系结构中的一些类必须具有同名的virtual成员函数(virtualkeyword) 至少有一 ...

  6. angular cannot get /

    每次遇到这问题都一脸懵逼,好像自己啥都没改咋就悲剧了 目前知道的办法是在命令行运行ng serve,它会告诉你详细错误 因为我是通过asp.net core的集成环境运行的,它没显示出详细错误

  7. Asp.net mvc4 快速入门之构建表单

    1.asp.net mvc4  Index.cshtml页面上构建表单form的方式 @{ ViewBag.Title = "Index"; Layout = "~/Vi ...

  8. React深入源码--了解Redux用法之Provider

    在Redux中最核心的自然是组件,以及组件相关的事件与数据流方式.但是我们在Redux中并没有采用传统的方式在getInitialState()中去初始化数据,而是采用Provider统一处理,省去了 ...

  9. aop学习总结二------使用cglib动态代理简单实现aop功能

    aop学习总结二------使用cglib动态代理简单实现aop功能 模拟业务需求: 1.拦截所有业务方法 2.判断用户是否有权限,有权限就允许用户执行业务方法,无权限不允许用户执行业务方法 (判断是 ...

  10. 给js设定一个统一的入口

    javascript是种脚本语言,浏览器下载到哪儿就会运行到哪儿,这样的特性会为编程提供方便,但也easy使程序过于凌乱.支离破碎. js从功能上能够分为两大部分--框架部分和应用部分,框架部分提供的 ...