UPC- 圆桌问题

山再高,往上爬,总能登顶;
路再长,走下去,定能到达。

圆桌问题

题目描述

圆桌上围坐着2n个人其中n个人是好人,另外n个人是坏人。如果从第一个人开始数数数到第m个人,则立即处死该人,然后从被处死的人之后开始数数,再将数到的第m个人处死…… 依此方法不断处死围坐在圆桌上的人。试问预先应如何安排这些好人与坏人的座位,能使得在处死n个人之后,圆桌上围坐的剩余的n个人全是好人。

输入

仅一行都有两个数依次为n和m,表示一个问题的描述信息。

输出

输出问题的解,问题的解可以用连续的若干行字符来表示,每行的字符数量不超过50,但是在问题的解中不允许出现空白字符和空行,用大写字母 G 表示好人、大写字母 B 表示坏人。

Sample Input

2 3

Sample Output

GBBG

题目大意

这题的意思是经典的约瑟夫环问题,每逢数字n就被枪决。
然后有几个人是串通好的,他们不想被杀掉。所以他们应该留在最后。

思路分析

这题主要就是一个模拟思路,遇到就剔除就好。
我的思路就是加一个标记,超过限制就取余,遇到就eraser
实际上玩的就是一个vector操作
然后就是输出了,每50个换一个行

AC时间到

#include<algorithm>
#include<iostream>
#include<string.h>
#include <iomanip>
#include<stdio.h>
#include<utility>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
#pragma warning(disable:4244)
#define PI 3.141592653589793
#pragma GCC optimize(2)
#define accelerate cin.tie(NULL);cout.tie(NULL);ios::sync_with_stdio(false);
#define EPS 1.0e-8
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const char char_inf = 127;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll read() {
    ll c = getchar(), Nig = 1, x = 0;
    while (!isdigit(c) && c != '-')c = getchar();
    if (c == '-')Nig = -1, c = getchar();
    while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
    return Nig * x;
}
inline void out(ll a) {
    if (a < 0)putchar('-'), a = -a;
    if (a >= 10)out(a / 10);
    putchar(a % 10 + '0');
}
ll phi(ll n)
{
    ll ans = n, mark = n;
    for (ll i = 2; i * i <= mark; i++)
        if (n % i == 0) { ans = ans * (i - 1) / i; while (n % i == 0)n /= i; }
    if (n > 1)ans = ans * (n - 1) / n; return ans;
}
ll qpow(ll x, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1)res = (res * x) % mod;
        x = (x * x) % mod;
        n >>= 1;
    }
    return res;
}
#define Floyd for(int k = 1; k <= n; k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
#define read read()
vector<int>q;
bool judge[100000];
int main()
{
    int n = read, m = read;
    int mark = n << 1;
    for (int i = 0; i < mark; i++)//存入编号从0开始就行
        q.push_back(i);//从0开始标记也一样,只不过取余操作需要改变下
    int id = 0;//位置标记
    for (int i = 0; i < n; i++)
    {
        id = (id + m - 1) % q.size();//超过边界返回第一个人
        q.erase(q.begin() + id);
    }
    int N = 0;
    for (auto i : q)judge[i] = 1;//i是迭代q里所有剩余的元素
    for (int i = 0; i < mark; i++)
    {
        if (judge[i])putchar('G');//如果活到最后
        else putchar('B');//如果没有活到最后
        N++;//50次换行
        if (N == 50)
        {
            N = 0;
            putchar('\n');
        }
    }
}

By-轮月

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章