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,然后可以移除一些元素变成数组b,这个数组b有个规定是:如果第i个元素可以整除i,那么这个数组b就是好的,现在问数组a可以构造出多少个数组b.
解析:
dp[i][j]代表前i个数构成长度为j的b数组的方案数目
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1]
用滚动数组优化空间
dp[j] = dp[j] + dp[j - 1]
如果当前数字能整除x,她一定可以放在所有长度为x-1的子序列的后面,但是这样会有本层状态影响,所以因子从大到小dp,之后统计所有长度的合法子序列之和就可以了
ac:
#include<bits/stdc++.h>
#define ll long long
#define MAXN 100005
using namespace std;
const int mod =1e9+7;
int a[MAXN];
ll dp[MAXN];
vector<int> p[MAXN*10];
void init()//预先处理因子
{
for(int i=1;i<=MAXN*10;i++)
{
for(int j=i;j<=MAXN*10;j+=i)
{
p[j].push_back(i);
}
}
}
int main()
{
int n;
init();
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
int m=p[a[i]].size();
for(int j=m-1;j>=0;j--)
{
int t=p[a[i]][j];
if(t>i) continue;
if(t==1)
dp[1]=(dp[1]+1)%mod;
else
dp[t]=(dp[t]+dp[t-1])%mod;
}
}
ll ans=0;
for(int i=1;i<=n;i++)
{
ans=(ans+dp[i])%mod;
}
printf("%I64d\n",ans);
return 0;
}