UESTC 923 稳住GCD DP + GCD】的更多相关文章

定义:dp[i][j] 表示 在前i个数中,使整个gcd值为j时最少取的数个数. 则有方程: gg = gcd(a[i],j) gg == j : 添加这个数gcd不变,不添加,  dp[i][j] = dp[i-1][j] gg != j: t添加,更新答案,                dp[i][gg] = dp[i-1][j] + 1 最后答案为dp[n][g] (g为原始的所有数的gcd) 时间复杂度: O(n*max(a[i])) 代码: #include <iostream>…
Play with GCD 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/play-with-gcd Description Minka is very smart kid who recently started learning computer programming. He learned how to calculate the Greatest Common Divisor (GC…
GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2742    Accepted Submission(s): 980 Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). There ar…
分析:对于区间[i,j],枚举j. 固定j以后,剩下的要比较M_gcd(k,j) = gcd(ak,...,aj)*(j-k+1)的大小, i≤k≤j. 此时M_gcd(k,j)可以看成一个二元组(g, k). 根据gcd的性质gcd(a1,a2,...,an) = gcd(a1,gcd(a2,..,an)),而且gcd(a,b) | b. 如果gcd(ak,...,aj) != gcd(ak+1,...,aj),那么gcd(ak,...,aj) ≤ 2*gcd(ak+1,...,aj). 原本…
http://acm.uestc.edu.cn/#/problem/show/923 给定一堆数字,求其所有数字的gcd. 现在要删除最多的数字,使得剩下的数字的gcd和原来的一样. 设dp[i][val]表示在前i个数中,得到val这个数字所需的最小数字,怎么得到val这个数字?就是gcd得到. dp[i][gcd] 然后转移就是 dp[i][a[i]] = 1是必然的,自己一个 枚举新数字得到新的gcd  val dp[i][val] = min(dp[i][val], dp[i - 1][…
题目: 聪明的猴子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1123 Accepted Submission(s): 294 Problem Description 森林中有一排香蕉树(无限长),一只猴子站在其中一棵树上,猴子在跳跃前要先抽取一张卡片,卡片上写有A+1个自然数,其中最后一个是B,前A个数只能小于等于B,卡片上的数字可以…
D. Array GCD 题目连接: http://codeforces.com/contest/624/problem/D Description You are given array ai of length n. You may consecutively apply two operations to this array: remove some subsegment (continuous subsequence) of length m < n and pay for it m·…
树形dp用一下就好了 /* dp[i]表示不删节点的gcd值 每个结点开个vector用来存储删一个点之后的最大值 然后排序 去重 */ #include<bits/stdc++.h> #include<vector> using namespace std; #define maxn 200005 ]; int n,a[maxn],head[maxn],tot,dp[maxn]; vector<int>vec[maxn]; void init(){ memset(he…
一道我想骂人的题,差点把我气炸了. 题意: 求一个数的集合中(非多重集,每个数只出现一次)所有子集的gcd的和.结果MOD10^8+7输出. 输入输出不说了,自己看吧,不想写了. 当时我真把它当作数论题来写了,以为可以推导出什么公式然后化简大量重复的操作的.结果最后也没找到.最后题解说是dp,我同学说是暴力,吐血10升. 然后弄出来dp方程之后还是反复的wa,方程明明没啥问题,愣是卡了2个小时找不出错误,心情烦躁的要命,坑爹的室友还各种看视频打游戏,还不带耳机,我自己只好带着耳机大声放音乐,最后…
CA Loves GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5656 Description CA is a fine comrade who loves the party and people; inevitably she loves GCD (greatest common divisor) too. Now, there are N different numbers. Each time, CA will select s…