约瑟夫问题在大部分情况中是一道水题,但是如果把数据加强一点,可能会难倒一片人,所以今天来介绍如何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,它是一种关联式容器,可以用这东西来模拟约瑟夫问题的全过程!!!剩下的就是一些简单操作了!