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 ...
随机推荐
- web.xml文件的 xsd引用(或dtd引用)学习
1. 为什么web.xml会有不同版本的xsd引用: JDK依赖变化: 或 servlet(JAVA EE)自身API的改变: 2. 为什么会有dtd和xsd两个版本的区别 我是在这篇文章中看到的,作 ...
- 关闭火狐定期向“http://detectportal.firefox.com/”发包
问题:最近发现火狐总是向http://detectportal.firefox.com/发包 办法:firefox地址栏输入 about:config,然后搜索找到 network.captive-p ...
- VS2010 MFC中 静态编译设置方法
问题:VS2010 c++编写的程序在别人的机子运行不了,缺少mfc100u.dll xxx100d.dll等 解决方法:1.将这些dll打包,和应用程序一起发布;2.采用MFC静态编译; 静态编译: ...
- 14.【nuxt起步】-Pm2 和nuxt服务运行
1.安装pm2 npm install pm2 -gd 2.启动 Pm2 start ./bin/www 3. pm2 save 4.Pm2 startup 5.Pm2 save修改 package. ...
- dubbo应用程序的单元测试环境搭建(springtest,powermock,mockito)
转:http://blog.csdn.net/yys79/article/details/66472797 最近,项目中频繁用到dubbo,而且java工程用引用了几十个关联系统的服务(如用户认证,基 ...
- 常见函数strlen、strcmp、strstr原型实现
数组元素的结束符为'\0'.串的结束符为NULL 一.strlen #include <iostream> using namespace std; long h_strlen(const ...
- npm run watch-poll 监控css、js 文件更新
后台执行npm run watch-poll 你可以在执行命令的后面接一个&命令就会在后台运行了.完整命令:npm run watch-poll & 就是任务号 文章来源:刘俊涛的博客 ...
- css:html() text() val()
转http://www.jb51.net/article/35867.htm .html()用为读取和修改元素的HTML标签 对应js中的innerHTML .html()是用来读取元素的HTM ...
- 安装centos出错
在vitural Box中安装centos,出现了如下问题,重新下一遍就好了,如果网速很慢,下载的过程中总是断断续续的就容易出现下载文件损坏的问题. Could not get the storage ...
- .NET C#生成随机颜色,可以控制亮度,生成暗色或者亮色 基于YUV模式判断颜色明亮度
.NET C#生成随机颜色,可以控制亮度,生成暗色或者亮色 基于YUV模式判断颜色明亮度 随机颜色在日常开发中很常用到,有时候要控制颜色明亮度,比如在白色背景网页上的随机颜色,一般要求颜色稍微暗一 ...