3 seconds
256 megabytes
standard input
standard output
You are given an integer array a1,a2,…,ana1,a2,…,an.
The array bb is called to be a subsequence of aa if it is possible to remove some elements from aa to get bb.
Array b1,b2,…,bkb1,b2,…,bk is called to be good if it is not empty and for every ii (1≤i≤k1≤i≤k) bibi is divisible by ii.
Find the number of good subsequences in aa modulo 109+7109+7.
Two subsequences are considered different if index sets of numbers included in them are different. That is, the values of the elements do not matter in the comparison of subsequences. In particular, the array aa has exactly 2n−12n−1 different subsequences (excluding an empty subsequence).
The first line contains an integer nn (1≤n≤1000001≤n≤100000) — the length of the array aa.
The next line contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106).
Print exactly one integer — the number of good subsequences taken modulo 109+7109+7.
input
2 1 2
output
3
input
5 2 2 1 22 14
output
13
In the first example, all three non-empty possible subsequences are good: {1}{1}, {1,2}{1,2}, {2}{2}
In the second example, the possible good subsequences are: {2}{2}, {2,2}{2,2}, {2,22}{2,22}, {2,14}{2,14}, {2}{2}, {2,22}{2,22}, {2,14}{2,14}, {1}{1}, {1,22}{1,22}, {1,14}{1,14}, {22}{22}, {22,14}{22,14}, {14}{14}.
Note, that some subsequences are listed more than once, since they occur in the original array multiple times.
Intention: Give you a sequence of length n and find all the number of subsequences that make all ai%i==0.
Answer: Obviously a topic of dynamic programming, using d[i][j] to represent the subsequence size of length j (j must be a factor of ai) in the ith number of the original sequence. The first dimension can be represented by a reverse-order scrolling array of j. It is worth mentioning that you can use the representative to find all the factors. (Harmonic series [n*调和级数], complexity (nlogn))
Code:
#include<iostream> #include<stdio.h> #include<algorithm> #include<vector> using namespace std; typedef long long ll; const int maxn = 1e6+5; const int Mod=1000000007; ll dp[maxn]; int a[maxn]; vector<int> v[maxn]; void init() { for(int i=1;i<=1000000;i++) { for(int j=i;j<=1000000;j+=i) { v[j].push_back(i); } } } int main() { init(); int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); dp[0]=1; for(int i=1;i<=n;i++) { for(int j=v[a[i]].size()-1;j>=0;j--) { int tmp=v[a[i]][j]; dp[tmp]=(dp[tmp]+dp[tmp-1])%Mod; } } ll ans=0; for(int i=1;i<=1000000;i++) ans=(ans+dp[i])%Mod; printf("%I64d\n",ans); return 0; }