题目链接:https://codeforces.com/contest/1152/problem/C 题意:给定a,b(<1e9).求使得lcm(a+k,b+k)最小的k,若有多个k,求最小的k.(k>=0) 思路:昨晚打cf因为某些原因,沉不下心来看题,本来是个上分的好机会QAQ...所以吸取教训,下次状态好的时候再打比赛.    回到题目,首先给出gcd(a,b)=gcd(a,a-b),这个很显然,所以有gcd(a+k,b+k)=gcd(a+k,a-b).而lcm(a+k,b+k)=(a+…
题目链接:https://codeforces.com/contest/1152/problem/C 题目大意:给定两个正整数a,b,其中(1<=a,b<=1e9),求一个正整数k(0<=k),使得a+k与b+k的最小公倍数最小. 解题思路:首先我们需要知道gcd(a,b)=gcd(a,b-a)=gcd(b,b-a)(b>a)的 我们要求的是lcm(a+k,b+k)=(a+k)(b+k)/gcd(a+k,b+k)=(a+k)(b+k)/gcd(a+k,b-a) 因为b-a是定值,所…
传送门 题意: 给出两个整数a,b: 求解使得LCM(a+k,b+k)最小的k,如果有多个k使得LCM()最小,输出最小的k: 思路: 刚开始推了好半天公式,一顿xjb乱操作: 后来,看了一下题解,看到一个引理: GCD(a,b) = GCD(a,b-a) = GCD(b,b-a)(b > a) 假设GCD(a,b) = c; a%c = ; b%c = ; 那么(b-a)%c = ; 这证明了a和(b-a),b和(b-a)有公约数c; 假设GCD(a,b-a)=c' > c; 那么,a%c'…
传送门 •题意 给出两个正整数 a,b: 求解 k ,使得 LCM(a+k,b+k) 最小,如果有多个 k 使得 LCM() 最小,输出最小的k: •思路 时隔很久,又重新做这个题 温故果然可以知新❤ 重要知识点 GCD(a,b)=GCD(a,b-a)=GCD(b,b-a) (b>a) 证明: 设GCD(a,b)=c 则a%c=0,b%c=0,(b-a)%c=0 所以GCD(a,b-a)=c 得GCD(a,b)=GCD(a,b-a) gcd(a+k,b-a)肯定是(b-a)的因子 所以gcd(a…
C. GCD Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/583/problem/C Description The GCD table G of size n × n for an array of positive integers a of length n is defined by formula Let us remind you that the greatest c…
题目:http://codeforces.com/contest/1152/problem/C 题意:给你a,b, 你可以找任意一个k     算出a+k,b+k的最小公倍数,让最小公倍数尽量小,求出这个k 思路: 因为现在两个都是未知数,我们无法确定 我们根据gcd底层实现原理 gcd(a+k,b+k) = gcd(b-a,a+k) a=c*x; b=c*y; b-a=c*(y-x) 所以证明b-a的因子是a的因子也是b的因子 那么我们只要枚举出b-a的因子,然后再套入a+k中求得k,然后枚举…
题目链接:http://codeforces.com/contest/583/problem/C C. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The GCD table G of size n × n for an array of positive integers a of length n …
https://codeforces.com/contest/1152/problem/C 题意 a和b,找到k,使得lcm(a+k,b+k)最小(a,b:1e9) 题解 设gcd=gcd(a+k,b+k)且\(a<b\),即(a+k)%gcd=0,(b+k)%gcd=0,(a+k)%gcd=(b+k)%gcd,a%gcd=b%gcd 移项得b%gcd-a%gcd=0,(b-a)%gcd=0 枚举b-a的因数即gcd,维护最小值 代码 #include<bits/stdc++.h> #d…
题目链接:https://codeforces.com/contest/1370/problem/B 题意 给出 $2n$ 个数,选出 $2n - 2$ 个数,使得它们的 $gcd > 1$ . 题解 大于 $1$ 最好构造的 $gcd$ 就是 $2$ 了,根据奇偶将 $2n$ 个数分类,然后两个奇数一对,两个偶数一对即可. 代码 #include <bits/stdc++.h> using namespace std; void solve() { int n; cin >>…
B. GCD Arrays 题源:https://codeforces.com/contest/1629/problem/B 题目大意 给出一段区间[l, r],可以进行操作(把任意两个数拿出来,把他俩乘积放回去),如果经过 k 次该操作后,能找到两个数a, b, 使得 gcd(a, b) > 1,就输出"YES",否则输出"NO" 思路 如果要使得整个数组的GCD大于 1,每个元素都必须具有一个共同的质因数,因而很容易得出 2 是最常见的质因数. 又因为偶数…
C. GCD Table The GCD table G of size n × n for an array of positive integers a of length n is defined by formula Let us remind you that the greatest common divisor (GCD) of two positive integers x and y is the greatest integer that is divisor of both…
A. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The GCD table G of size n × n for an array of positive integers a of length n is defined by formula Let us remind you that the…
C. Neko does Maths time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko ha…
学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152B. Neko Performs Cat Furrier Transform 题目链接:"https://codeforces.com/contest/1152/problem/B" 题目描述: Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of th…
学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152A - Neko Finds Grapes 题目链接:"https://codeforces.com/contest/1152/problem/A" 题目描述: On a random day, Neko found n treasure chests and m keys. The i-th chest has an integer ai written on it and the j-t…
应该经常需要锻炼一下英语阅读理解能力和代码能力,所以以后还是需要多打打CF. 今天大概就是水一水找找感觉. A. Neko Finds Grapes $n$个箱子,$m$个钥匙 ($n,m \leq 10^6$),每个箱子有参数$a_i$,每个钥匙有参数$b_i$ 当且仅当,$a_i + b_j \equiv 1 (mod 2)$,则说箱子和钥匙配对成功. 注意到,一个箱子只能和一个钥匙配对最多一次. 要求最大化配对数. Solution : 直接统计即可,考虑配对只可能是奇数+偶数或者偶数+奇…
对角线上的元素就是a[i],而且在所在行和列中最大, 首先可以确定的是最大的元素一定是a[i]之一,这让人想到到了排序. 经过排序后,每次选最大的数字,如果不是之前更大数字的gcd,那么只能是a[i]之一. div2路漫漫... #include<bits/stdc++.h> using namespace std; typedef int ll; ll a[]; *]; ll gcd(ll a,ll b) { return b?gcd(b,a%b):a; } map<int,int&g…
https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没有公共点,问边集大小 题解 对于一颗字典树,从低向上贪心,最底层的边全拿,定义好状态,记忆化搜索计数 定义dp[i][j]为左括号数量为i,右括号数量为j的最大边集 \(i<n\),\(dp[i][j]->dfs(i+1,j)\) \(j<i\),\(dp[i][j]->dfs(i,j…
C. Neko does Maths 题意 给 \(a,b\) ,求一个最小的 \(k\) 使得 \(\text{lcm}(a+k,b+k)\) 最小. \(a,b\le 10^9\) 题解 \(\gcd (a+k,b+k) = \gcd(b-a,a+k)\) . 只需枚举 \(b-a\) 的因数作为 \(\gcd\) ,容易算出最小的 \(k\) ,然后更新答案即可. code #include<cstdio> long long mn=1ll<<60; int a,b,k; i…
题意:有一个长度为\(2n\)的数组,删去两个元素,用剩下的元素每两两相加构造一个新数组,使得新数组所有元素的\(gcd\ne 1\).输出相加时两个数在原数组的位置. 题解:我们按照新数组所有元素均为偶数来进行构造,因为旧数组的长度为\(2n\),所以无论原数组有多少个奇数和偶数,我们都可以选择删去两个数,使得剩下的数两两组合得到偶数,直接分类讨论输出即可. 代码: #include <iostream> #include <cstdio> #include <cstrin…
A. Neko Finds Grapes 代码: #include <bits/stdc++.h> using namespace std; ; int N, M; int a[maxn], b[maxn]; , evea = , oddb = , eveb = ; int main() { scanf("%d%d", &N, &M); ; i < N; i ++) { scanf("%d", &a[i]); ) odda…
A 签到 #include<bits/stdc++.h> using namespace std; ],t[],ans; int main() { scanf("%d%d",&n,&m); ,x;i<=n;i++)scanf(]++; ,x;i<=m;i++)scanf(]++; ans=min(s[],t[])+min(s[],t[]); printf("%d",ans); } B 要求40次,而log(1e6)≍20,也就…
传送门 题意: 给出一个数x,有两个操作: ①:x ^= 2k-1; ②:x++; 每次操作都是从①开始,紧接着是② ①②操作循环进行,问经过多少步操作后,x可以变为2p-1的格式? 最多操作40次,输出操作数和所有操作中步骤①的操作数的k: 我的思路: 操作①每次都是异或 (k-1) 个1: 我们最终的结果是将 x 变为(p-1)个1: 那么,我们只要每次异或操作都将x中最高的0位变为1: 因为x最多只有20位,所以,完全可以在40个操作内将x变为(p-1)个1: 例如: 7654321(位置…
就是一欧拉路径 贴出邻接表欧拉路径 CODE #include <bits/stdc++.h> using namespace std; const int MAXN = 100005; int n, b[MAXN], c[MAXN], bin[MAXN<<1], tot; int val[MAXN], deg[MAXN], stk[MAXN<<1], top; int fir[MAXN], cnt=1, nxt[MAXN<<1], to[MAXN<&…
题意 有nnn个点,每个点只能走到编号在[1,min(n+m,1)][1,min(n+m,1)][1,min(n+m,1)]范围内的点.求路径长度恰好为kkk的简单路径(一个点最多走一次)数. 1≤n≤109,1≤m≤4,1≤k≤min(n,12)1\le n\le 10^9,1\le m\le 4,1\le k\le min(n,12)1≤n≤109,1≤m≤4,1≤k≤min(n,12) 分析 直接考虑走路径的话不能判有没有走过,然后就把路径转化为一个序列,每次往里面插入新的点(神了).因为…
Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和 [Problem Description] 给你一棵树,树上每个节点都有一个权值.定义\(1\sim v\)的最短路径所经过的所有节点\(u\)称为\(v\)节点的祖先.定义函数\(f(u,v)=gcd(u,t1,t2,\dots,v)\),其中\(u,t1,t2,\dots\)都是\(v\)的祖先.求\(\sum f(u,v)\). […
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate it n = int(raw_input()) s = "" a = ["I hate that ","I love that ", "I hate it","I love it"] for i in ran…
Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接快速幂 代码 #include<bits/stdc++.h> using namespace std; long long quickpow(long long m,long long n,long long k) { long long b = 1; while (n > 0) { if…
Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 模拟一下就好了 代码 #include<bits/stdc++.h> using namespace std; string s[3]; map<char,int>r,c; char ss[2][107]; int main() { s[0]="qwertyuiop"…
Codeforces Round #116 (Div. 2, ACM-ICPC Rules) 代码 Codeforces Round #116 (Div. 2, ACM-ICPC Rules) A. Defragmentation 按颜色分配\(1\)到\(\sum_{i=1}^{m}{n_i}\)位置,那么可以得到\(t_i\)表示位置\(i\)最后占据位置\(t_i\),-1表示该位置为空. 那么会出现两种情况: \(t_1 \to t_2 \to \cdots \to \ {-1}\),即…