hdu5656 CA Loves GCD(gcd, dp)
题目链接: hdu5656 ( CA Loves GCD )
\(dp[i][j]\) 代表前 \(i\) 个数的所有组合中 \(gcd\) 为 \(j\) 的个数。
/**
* hdu5656 CA Loves GCD
*
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
LL gcd(LL a, LL b)
{
LL t;
while (b) {
t = a%b;
a = b;
b = t;
}
return a;
}
const int N = 1003;
const int mod = 1e8+7;
LL dp[N][N], g[N][N];
int main()
{
for (int i = 0; i <= 1000; ++i) {
for (int j = 0; j <= 1000; ++j) {
g[i][j] = gcd(i, j);
}
}
int T;
scanf("%d", &T);
for (int cas = 1; cas <= T; ++cas) {
int n;
memset(dp, 0, sizeof dp);
scanf("%d", &n);
dp[0][0] = 1;
for (int i = 0; i < n; ++i) {
int x;
scanf("%d", &x);
for (int j = 0; j <= 1000; ++j) {
int t = g[j][x];
dp[i+1][t] += dp[i][j];
dp[i+1][j] += dp[i][j];
dp[i+1][t] %= mod;
dp[i+1][j] %= mod;
}
}
LL ans = 0;
for (int j = 1; j <= 1000; ++j) ans += dp[n][j]*j;
printf("%lld\n", ans%mod);
}
return 0;
}

浙公网安备 33010602011771号