[cf516E]Drazil and His Happy Friends
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 100005
4 #define ll long long
5 int n,m,d,kk,b[N],g[N],vb[N],vg[N],v[N<<1];
6 ll ans;
7 bool cmp(int x,int y){
8 return (x%d<y%d)||(x%d==y%d)&&(x<y);
9 }
10 int exgcd(int x,int y,int &a,int &b){
11 if (!y){
12 a=1;
13 b=0;
14 return x;
15 }
16 int g=exgcd(y,x%y,b,a);
17 b-=(x/y)*a;
18 return g;
19 }
20 ll calc(){
21 bool flag=(n<=vb[0]+vg[0]);
22 ll ans=-1;
23 if (flag){
24 for(int i=0,j=1,k=1;i<n;i++){
25 if (((j>vb[0])||(vb[j]!=i))&&((k>vg[0])||(vg[k]!=i))){
26 flag=0;
27 break;
28 }
29 if ((i<m)&&((j>vb[0])||(vb[j]!=i)||(k>vg[0])||(vg[k]!=i)))ans=i;
30 if ((j<=vb[0])&&(vb[j]==i))j++;
31 if ((k<=vg[0])&&(vg[k]==i))k++;
32 }
33 if (flag)return ans;
34 ans=0;
35 }
36 v[0]=0;
37 for(int i=1;i<=vb[0];i++)v[++v[0]]=1LL*vb[i]*kk%n;
38 for(int i=1;i<=vg[0];i++)v[++v[0]]=1LL*vg[i]*kk%n;
39 sort(v+1,v+v[0]+1);
40 if (v[1]==v[v[0]])return 1LL*v[1]*m%n+1LL*(n-1)*m;
41 ans=1LL*v[v[0]]*m%n+((v[1]-v[v[0]]+1LL*kk*n)%n-1)*m;
42 for(int i=1;i<v[0];i++)
43 if (v[i]!=v[i+1])ans=max(ans,1LL*v[i]*m%n+((v[i+1]-v[i])%n-1LL)*m);
44 return ans;
45 }
46 int main(){
47 scanf("%d%d",&n,&m);
48 d=exgcd(n,m,b[0],g[0]);
49 n/=d;
50 m/=d;
51 if (n>m)kk=(g[0]%n+n)%n;
52 else kk=(b[0]%m+m)%m;
53 scanf("%d",&b[0]);
54 for(int i=1;i<=b[0];i++)scanf("%d",&b[i]);
55 scanf("%d",&g[0]);
56 for(int i=1;i<=g[0];i++)scanf("%d",&g[i]);
57 if (d>b[0]+g[0]){
58 printf("-1");
59 return 0;
60 }
61 sort(b+1,b+b[0]+1,cmp);
62 sort(g+1,g+g[0]+1,cmp);
63 if (n<m)swap(n,m);
64 for(int i=0,j=1,k=1;i<d;i++){
65 vb[0]=vg[0]=0;
66 while ((j<=b[0])&&(b[j]%d==i))vb[++vb[0]]=b[j++]/d;
67 while ((k<=g[0])&&(g[k]%d==i))vg[++vg[0]]=g[k++]/d;
68 if ((!vb[0])&&(!vg[0])){
69 printf("-1");
70 return 0;
71 }
72 ans=max(ans,calc()*d+i);
73 }
74 printf("%lld",ans);
75 }
[cf516E]Drazil and His Happy Friends的更多相关文章
- CodeForces 515C. Drazil and Factorial
C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- CodeForces 515B. Drazil and His Happy Friends
B. Drazil and His Happy Friends time limit per test 2 seconds memory limit per test 256 megabytes in ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序
B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...
- [codeforces 516]A. Drazil and Factorial
[codeforces 516]A. Drazil and Factorial 试题描述 Drazil is playing a math game with Varda. Let's define ...
- CF Drazil and Factorial (打表)
Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CF Drazil and His Happy Friends
Drazil and His Happy Friends time limit per test 2 seconds memory limit per test 256 megabytes input ...
- CF Drazil and Date (奇偶剪枝)
Drazil and Date time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #292 (Div. 1) - B. Drazil and Tiles
B. Drazil and Tiles Drazil created a following problem about putting 1 × 2 tiles into an n × m gri ...
随机推荐
- Dapr + .NET Core实战(十二)服务调用之GRPC
什么是GRPC gRPC 是一种与语言无关的高性能远程过程调用 (RPC) 框架. gRPC 的主要优点是: 高性能轻量级 RPC 框架. 协定优先 API 开发,默认使用协议缓冲区,允许与语言无关的 ...
- 题解 「BZOJ3636」教义问答手册
题目传送门 Description 作为泉岭精神的缔造者.信奉者.捍卫者.传承者,Pear决定印制一些教义问答手册,以满足泉岭精神日益增多的信徒.Pear收集了一些有关的诗选.语录,其中部分内容摘录在 ...
- uoj21 缩进优化(整除分块,乱搞)
题目大意: 给定一个长度为\(n\)的序列 让你找一个\(x\),使得\(ans\)尽可能小 其中$$ans=\sum_{i=1}^{n}\lfloor\frac{a_i}{x}\rfloor + \ ...
- res目录下的结构
目录 res目录下的结构 drawable开头的文件夹 mipmap开头的文件夹 values开头的文件夹 layout文件夹 使用res目录下的资源 res目录下的结构 如果你展开res目录看一下, ...
- 使用 z3 进行逆向 解密字符串
在逆向过程中,我们知道了一个结果值,和一段计算代码.这个时候我们需要知道计算前的值是什么:需要用到 z3 模块来进行解题 z3项目地址 Java代码如下: private String b(Strin ...
- Windows 安装 gcc
Windows 安装 gcc ① 官网下载 GCC, the GNU Compiler Collection - GNU Project - Free Software Foundation (FSF ...
- 初学python-day9 函数1(已更新)
函数 一.函数基础 1.什么是函数 在一个完整的项目中,某些功能会被重复使用,那么会将代码段封装成函数,当我们要使用的时候,直接调用即可. 函数是可以实现一定的小程序或者功能. 优点: 增加了代码的重 ...
- JVM:类加载与字节码技术-2
JVM:类加载与字节码技术-2 说明:这是看了 bilibili 上 黑马程序员 的课程 JVM完整教程 后做的笔记 内容 这部分内容在上一篇笔记中: 类文件结构 字节码指令 编译期处理 类加载阶段 ...
- poi实现生成下拉选联动
在我们实际的程序开发中,经常需要用到从excel导入数据中系统中,而为了防止用户在excel中乱输入文字,有些需要用到下拉选的地方,就需要从程序中动态生成模板.本例子简单的讲解一下,如何生成级联下拉选 ...
- IDEA + maven 零基础构建 java agent 项目
200316-IDEA + maven 零基础构建 java agent 项目 Java Agent(java 探针)虽说在 jdk1.5 之后就有了,但是对于绝大多数的业务开发 javaer 来说, ...