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

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

  10. #292 (div.2) D.Drazil and Tiles (贪心+bfs)

    Description Drazil created a following problem about putting  ×  tiles into an n × m grid: "The ...

随机推荐

  1. ScutSDK 0.9版本发布

    ScutSDK简介: ScutSDK是和Scut游戏服务器引擎,简化客户端开发的配套SDK,她彻底打通了Scut开源游戏服务器引擎与客户端引擎(如Cocos2d-x/Quick-x/Unity3D)项 ...

  2. 根据DatabaseMetaData确定数据库类型

    根据DatabaseMetaData确定数据库类型 DataSource dataSource = dataSourceTransactionManager.getDataSource(); conn ...

  3. 24. Spring Boot环境变量读取和属性对象的绑定【从零开始学Spring Boot】

    转:http://blog.csdn.net/linxingliang/article/details/52069509 凡是被spring管理的类,实现接口EnvironmentAware 重写方法 ...

  4. blind xxe攻击

    最近做啊里的题的时候遇到了 http://hivesec.net/web-security/%E5%85%B3%E4%BA%8Eblind-xxe.html

  5. 应该知道的Linux技巧(转载)

    这篇文章来源于Quroa的一个问答<What are some time-saving tips that every Linux user should know?>—— Linux用户 ...

  6. 思维探索者:完善个人知识体系的重要性 Google只会告诉你结果

    http://www.nowamagic.net/librarys/veda/detail/1711前面说了,人类解决问题大部分时候会习惯性地使用联想思维,简言之就是首先枚举你关于这个问题能够想到的所 ...

  7. React学习之受控和非受控组件

    受控组件是通过事件完成对元素value的控制,反之就是非受控组件. 1.受控组件的value通过onChange事件来改变,非受控不需要通过事件来改变value. 2.受控组件通过事件通过setSta ...

  8. Web前端学习攻略

    HTML+CSS: HTML进阶.CSS进阶.div+css布局.HTML+css整站开发. JavaScript基础: Js基础教程.js内置对象常用方法.常见DOM树操作大全.ECMAscript ...

  9. LeetCode HashTable 30 Substring with Concatenation of All Words

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  10. java 接口回调

    学习自:http://blog.csdn.net/xiaanming/article/details/8703708/ http://hellosure.iteye.com/blog/1130176 ...