HDU 5656 CA Loves GCD dp,常数优化
2017-03-24 14:52
253 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5656
题意:
解法:
题意:
解法:
//HDU 5656 #include <bits/stdc++.h> using namespace std; const int maxn = 1050; const int mod = 100000007; int dp[maxn][maxn], a[maxn], n;///dp[i][j]表示在前i个数中,选出若干个数使得它们的gcd为j的方案数, ///于是只需要枚举第i+1个数是否被选中来转移就可以了 long long ans; int gcd(int a, int b){ return b==0?a:gcd(b, a%b); } void update(int &x, long long y) { x += y; if(x>=mod) x-=mod; } int main() { int t; scanf("%d", &t); while(t--) { memset(dp, 0, sizeof(dp)); dp[0][0] = 1; ans = 0; scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 0; i < n; i++){ for(int j = 0; j < maxn; j++){ if(dp[i][j]){//不加限制会T (dp[i+1][j] += dp[i][j])%=mod; (dp[i+1][gcd(j, a[i+1])] += dp[i][j])%=mod; } } } for(int i = 1; i < maxn; i++){ (ans += 1LL*i*dp [i]%mod)%=mod; } printf("%d\n", ans); } return 0; }
相关文章推荐
- HDU 5656 CA Loves GCD (数论DP)
- HDU 5656 CA Loves GCD (dp)
- hdu 5656 CA Loves GCD(dp)
- hdu 5656 CA Loves GCD(dp+gcd)(Bestcoder #78 1002)
- HDU 5656 CA Loves GCD dp
- HDU 5656 ——CA Loves GCD——————【dp】
- HDU 5656 CA Loves GCD (BestCoder Round #78) DP
- HDU 5656 CA Loves GCD (容斥)
- HDU 5656 CA Loves GCD 01背包+gcd
- hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)
- hdoj 5656 CA Loves GCD 【dp】
- HDU 5656 CA Loves GCD
- HDU 5656:CA Loves GCD
- HDU 5656 CA Loves GCD
- hdu CA Loves GCD dp
- CA Loves GCD (BC#78 1002) (hdu 5656)
- hdu 5656 CA Loves GCD(穷举子集类dp)
- 数学(GCD,计数原理)HDU 5656 CA Loves GCD
- HDU 5656 CA Loves GCD 01背包+gcd
- hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)