B. Drazil and His Happy Friends
这是 Codeforces Round #292 (Div. 2)的一道题,原题在这里,题意就是:
小明有n个男同学(编号为 0 ~ n-1)和m个女同学 (编号为 0 ~ m-1),小明要安排男女之间约会,如何安排约会呢,假设时间是i (i >=0) ,小明在 i 天就邀请 i % n 号男同学,和 i % m 号女同学约会,如果男女同学之间有一个是快乐的,那么两个人都是快乐的,那么问题来了,小明能否使他们全部都快乐?
我当时是这样想的,要是n和m相等的话,那么要是在编号区间 [0, n)之间都有人快乐,例如,
例a:
要是n = m = 3, n 中快乐的有 0、1, m 中快乐的有 2,那么全部人在约会后都会快乐。
要是n 和 m 不想等呢? 问题来了,然后这个时间要怎么联系起来?
我当时想不出,模拟这个约会的过程,用O(n * m) 的方法,结果被hack了一下,水爆了 = =、 看了editorial后才明白的。
好了,回到问题,要是n 和m 不想等的时候呢,假设n = 2 , m = 3,则在时间 i = 2 后,在男生中 2%2 = 0 号男生与 2%3 = 2 号女生约会,可以把m 中的2 号女生看作一个,与 两个男生约会,问题就变成了 n = 2, m = 1, 之所以要这么做,是为了找出一个区间,这个区间满足,每个编号都有人快乐, 就像前面例a 一样。然后继续,n = 2, m = 1时,在时间i = 1 时,男生中 1 % 2 = 1 号男生与 1 % 1 = 0 号女生约会,把 n 中的1 号男生看作一个,与1个女生约会,问题就编程了
n = m = 1, 这样,就达到了之前说的一个目的,找到区间[0, 1) 要是这个内对应的编号都有人快乐,那么全部人都会快乐。先设 c 是 n 和 m 的最大公约数,在 n = 2, m = 3, i = 2 时,这样变换, n = 2, m = m % n = 1, i 在累加时也这样做,最后把问题变换为 n = m = 1, 这样,只要区间之间
[0, 1) 之间对应的编号都有人快乐,那么全部人都会快乐。是不是很像gcd,在例子a 中,因 0、1 号是快乐的,那么0 % 1 = 0, 1 % 1 = 1,会因为他们而快乐,所以,每个快乐的人的编号去模n和m的公约数,就求出因为他们而快乐的人的编号。好了,说了一大堆没有逻辑的话,该上代码了。
#include <stdio.h>
#include <iostream>
#include <map>
#include <string.h>
using namespace std; int gcd(int a, int b){
int c, d;
d = max(a, b); c = min(a, b);
if(d%c == 0) return c;
else return gcd(d%c, c);
}
int pep[101];
int main(){
int n, m, b, g, c, i, cnt = 0, id;
// freopen("e:/in.txt", "r", stdin);
memset(pep, 0, sizeof(pep));
cin >> n >> m;
c = gcd(n, m);
cin >> b;
for( i = 0; i < b; i++ ){
cin >> id;
if(!pep[id%c]) pep[id%c] = 1, cnt++;
}
cin >> g;
for( i = 0; i < g; i++ ){
cin >> id;
if(!pep[id%c]) pep[id%c] = 1, cnt++;
} if(cnt == c) cout << "Yes";
else cout << "No";
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
B. 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 ...
- #292 (div.2) D.Drazil and Tiles (贪心+bfs)
Description Drazil created a following problem about putting × tiles into an n × m grid: "The ...
随机推荐
- 使用ssh从外网访问内网
一.场景如下: 各个角色的对应关系如下: 角色 描述 APP 个人笔记本,属于内网IP sshd server 公网 VPS ( 映射端口: port 2222 ),拥有公网IP ssh client ...
- Android蓝牙开发教程(三)——蓝牙设备相互通讯
在上一篇中已经介绍如何连接我们搜索到的蓝牙设备,如果你还没阅读过,建议先看看上一篇文章Android蓝牙开发教程(二)——连接蓝牙设备 在上一篇文章中,无论是自动连接还是被动连接,连接成功后,都是将获 ...
- 转: CSRF(Cross Site Request Forgery 跨站域请求伪造) 背景与介绍
from: https://www.ibm.com/developerworks/cn/web/1102_niugang_csrf/ 在 IBM Bluemix 云平台上开发并部署您的下一个应用 ...
- winform程序公布后,client下载报错“您的 Web 浏览器设置不同意执行未签名的应用程序”
如题 在winserver2008服务器上操作会报错.解决的方法: IE→Internet选项→安全→可信网站,加入信任公布的IP地址
- [C++设计模式] state 状态模式
<head first 设计模式>中的样例非常不错,想制造一个自己主动交易的糖果机,糖果机有四个状态:投入钱不足,投入钱足够,出售糖果,糖果售罄. 糖果机的当前状态处于当中不同的状态时,它 ...
- webstrom 代码工具(转http://www.cnblogs.com/tangdanni11/p/5149063.html)
Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...
- iBatis2 SqlMap中经常使用sql语句
本来我也不喜欢iBatis,那是由于我当时还不怎么会用它,如今我想说,iBatis是个好东西,不信你试试看.以下是我在项目实践中对iBatis的一个小总结.希望帮助众多在疲于iBatis编码而无暇思考 ...
- 如何配置Spring Boot Tomcat
1.概述 Spring Boot Web应用程序默认包含预配置的嵌入式Web服务器.但在某些情况下,我们要修改默认配置以满足自定义要求. 在本教程中,我们将介绍通过application.proper ...
- ASP.NET数据库连接字符串的加密与解密
ASP.NET web.config中,数据库连接字符串的加密与解密. 虽然不怎么新鲜,但相信还是有许多人不知道,好,不说废话,直接给方法:开始--->运行,输入cmd,接着输入以下内容 加密: ...
- iframe子页面获取父页面元素的方法
在iframe子页面获取父页面元素 代码如下: $.('#objld', parent.document); 在父页面获取iframe子页面的元素 代码如下: $("#objid" ...