约瑟夫问题『数据加强版』

1 篇文章 0 订阅

约瑟夫问题在大部分情况中是一道水题,但是如果把数据加强一点,可能会难倒一片人,所以今天来介绍如何A掉这题!『这里介绍的数据范围n,m均在30000以内』

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<iostream>
using namespace std;
vector<int>a;
struct node{
    int u,v,w;
}e[100010];
int flag[100010];
long long gl(){
    char c=getchar();long long f=1;
    int sum=0;
    while((c>'9' || c<'0') && c!='-')
        c=getchar();
    if(c=='-'){f=-1;c=getchar();}
    while(c>='0' && c<='9'){
        sum=sum*10+c-'0';
        c=getchar();
    }
    return f*sum;
}
int gi(){
    char c=getchar();int f=1,sum=0;
    while((c>'9' || c<'0') && c!='-')
        c=getchar();
    if(c=='-'){f=-1;c=getchar();}
    while(c>='0' && c<='9'){
        sum=sum*10+c-'0';
        c=getchar();
    }
    return f*sum;
}
int main(){
    int i,j,k,n,m;
    scanf("%d%d",&n,&m);
    int tot=n*2,now=1;
    for(i=1;i<=tot;i++)a.push_back(i);
    while(tot>n){
        now=(now+m-1);
        if(now<=tot){
            flag[a[now-1]]=1;
            a.erase(a.begin()+now-1);
            now=(now==tot?1:now);
        }
        else{
            now%=tot;
            now=(now==0?tot:now);
            flag[a[now-1]]=1;
            a.erase(a.begin()+now-1);
            now=(now==tot?1:now);
        }
        tot--;
    }
    for(i=1;i<=2*n;i++)
        if(flag[i])printf("B");
        else printf("G");
    puts("");
    return 0;
}

这里用的是STL『大法好』中的不定长数组——vector,它是一种关联式容器,可以用这东西来模拟约瑟夫问题的全过程!!!剩下的就是一些简单操作了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值