[codeforces1061C]Multiplicity

版权声明:辛辛苦苦码字,你们转载的时候记得告诉我 https://blog.csdn.net/dxyinme/article/details/84402531

C. Multiplicity
time limit per test : 3 seconds
memory limit per test : 256 megabytes

You are given an integer array a 1 , a 2 , , a n a_1,a_2,…,a_n .
The array b b is called to be a subsequence of a if it is possible to remove some elements from a to get b b .Array b 1 , b 2 , , b k b_1,b_2,…,b_k is called to be good if it is not empty and for every i ( 1 i k ) b i i (1≤i≤k) b_i is divisible by i i . Find the number of good subsequences in amodulo 1 0 9 + 7 10^9+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 a a has exactly 2 n 1 2^n−1 different subsequences (excluding an empty subsequence).

Input

The first line contains an integer n ( 1 n 100000 ) n(1≤n≤100000) — the length of the array a a .
The next line contains integers a 1 , a 2 , , a n ( 1 a i 1 0 6 ) a_1,a_2,…,a_n(1≤a_i≤10^6) .
Output
Print exactly one integer — the number of good subsequences taken modulo 1 0 9 + 7 10^9+7
Examples
Input

2
1 2

Output

3

Input

5
2 2 1 22 14

Output

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}, {2,22}, {2,14}, {1}, {1,22}, {1,14}, {22}, {22,14}, {14}.
Note, that some subsequences are listed more than once, since they occur in the original array multiple times.

题意:
给定一个数组{ a k a_k },问这个数组的所有子序列{ b k b_k }中,有多少子序列满足:对于所有的 i ( 1 < = i < = k ) i(1<=i<=k) 满足 b i b_i i i 的倍数,答案对 1 0 9 + 7 10^9+7 取模

题解:
先处理出 a [ i ] a[i] 的所有因数。
接着就是DP
f [ i ] [ j ] f[i][j] 表示从 1 1 ~ i i 前面所有序列中,长度为 j j 的方案数
所以 f [ i ] [ j ] = ( f [ i ] [ j ] + f [ i ] [ j 1 ] ) f[i][j]=(f[i][j]+f[i][j-1]) % M O D MOD
j j 可以通过枚举 a [ i ] a[i] 的因数得到
然后我们发现可以把 i i 这个维度压掉…

#include<bits/stdc++.h>
#define LiangJiaJun main
#define ll long long
#define MOD 1000000007
using namespace std;
vector<int>v[100004];
int n,f[1000004];
int w33ha(){
    memset(f,0,sizeof(f));
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        v[i].clear();
        for(int j=1;j<=sqrt(x);j++){
            if(x%j==0){
                v[i].push_back(j);
                if(x!=j*j)v[i].push_back(x/j);
            }
        }
        sort(v[i].begin(),v[i].end());
    }
    ll ans=0;
    f[0]=1;
    for(int i=1;i<=n;i++){
        for(int j=v[i].size()-1;j>=0;j--){
            f[v[i][j]]=(f[v[i][j]]+f[v[i][j]-1])%MOD;
            ans=(ans+f[v[i][j]-1])%MOD;
        }
    }
    printf("%d\n",ans);
    return 0;
}
int LiangJiaJun(){
    while(scanf("%d",&n)!=EOF)w33ha();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/dxyinme/article/details/84402531