Mike and gcd problem CodeForces - 798C】的更多相关文章

题目链接 比较棒的一道题, 题意: 给你一个N个数的数组,让你用尽量少的操作使整个数组的gcd大于1,即gcd(a1 ,a2,,,,an) > 1 如果可以输出YES和最小的次数,否则输出NO 首先我们来看一下这个操作, 如果对   a b 老两个数进行操作 第一次为 a-b a+b 第二次为 -2b  2a 由此可见,任何两个数最多进行两次操作,可以让他们都能被2整除. 所以也就没有NO的情况. 那么我们只需要预处理一下gcd,如果>1了,直接输出0次. gcd=1的话,那么就需要我们去处理…
题目 (智商题 or 糟心的贪心) 题意: 有一个数列a1,a2,...,an,每次操作可以将相邻的两个数x,y变为x-y,x+y,求最少的操作数使得gcd(a1,a2,...,an)>1.gcd(a1,...,an)表示最大的非负整数使得所有ai都能被gcd(a1,...,an)整除. 分析: 首先,如果原来gcd就不是1那么答案就是0.如果gcd是1:相邻两数x和y的变化方法为:x,y=>x-y,x+y设新的gcd值为d,那么x-y和x+y能被d整除,因此2x和2y能被d整除,因此d最大是…
C. Mike and gcd problem time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input output: standard output Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful…
题目连接:http://codeforces.com/contest/798/problem/C C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mike has a sequence A = [a1, a2, ..., an] of length n. He considers…
C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<cmath> using namespace std; ; int a[maxn]; int gcd(…
题目:Mike and gcd problem 题意:给一个序列a1到an ,如果gcd(a1,a2,...an)≠1,给一种操作,可以使ai和ai+1分别变为(ai+ai+1)和(ai-ai+1);问需要执行几次这个操作才能使得gcd(a1,a2,...an)>1. 分析: 1.首先,答案总是YES. 2,假设gcd(a1,a2,...an)=1,在一次对ai和ai+1的操作后新的gcd为d,则d满足:d|ai - ai + 1 and d|ai + ai + 1  d|2ai and d|2…
/* CF798 C. Mike and gcd problem http://codeforces.com/contest/798/problem/C 数论 贪心 题意:如果一个数列的gcd值大于1,则称之为美丽数列 给出数列a,可以将a_i 和 a_(i+1)换为其差和其和 如果可以变为美丽数列,输出YES并输出最少的变换次数 否则输出NO 思路: 如果变换a b a b -> a-b a+b -> -2b 2a 因此变换两个可以把a b乘以2 而若a b都是奇数,只需变换一次 所以每次先…
C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if…
题目链接:http://codeforces.com/problemset/problem/798/C 题意:给你n个数,a1,a2,....an.要使得gcd(a1,a2,....an)>1,可以执行一次操作使ai,ai+1变为ai - ai + 1, ai + ai + 1.求出使得gcd(a1,a2,....an)>1所需要的最小操作数. 解题思路:首先,要知道如果能够实现gcd(a1,a2,....an)>1,那么a1~an肯定都是偶数(0也是偶数),所以我们的目的就是用最少的操…
[题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 a[i]变为a[i]-a[i+1],a[i+1]变为a[i]+a[i+1]; 然后问你能不能通过以上变换使得最后所有元素的gcd>1 [题解] 答案总是存在的; 因为把这个操作进行两次可以得到-2*a[i+1],2*a[i] 也就是如果对每相邻的元素进行操作,最后都能使得这两个元素都变成偶数; 最后…
题意: 给出一个数列,现在有一种操作,可以任何一个a[i],用a[i] – a[i+1]和a[i]+a[i+1]替代a[i]和a[i+1]. 问现在需要最少多少次操作,使得整个数列的gcd大于1. 思路: 经过思考后发现,除非所有的数的gcd已经大于1,那么就必须把全部数字变为偶数. 变数字的时候,必须从第一个不是偶数的数字开始变化,每次都从下标最小的为奇数的数字开始变化. 如果是奇数,奇数,那么显然经过一次就可以全部变为偶数: 如果是奇数,偶数,那么必须经过两次才能全部变成偶数. 这题实际是贪…
题目意思:给出一个n个数的序列:a1,a2,...,an (n的范围[2,100000],ax的范围[1,1e9] ) 现在需要对序列a进行若干变换,来构造一个beautiful的序列: b1,b2, ..., bn,使得最大公约数 gcd(b1,b2,...,bn) > 1. 变换:  任意ai,ai+1 进行一次操作时,可以用 ai-ai+1, ai+ai+1 来替换. 问序列 a 构造成 序列 b ,使得gcd(b序列) > 1 的最小操作次数 题目解析: 首先,这个题目是肯定有解的,也…
题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 并且把 ai - a(i + 1), ai + a(i + 1) 放入原来的位置.问是否能够在几步操作后使得串的gcd大于1然后要求最小的操作数. 题解:偶数=偶数*偶数 or 奇数*偶数,奇数=奇数*奇数. 如果整个字符串全是偶数的话肯定gcd是大于1的.介于题目要求的操作,奇数-(or)+奇数=…
如果一开始就满足题意,不用变换. 否则,如果对一对ai,ai+1用此变换,设新的gcd为d,则有(ai - ai+1)mod d = 0,(ai + ai+1)mod d = 0 变化一下就是2 ai mod d = 0 2 ai+1 mod d = 0 也就是说,用两次变换之后,gcd至少扩大2倍,于是,最优方案就是我们将所有的奇数都变成偶数. 只需要找出所有奇数段,答案就是sigma([奇数段的长度/2]+(奇数段的长度 mod 2 ==1 ?)). #include<cstdio> #i…
原题链接:http://codeforces.com/contest/798/problem/C 题意:有一个数列A,gcd(a1,a2,a3...,an)>1 时称这个数列是“漂亮”的.存在这样的操作,使ai,ai+1变为(ai-ai+1), (ai+ai+1).问最少进行这样的操作使数列是“漂亮”的. 思路:考虑gcd(a1,a2,a3...,an)>1 的情况: 我们对ai,ai+1进行两次操作可以得到2ai,2ai+1,也就是说一对相邻的数字最多操作2次使它们的gcd=2>1.而…
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beautiful. In one move he c…
思路: 首先如果数列的最大公约数大于1,直接输出即可. 否则,设对原数列中的ai和ai+1进行一次操作,分别变为ai - ai+1和ai + ai+1.设新数列的最大公约数为d,则由于d|(ai - ai+1)并且d|(ai + ai+1)得到d|(2ai)且d|(2ai+1).则d|gcd(a1, a2, ..., 2ai, 2ai+1, ai+2, ..., an)|2gcd(a1, a2, ..., an) = 2.说明进行一次这样的操作最多可以把最大公约数变为原来的2倍.所以我们的目标就…
嗯... 题目链接:https://www.luogu.org/problemnew/show/CF798C 这道题首先要会写gcd..也类似一种找规律吧... 问题的操作是在两个数的基础上进行的: 那么我们不妨只考虑两个数的操作,手写几组数据不难发现,所有写出来的两个数A.B,都会在至多两次操作内完成任务.那么我们可以考虑其性质: 两个数A.B.无非四种情况: 奇数,奇数--------------->操作后变成       偶数,偶数 奇数,偶数--------------->操作后变成…
Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him an interesting geometry problem. Let's define f([l, r]) = r - l + 1…
E. Mike and Geometry Problem 题目连接: http://www.codeforces.com/contest/689/problem/E Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him an interesting geometry problem. Let's define f([l, r]) = r - l + 1 to…
任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Mike wants to prepare for IMO but he doesn't know geometry, so…
E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him an interesting geometry pro…
题目链接: E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him an interesting geomet…
J - Military Problem CodeForces - 1006E 就是一道dfs序的问题 给定一个树, 然后有q次询问. 每次给出u,k, 求以u为根的子树经过深搜的第k个儿子,如果一个节点有多个儿子,按照儿子从小到大的顺序,依次访问,不存在则输出-1. 预处理记录一下每个节点的出入时间 最后每次query直接判断就好了 #include <cstdio> #include <algorithm> #include <vector> using names…
B - Save the problem! CodeForces - 867B 这个题目还是很简单的,很明显是一个构造题,但是早训的时候脑子有点糊涂,想到了用1 2 来构造, 但是去算这个数的时候算错了... 用1 2 来构造 可以先枚举一些数来找找规律. 1 1 2 2 3 1 1 1    2 1 1 4 .... 可以发现每一个数都是 n/2+1 的可能, 所以反过来推过去就是 (s-1)*2  或者(s-1)*2+1 这个(s-1)*2+1的答案才是正确答案 因为 这个s可以==1 #i…
原题: Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him an interesting geometry problem. Let's define f([l, r]) = r - l + 1 to be the number of integer points in the segment [l, r] with l ≤ r (say that ). Y…
https://codeforces.com/contest/1174/problem/E dp 好题 *(if 满足条件) 满足条件 *1 不满足条件 *0 ///这代码虽然写着方便,但是常数有点大 #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <string> #include <algorithm> #inclu…
给定一个函数: f([l,r]) = r - l + 1; f(空集) = 0; 即f函数表示闭区间[l,r]的整点的个数 现在给出n个闭区间,和一个数k 从n个区间里面拿出k个区间,然后对这k个区间求并集,并求并集的f函数值 求所有C(n,k)种方案的f函数值之和 1 <= k <= n <= 200000 -10^9 <= l <= r <= 10^9 思路: 思路其实很容易想到 对这些区间缩点 g(i) 表示i这个点代表的区间的点数(即点i实际的点数) s(i)…
离散化,树状数组,组合数学. 这题的大致思路和$HDU$ $5700$一样.都是求区间交的问题.可以用树状数组维护一下. 这题的话只要计算每一个$i$被统计了几次,假设第$i$点被统计了$ans[i]$次,累加和就是答案. $ans[i]$就是看$i$点之后有多少个区间右端点,假设有$m$个,那么$ans[i]$就等于$m$个里面选$k$个的方案数. 因为数据中$L$,$R$范围较大,所以需要先离散化,计算离散化之后的情况,然后再统计离散化之前的情况. #pragma comment(linke…
题目链接:传送门 题目大意:给你n个区间,求任意k个区间交所包含点的数目之和. 题目思路:将n个区间都离散化掉,然后对于一个覆盖的区间,如果覆盖数cnt>=k,则数目应该加上 区间长度*(cnt与k的组合数) ans=ans+(len*C(cnt,k))%mod; #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorith…