[cf516E]Drazil and His Happy Friends

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 ...
随机推荐
- 第29篇-调用Java主类的main()方法
在第1篇中大概介绍过Java中主类方法main()的调用过程,这一篇介绍的详细一点,大概的调用过程如下图所示. 其中浅红色的函数由主线程执行,而另外的浅绿色部分由另外一个线程执行,这个线程最终也会负责 ...
- PostgreSQL 大小写问题 一键修改表名、字段名为小写
标准的SQL是不区分大小写的.但是PostgreSQL对于数据库中对象的名字允许使用支持大小写区分的定义和引用方法.方式就是在DDL中用双引号把希望支持大小的对象名括起来.比如希望创建一个叫AAA的表 ...
- bzoj2037 Sue的小球(区间dp,考虑到对未来的贡献)
大致意思就是现在你要不断的奔跑到不同的地点去接球,每一秒可以移动一个单位长度,而你接到一个球的动作是瞬间的,收益是y[i]-t*v[i] 然后呢,要求分数最高. 起初看这个 ...
- CVPR2021提出的一些新数据集汇总
前言 在<论文创新的常见思路总结>(点击标题阅读)一文中,提到过一些新的数据集或者新方向比较容易出论文.因此纠结于选择课题方向的读者可以考虑以下几个新方向.文末附相关论文获取方式. ...
- Proxypool代理池搭建
个人博客:点我 前言 项目地址 : https://github.com/jhao104/proxy_pool 这个项目是github上一个大佬基于python爬虫制作的定时获取免费可用代理并入池的代 ...
- html视口单位:vw,vh,rem
前言 不像响应式布局,通过media query,设置几个变化点来适配,流体排版通过调整大小,适配所有设备宽度.这个方法可以使我们开发的网页,在几乎所有屏幕尺寸上都可以使用.但出于一些原因,它的使用率 ...
- 重学c#系列——list(十二)
前言 简单介绍一下list. 正文 这里以list为介绍. private static readonly T[] s_emptyArray = new T[0]; public List() { t ...
- Linux常用命令介绍(满足日常操作)
大家好,今天来给大家分享一些Linux的常用命令,希望对大家有用 命令行的基本格式: 命令字 [选项] [参数] 其中,命令字.选项.参数之间用空格分开,多余的空格将被忽略.[ ]括起来的 ...
- Idea Maven auto Import
- Noip模拟22 2021.7.21
T1 d 简化题意就是找到相对平均长宽的偏移量较大的矩形给他删掉 可以说是个贪心,按照a,b分别为第一关键字排序 然后假装删去要求的那么多个按a排序的较小的,然后再去b中, 找到 删去的a中的那几个矩 ...