[USACO2.2]迴圈數 Runaround Numbers
阿新 • • 發佈:2018-12-13
連結
大意
定義一種數,滿足以下要求
- 沒有0
- 沒有重複出現的數字
- 每次往後跳第一個數字個數字(若超過則按環處理),能重新跳回第一個數字,且不死迴圈
舉個栗子:
81362 往後跳8次,跳到6,往後跳6次,跳到2,往後跳2次跳到1,往後跳1次跳到3,往後跳3次,跳到8。
求
之後的第一個迴圈數
思路
暴力模擬
程式碼
/*
ID:hzbismy1
LANG:C++
TASK:runround
*/
#define file(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout)
#include<cstdio>
#include<cctype>
#include<algorithm>
using namespace std;int n,len,i,x,a[21];
inline char Getchar()
{
static char buf[100000],*p1=buf+100000,*pend=buf+100000;
if(p1==pend)
{
p1=buf; pend=buf+fread(buf,1,100000,stdin);
if (pend==p1) return -1;
}
return *p1++;
}
inline int read()
{
char c;int d=1,f=0;
while(c=Getchar(),!isdigit(c))if(c==45)d=-1;f=(f<<3)+(f<<1)+c-48;
while(c=Getchar(),isdigit(c)) f=(f<<3)+(f<<1)+c-48;
return d*f;
}
inline void write(register long long x)
{
if(x<0)write(45),x=-x;
if(x>9)write(x/10);
putchar(x%10+48);
return;
}
inline bool xh(register int x)
{
len=0;
while(x){a[++len]=x%10;x/=10;}
reverse(a+1,a+1+len);
bool vis[31]={1};
int k=1;
for(register int i=1;i<=len;i++)
{
if(vis[a[k]]) return false;//出現過
vis[a[k]]=true;//標記
k=(k+a[k]-1)%len+1;//往後跳
}
return k==1;
}
signed main()
{
file(runround);
n=read();
for(i=n+1;!xh(i);i++);
write(i);putchar(10);
}