令$d=\gcd(n,m)$,存在$x$和$y$使得$xn+i=ym+j$的充要条件是$i\equiv j(mod \ d)$,因此将$xd+i$(其中$0\le i<d$)作为一组,共有$d$组,根据上述结论任意两组之间相互独立
若一组中没有快乐的人,由于独立性必然无解,即有解需要$且\forall 0\le i<d,\exists t\in x\cup y且t\equiv i(mod\ d)$,必要条件为$d\le b+g$(以下记$b+g$为$N$来表示复杂度),因此可以对每一组分别求出天数$t_{i}$,则有$ans=\max_{0\le i<d}t_{i}d+i$(以下子问题中仍用$ans$来表示$t_{i}$)
对于子问题,显然有$\gcd(n,m)=1$,再不妨假设$n\ge m$($n<m$交换即可),则若第$t\ge m$天所有男生都快乐,所有女生必然快乐
证明:对于前$m$天,对$m$取模构成完全剩余系,即每个女生恰好出现一次;对$n$取模构成剩余系,即男生各不相同
反证法,若某个女生不快乐,其和其对应的男生必然都不快乐,而到第$t$天那个男生仍然不快乐,不符合假设
由此,$ans$即为最后一个快乐的男生的时间或$ans<m$,后者特判一下即可
若第$t$天且女生$i$(其中$i\equiv t(mod\ m)$)快乐,则男生$j$(其中$j\equiv t(mod\ n)$)必然快乐,而类似的可以证明女生$i$快乐等价于男生$(j+n-m)\ mod\ n$快乐的,因此不妨看作:若第$t$天且男生$(i+n-m)\ mod\ n$快乐,则男生$i$必然快乐
还需要考虑初始状态,若初始女生$i$快乐且男生$i$不快乐,可以看作男生$i$快乐(以下就用$S=x\cup y$来表示初始快乐的集合)
考虑建图:将$((i+n-m)\ mod\ n,i)$连边,由于$\gcd(n,m)=1$,因此这必然是一个大小为$n$的环,每一个初始的点影响其后面的若干个数(直到下一个初始的点),即$ans=\max_{t\in S}(t+m\cdot \min_{k>0,(t+km)\ mod\ n\in S}(k-1))$
考虑后面这个$\min$:若枚举最终结果$t'=(t+km)\ mod\ n$,即求同余方程$t+km\equiv t'(mod\ n)$的最小正整数解:用exgcd求出$km\equiv 1(mod\ n)$的最小正整数解$k'$(预处理),原方程最小正整数解即为$(k'(t'-t+n)+n-1)\ mod\ n+1$
先特判$t'=t$的情况,当$|S|=1$时才会选择这一个,因此解即$k'(t'-t+n)\ mod\ n=(k't'-k't+k'n)\ mod\ n$,那么将所有数按照$kt'\ mod\ n$排序并找到之后的那个数即可,复杂度$o(\log_{2}n+N\log_{2}N)$

 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的更多相关文章

  1. CodeForces 515C. Drazil and Factorial

    C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  2. 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 ...

  3. 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. ...

  4. 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 ...

  5. [codeforces 516]A. Drazil and Factorial

    [codeforces 516]A. Drazil and Factorial 试题描述 Drazil is playing a math game with Varda. Let's define  ...

  6. CF Drazil and Factorial (打表)

    Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. CF Drazil and His Happy Friends

    Drazil and His Happy Friends time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. CF Drazil and Date (奇偶剪枝)

    Drazil and Date time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  9. 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 ...

随机推荐

  1. 对于caffe程序中出现的Unknown database backend问题的报错怎么办?

    在预处理器中添加USE_LMDB,因为caffe需要一种数据输入格式 这样,在db.cpp中#ifdef USE_LMDB就会变亮,显示使用的数据格式为LMDB

  2. 开发函数计算的正确姿势——OCR 服务

    作者 | 杜万(倚贤) 阿里云技术专家 简介 首先介绍下在本文出现的几个比较重要的概念: OCR(光学字符识别):光学字符识别(Optical Character Recognition, OCR)是 ...

  3. CF49E Common ancestor(dp+dp+dp)

    纪念卡常把自己卡死的一次自闭模拟赛 QWQ 一开始看这个题,以为是个图论,仔细一想,貌似可以直接dp啊. 首先,因为规则只有从两个变为1个,貌似可以用类似区间\(dp\)的方式来\(check\)一段 ...

  4. 用C++实现的数独解题程序 SudokuSolver 2.4 及实例分析

    SudokuSolver 2.4 程序实现 本次版本实现了 用C++实现的数独解题程序 SudokuSolver 2.3 及实例分析 里发现的第三个不完全收缩 grp 算法 thirdGreenWor ...

  5. vue.$set实现原理

    上源码: export function set (target: Array<any> | Object, key: any, val: any): any { if (process. ...

  6. jq问题

    <div id="box"> <p> <span>A</span> <span>B</span> </ ...

  7. HCNP Routing&Switching之BGP路由宣告

    前文我们了解了BGP报文结构.类型以及邻居状态相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15422924.html:今天我们来聊一聊BGP路由宣告 ...

  8. java中延时队列的使用

    最近遇到这么一个需求,程序中有一个功能需要发送短信,当满足某些条件后,如果上一步的短信还没有发送出去,那么应该取消这个短信的发送.在翻阅java的api后,发现java中有一个延时队列可以解决这个问题 ...

  9. python画图的工具及网站

    ①Gallery - Matplotlib 3.4.3 documentation 学会模仿并超越 ②Examples - Apache ECharts js网页端动态展示 ③WEB色見本 原色大辞典 ...

  10. Python import cStringIO ImportError: No module named 'cStringIO'

    From Python 3.0 changelog; The StringIO and cStringIO modules are gone. Instead, import the io modul ...