usaco 2.2.3(runround)
题目:
Runaround numbers are integers with unique digits, none of which is zero (e.g., 81362) that also have an interesting property, exemplified by this demonstration:
- If you start at the left digit (8 in our number) and count that number of digits to the right (wrapping back to the first digit when no digits on the right are available), you'll end up at a new digit (a number which does not end up at a new digit is not a Runaround Number). Consider: 8 1 3 6 2 which cycles through eight digits: 1 3 6 2 8 1 3 6 so the next digit is 6.
- Repeat this cycle (this time for the six counts designed by the `6') and you should end on a new digit: 2 8 1 3 6 2, namely 2.
- Repeat again (two digits this time): 8 1
- Continue again (one digit this time): 3
- One more time: 6 2 8 and you have ended up back where you started, after touching each digit once. If you don't end up back where you started after touching each digit once, your number is not a Runaround number.
Given a number M (that has anywhere from 1 through 9 digits), find and print the next runaround number higher than M, which will always fit into an unsigned long integer for the given test data.
PROGRAM NAME: runround
INPUT FORMAT
A single line with a single integer, M
SAMPLE INPUT (file runround.in)
81361
OUTPUT FORMAT
A single line containing the next runaround number higher than the input value, M.
SAMPLE OUTPUT (file runround.out)
81362
放在了dp这一章,怎么想都不会,无奈之下想枚举试试运气。表示几天没有敲代码,中间出现了各种不可思议的错误。枚举过了。
/* ID:614433244 PROG: runround LANG: C++ */ #include"iostream" #include"cstdio" #include"cstring" using namespace std; int a[10],head; bool vis[10],use[10]; int isok( int n ) { memset(use,0,sizeof(use)); use[0]=1; memset(vis,0,sizeof(vis)); head=0; int tt=n; while( tt ) { a[head]=tt%10; tt=tt/10; if( use[a[head]]==1 ) return 0; else use[a[head]]=1; head++; } int l,r; l=0;r=head-1; while( l<=r ) { int x; x=a[l];a[l]=a[r];a[r]=x; l++;r--; } int v=0; vis[0]=1; int flag=1; int p; while( 1 ) { v=(a[v]+v)%head; if( vis[v] ) { p=v; break; } else vis[v]=true; } for( int i=0;i<head;i++ ) { if( vis[i]==0 ) { flag=0;break; } } return (p==0)&&flag; } int main() { freopen("runround.in","r",stdin); freopen("runround.out","w",stdout); int n; scanf("%d",&n); int i; for( i=n+1;;i++ ) { if( isok(i) ) { printf("%d\n",i); break; } } return 0; }
浙公网安备 33010602011771号