Bash Plays with Functions CodeForces - 757E (積性函數dp)
阿新 • • 發佈:2019-05-14
print space fin 定義 == long force mes pri
大意: 定義函數$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_{r-1}(u)+f_{r-1}(v)}{2}$.
$q$組詢問, 求$f_r(n)$的值模1e9+7.
顯然可以得到$f_0(n)=2^{\omega(n)}$, 是積性函數.
所以$f_r=f_{r-1}*1$也為積性函數, 然後積性函數$dp$即可.
問題就轉化為對每個素數$p$, 求$dp[p][r][k]=f_r(p^k)$.
$dp[p][r][k]=\sum\limits_{x=0}^k dp[p][r-1][x]$.
而$dp[p][0][k]=1$, 所以每個素數貢獻相同, 只需要$dp$一次即可.
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl ‘\n‘ #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<‘ ‘;hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<‘0‘||p>‘9‘)p=getchar();while(p>=‘0‘&&p<=‘9‘)x=x*10+p-‘0‘,p=getchar();return x;} //head const int N = 1e6+10; int dp[N][22], sum[N][22], mi[N]; int main() { dp[0][0]=sum[0][0]=1; REP(i,1,21) sum[0][i]=sum[0][i-1]+(dp[0][i]=2); REP(i,1,N-1) { dp[i][0]=sum[i][0]=1; REP(j,1,21) sum[i][j]=(sum[i][j-1]+(dp[i][j]=sum[i-1][j]))%P; } REP(i,1,N-1) mi[i] = i; REP(i,2,N-1) if (mi[i]==i) { for (int j=i; j<N; j+=i) mi[j]=min(mi[j],i); } int q; scanf("%d", &q); while (q--) { int r, n; scanf("%d%d", &r, &n); int ans = 1; while (n!=1) { int t = mi[n], k = 0; while (n%t==0) n/=t, ++k; ans = (ll)ans*dp[r][k]%P; } printf("%d\n", ans); } }
Bash Plays with Functions CodeForces - 757E (積性函數dp)