【JZOJ B組】斐波那契
阿新 • • 發佈:2018-11-01
Description
小明有一個數列。
a[0] = a[1] = 1。
a[i] = i * a[i - 1] * a[i - 2](i≥2)。
小明想知道a[n]的因子個數。
Input
輸入僅一個正整數n。
Output
輸出a[n]的因子個數mod 1,000,000,007的值。
Sample Input
3
Sample Output
4
Hint
【資料範圍】
對於30%的資料滿足0≤n≤1,000。
對於100%的資料滿足0≤n≤1,000,000。
思路
標題給了很大的提示
可以發現,a[n]=2f[n] * 3f[n-1] * 4f[n-2]
所以,把1–n分解質因數,再統計次數即可
程式碼
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int mod=1e9+7,maxn=1e6+77; int n,f[maxn],a[maxn]; bool b[maxn]; long long ans=1; int main() { scanf("%d",&n); f[1]=f[2]=1; for(int i=3; i<=n; i++) f[i]=(f[i-1]+f[i-2])%mod; for(int i=2; i<=n; i++) { if(b[i]) continue; b[i]=1; a[i]=f[n-i+1]; int j=i*2; while(j<=n) { b[j]=1; int x=j; while(x%i==0) { x/=i; a[i]=(a[i]+f[n-j+1])%mod; } j+=i; } } for(int i=2; i<=n; i++) ans=(1ll*ans*(a[i]+1))%mod; printf("%lld",ans); }