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.
类似与dp的思想搞一下,对于每个数字看他的因子-1有多少个可以作为最终子序列的,注意不要马上加上去,因为小因子的结果会影响大因子,或者可以把因子从大往小计算,这样就类似于滚动数组处理背包的做法了。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int MAXN = 1e6 + 5;
vector<int> fac[MAXN];
#define ll long long
ll ans[N];
stack<pair<int, ll> > pre;
ll mod = 1e9 + 7;
ll a[N];
int main()
{
for (int i = 1; i <= 1000000; i++)
{
for (int j = 1; j * i <= 1000000; j++)
{
fac[i * j].push_back(i);
}
}
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
ans[0] = 1;
for (int i = 1; i <= n; i++)
{
for (auto &u: fac[a[i]])
{
if (u > n)
continue;
//printf("%d %lld\n", u, ans[u - 1]);
pre.push(make_pair(u, ans[u - 1]));
}
while (!pre.empty())
{
pair<int, ll> f = pre.top();
pre.pop();
ans[f.first] = (ans[f.first] + f.second) % mod;
}
}
ll sum = 0;
for (int i = 1; i <= n; i++)
sum = (sum + ans[i]) % mod;
printf("%lld\n", sum);
return 0;
}
本文介绍了一种算法,用于计算给定整数数组中所有良好的子序列数量,一个子序列被认为是良好的,如果它不为空且每个元素都能被其在序列中的位置整除,通过构建因子映射并使用类似动态规划的方法,我们能够有效地解决这个问题。
798

被折叠的 条评论
为什么被折叠?



