一道我想骂人的题,差点把我气炸了。

题意:

求一个数的集合中(非多重集,每个数只出现一次)所有子集的gcd的和。结果MOD10^8+7输出。

输入输出不说了,自己看吧,不想写了。

当时我真把它当作数论题来写了,以为可以推导出什么公式然后化简大量重复的操作的。结果最后也没找到。最后题解说是dp,我同学说是暴力,吐血10升。

然后弄出来dp方程之后还是反复的wa,方程明明没啥问题,愣是卡了2个小时找不出错误,心情烦躁的要命,坑爹的室友还各种看视频打游戏,还不带耳机,我自己只好带着耳机大声放音乐,最后连音乐都听不下去了,恶心的想吐。

后来实在无奈了查了下题解,但是没人用dp写,有个用莫比乌斯反演的orz,还有个用暴力的,不过其实有dp的思想在里面。当然这不重要,重要的是我看见了他MOD加的位置挺有意思的,然后猛然想到我的int爆了!因为需要一个小于10^8的数×一个小于1000的数,这个数有可能爆!我叉!特么这不是故意卡int的意思吗?最后把这个改了终于过了……此时距离比赛结束已经5个小时了,我*!

状态转移方程:

dp[i][a[i]] += 1;

dp[i][j] += dp[i-1][j];

dp[i][gcd[j][a[i]]] += dp[i-1][j];

其中gcd[][]是预处理离线出来的,要不然可能会超时。

状态dp[i][j]表示在前n个数的集合中,gcd为j的集合有多少个。

方程表示三种情况:

  1. 只有a[i]的集合。
  2. 不存在a[i],只存在前i-1个数中若干数的集合。
  3. 存在a[i],且存在前i-1个数中若干数的集合。

时间复杂度为O(n*maxn),其中maxn为a[]数组中的最大值。

具体见代码——

 #include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long const int N = ;
const int Mod = ; int a[N];
LL dp[N][N];
int gcd[N][N];
int t, n; int Gcd(int x, int y)
{
if(x < y)
{
int t = x;
x = y;
y = t;
}
while(y != )
{
int t = y;
y = x%y;
x = t;
}
return x;
} void Table()
{
for(int i = ; i < ; i++)
{
for(int j = ; j <= i; j++)
{
gcd[i][j] = gcd[j][i] = Gcd(i, j);
}
}
} int main()
{
//freopen("test.in", "r", stdin);
Table();
scanf("%d", &t);
for(int tm = ; tm <= t; tm++)
{
scanf("%d", &n);
int maxn = ;
for(int i = ; i < n; i++)
{
scanf("%d", &a[i]);
maxn = maxn > a[i] ? maxn : a[i];
}
memset(dp, , sizeof(dp));
dp[][a[]] = ;
for(int i = ; i < n; i++)
{
dp[i][a[i]] += ; //转移方程1
for(int j = ; j <= maxn; j++)
{
dp[i][j] += dp[i-][j]; //转移方程2
dp[i][gcd[j][a[i]]] += dp[i-][j]; //转移方程3
dp[i][j] %= Mod;
dp[i][gcd[j][a[i]]] %= Mod;
}
}
int ans = ;
for(int i = ; i <= maxn; i++)
{
ans += (dp[n-][i]*i)%Mod; //这里小心dp如果是int可能会爆
ans %= Mod;
} printf("%d\n", ans);
}
return ;
}

自己确实挺弱的,还需要努力,但是今天确实非常烦!所有认为这些没什么好烦的,都是因为他没有身临其境的感觉。

hdu CA Loves GCD(dp)的更多相关文章

  1. HDU 5656 CA Loves GCD dp

    CA Loves GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5656 Description CA is a fine comrade w ...

  2. hdu-5656 CA Loves GCD(dp+数论)

    题目链接: CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)     Memory Limit: 262144/262144 K (Java/Ot ...

  3. HDU 5656 CA Loves GCD (数论DP)

    CA Loves GCD 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/B Description CA is a fine c ...

  4. HDU 5656 ——CA Loves GCD——————【dp】

    CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  5. hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)

    CA Loves GCD  Accepts: 64  Submissions: 535  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: 2 ...

  6. HDU 5656 CA Loves GCD 01背包+gcd

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5656 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  7. CA Loves GCD (BC#78 1002) (hdu 5656)

    CA Loves GCD  Accepts: 135  Submissions: 586  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: ...

  8. 数学(GCD,计数原理)HDU 5656 CA Loves GCD

    CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 2621 ...

  9. hdu 5656 CA Loves GCD

    CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

随机推荐

  1. Codeforces Round #540 Tanya and Candies 预处理

    http://codeforces.com/contest/1118/problem/B 题目大意,给你一个序列,删去一个数值之后,要求剩下序列奇数和偶数的和相同,问有多少种删法. 思路:预处理奇数和 ...

  2. 干货:制作科研slide简明规范

  3. [整]Android SlidingMenu Demo 环境搭建

    1. 下载ActionBarSherlock https://github.com/JakeWharton/ActionBarSherlock 2. 下载SlidingMenu https://git ...

  4. shape-outside 矩形之外的另一种思路

    http://docs.webplatform.org/wiki/css/properties/shape-outside

  5. 用原生js对表格排序

    阿里的模拟笔试题,当时时间有限没写出来,其实是因为自己对原生dom操作不熟悉,这里补一下. 题目的大意是有一个表格,如代码所示 <table> <tr> <th>N ...

  6. lucene删除索引——(五)

    增加在入门程序创建索引中,增删改用IndexWriter. 1.获取IndexWriter的代码 // public IndexWriter getIndexWriter() throws Excep ...

  7. 【ARTS】01_02_左耳听风-20181119~1125

    Algorithm 做一个 leetcode 的算法题 Unique Email Addresses https://leetcode.com/problems/unique-email-addres ...

  8. js async await 终极异步解决方案

    既然有了promise 为什么还要有async await ? 当然是promise 也不是完美的异步解决方案,而 async await 的写法看起来更加简单且容易理解. 回顾 Promise Pr ...

  9. 005_ss-link.info的ping探测工具

    用小工具ping.py测试距离您最快的节点 #!/usr/bin/env python # coding: utf-8 """ A pure python ping im ...

  10. 数组slice方法

    slice slice(start,end):方法可从已有数组中返回选定的元素,返回一个新数组,包含从start到end(不包含该元素)的数组元素.(不会改变原数组) start参数:必须,规定从何处 ...