题目链接:
C - Multiplicity |
大意:给n个整数的数列,你可以从中去掉一些数得到一个新的数列,新的数列是好数列当对于每一个i,a[i] %i == 0。问你有多少个好数列
解题思路:dp求解,二维的dp很好想,dp[i][j]表示前i个数中长度为j的好数列的个数,最后的答案就是sum(i:1->n)dp[n][i]
更新也很好想到
但是这样会MLE。注意到其实第一维是可以压缩掉的,那么我们更新的时候只在j为a[i]的因数才更新,所以对于一个数可以先预处理出来。即对于每一个因子dp[j] += dp[j-1]
AC Code
#pragma GCC diagnostic error "-std=c++11"
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define gcd __gcd
#define pb push_back
#define mp make_pair
#define lowbit(x) x & (-x)
#define PII pair<int, int>
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for(__typeof(b) i = a; i <= (b); i++)
#define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 7;
const int maxn = (int)1e5 + 5;
using namespace std;
int dp[(int)1e6+5];
int main()
{
int n; scanf("%d", &n);
dp[0] = 1;
rep(i, 1, n){
int x; scanf("%d", &x);
vector<int> v;
int m = sqrt(x);
rep(j, 1, m){
if(x % j == 0){
v.pb(j);
if(j * j != x) v.pb(x / j);
}
}
sort(all(v), greater<int>());
for(auto &j : v){
dp[j] = (dp[j] + dp[j-1]) % mod;
}
}
ll ans = 0;
rep(i, 1, n){
ans = (ans + dp[i]) % mod;
}
printf("%lld\n", ans);
return 0;
}
over