1. 程式人生 > >【Codeforces Round #523(Div. 2)】Multiplicity(dp)

【Codeforces Round #523(Div. 2)】Multiplicity(dp)

題目連結

C. Multiplicity

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

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

Input

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

Output

Print exactly one integer — the number of good subsequences taken modulo 109+7109+7.

Examples

input

Copy

2
1 2

output

Copy

3

input

Copy

5
2 2 1 22 14

output

Copy

13

Note

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.

 

【題意】

給定一陣列a[],從a[ ]中除去任意個元素得到b[ ],求能形成多少“好序列”;好序列的定義是:對於任意的 i 有 b[i]%i == 0(1 ≤ i ≤ size_b[ ])。

【解題思路】

設dp[i][j]表示前1-i的序列中長度為j的方案數。v[i]表示第i個數的因子,並將因子從小到大排序。易得每個數只有當序列長度剛好達到其擁有的因子時才能構成好序列。

所以dp[i][j]=dp[i-1][j]+dp[i-1][j-1] 當j是a[i]的因子時;否則 dp[i][j]=dp[i-1][j]

所以因子從小到大排序就很重要啦,因為要將二維陣列轉變為一維的滾動陣列,否則會爆記憶體的哦。

【程式碼】

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
const int mod=1e9+7;
typedef long long LL;
vector<int>v[maxn];
LL dp[maxn],a[maxn];
int main()
{
    LL n;
    scanf("%lld",&n);
    for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=sqrt(a[i]);j++)
        {
            if(a[i]%j==0)
            {
                v[i].push_back(j);
                if(a[i]!=j*j)v[i].push_back(a[i]/j);//例如7*8,避免8沒有存入
            }
        }
        sort(v[i].begin(),v[i].end());
    }
    dp[0]=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=v[i].size()-1;j>=0;j--)
        {
            dp[v[i][j]]+=dp[v[i][j]-1];
            dp[v[i][j]]%=mod;
        }
    }
    LL ans=0;
    for(int i=1;i<=n;i++)ans+=dp[i];
    printf("%lld\n",ans%mod);
}