【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]表示前i-1的序列中长度为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);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值