codeforces gym 100463I Yawner

//这题挂得让我怀疑我最近是不是做了什么坏事

题意:一个人有两个集合,先在其中一个集合选一个数x,然后向右走x布,然后再在另一个集合里选一个数y,向左走y步,问是否能走完数轴上所有点。

解:显然是求gcd(ai-bj)的值是不是1,然后有gcd(ai-bj)=gcd(ai-b1,ai-b2,ai-b3,........)=gcd(ai-b1,b2-b1,b3-b1,b4-b1.....)=gcd(gcd(ai-b1),gcd(bj-b1)),然后就可以把a,b分开求了。另外假如求出来gcd值是2,那么要特殊讨论a中是否有奇数,即 即我们先前的假设是步数一样,但实际上a可以比b多走一步。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<queue>
 8 #include<vector>
 9 #include<map>
10 #include<stack>
11 #include<string>
12 #define LL long long
13
14 const int MAXN=100007;
15 const int MAXM=0;
16 const int INF=2000000000;
17
18 using namespace std;
19
20 int a[MAXN],b[MAXN];
21 int n,m,cas=0;
22
23 int gcd(int a,int b){
24     if (b==0) return a;
25     return gcd(b,a%b);
26 }
27
28 bool check(int x){
29     if (x>2) return 0;
30     bool flag=0;
31     for (int i=0;i<n;i++){
32             if (a[i]&1) flag=true;
33     }
34     if (x==2 && !flag) return 0;
35     return 1;
36 }
37
38 int main(){
39     while (scanf("%d%d",&n,&m)==2){
40             if (n==0 && m==0) break;
41             cas++;
42             for (int i=0;i<n;i++) scanf("%d",&a[i]);
43             for (int i=0;i<m;i++) scanf("%d",&b[i]);
44             sort(a,a+n);
45             sort(b,b+m);
46             if (a[n-1]<b[0] || a[0]>b[m-1]){
47                     printf("Case %d: YES\n",cas);
48                     continue;
49             }
50             int ans=a[0]-b[0];
51             for (int i=1;i<n;i++) ans=gcd(ans,a[i]-b[0]);
52             for (int i=1;i<m;i++) ans=gcd(ans,b[i]-b[0]);
53             if (!check(abs(ans))){
54                     printf("Case %d: YES\n",cas);
55             }
56             else{
57                     printf("Case %d: NO\n",cas);
58             }
59     }
60     return 0;
61 }
62 /*
63 2 2
64 1
65 3
66 1
67 3
68
69 2 5
70 10
71 20
72 1
73 2
74 3
75 4
76 5
77 3 3
78 3
79 6
80 9
81 3
82 6
83 9
84 2 2
85 1
86 2
87 1
88 2
89 0 0
90 */

时间: 2024-08-23 14:57:44

codeforces gym 100463I Yawner的相关文章

Codeforces gym Hello 2015 Div1 B and Div2 D

Codeforces gym 100571 problem D Problem 给一个有向图G<V,E>和源点S,边的属性有长度L和颜色C,即E=<L,C>.进行Q次询问,每次给定一个点X,输出S到X的最短路的长度(不存在则输出 -1).但要求S到X的路径中相邻两条边颜色不一样. Limits Time Limit(ms): 1000 Memory Limit(MB): 256 |V|, |E|: [1, 10^5] X, S: [1, |V| ] L: [1, 10^9] |C|

Codeforces gym Hello 2015 Div1 E

Codeforces gym 100570 problem E (一种处理动态最长回文子串问题的方法) Problem 给一个长度为N的字符串S,字符集是'a'-'z'.进行Q次操作,操作分三种.一,修改位置X的字符为C:二,查询以P位置为中心的最长回文子串的长度,并输出:三,查询以P与P+1的中间位置为中心的最长回文子串的长度,并输出. More 第二种操作子串长度为奇数,一定存在:第三种操作子串长度为偶数,若不存在,输出 -1. Limits Time Limit(ms): 4000(1s足

Codeforces gym Hello 2015 Div1 C and Div2 E

Codeforces gym 100570 problem C Codeforces gym 100571 problem E Problem 给一个N行M列的矩阵Ma,进行Q次(Q<=10)查询,每次给定一个K,问有多少子矩阵,满足最大值max - 最小值min <=K. Limits Time Limit(ms): 8000 Memory Limit(MB): 512 N, M: [1, 400] Q: [1, 10] Ma(i, j), K: [1, 10^9] Solution (Th

【模拟】ECNA 2015 I What&#39;s on the Grille? (Codeforces GYM 100825)

题目链接: http://codeforces.com/gym/100825 题目大意: 栅栏密码.给定N(N<=10),密钥为一个N*N的矩阵,'.'代表空格可以看到,'X'代表被遮挡,还有密文字符串S,长度为N*N 每次将这个矩阵顺时针旋转90°,把矩阵中空格对应的位置按照从上到下从左到右的顺序依次填充上密文字符,求最终这个密文字符能否填满N*N的矩阵,能按顺序输出得到的答案,不能输出"invalid grille" 题目思路: [模拟] 直接模拟即可.旋转的坐标公式很好推.

Codeforces gym Hello 2015 Div2 B

Codeforces gym 100571 problem B Problem 设函数F(x),F(1)与F(2)已知,且当 i>=3,F(i)=a*F(i-2)+b*F(i-1).再给一个长度为N的数列A,进行Q次如下操作:每次给一个区间[L, R],对于每个k(L=<k<=R),将A[k]=A[k]+F[k-L+1].最后输出数列A(mod 10^9+7). Limits Time Limit(ms): 1000 Memory Limit(MB): 256 N, Q: [1, 10^

Codeforces Gym - 101147J Whistle&#39;s New Car

Discription Statements Whistle has bought a new car, which has an infinite fuel tank capacity. He discovered an irregular country since it has n cities and there are exactly n?-?1roads between them, of course, all cities are connected. He is so much

Codeforces Gym 101174 A Within Arm&#39;s Reach 贪心 手臂

#include<iostream> #include<stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <math.h> using namespace std; #define LL long long const int maxn=25; double a[maxn],l[maxn],r[maxn]; double ex,ey

Codeforces Gym 100269 Dwarf Tower (最短路)

题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game named "Dwarf Tower". In this game there are n different items,which you can put on your dwarf character. Items are numbered from 1 to n. Vasya want

CodeForces Gym 101063C 二进制压缩

http://codeforces.com/gym/101063/problem/C 给n个人,m样物品,每个人可以从物品中选择几样.两人选择物品的交集元素个数比上并集元素个数如果大于某个比例即可将两人配对.求配对数. n的范围是1e5,直接比较所有人的选择会TLE,应该将所有选择物品的情况用二进制压缩,m最大是10,情况数目小于2048,可以接受.注意配对总数范围应为long long. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> i