1. 程式人生 > >USACO 2.2 迴圈數

USACO 2.2 迴圈數

題目:https://www.luogu.org/problemnew/show/P1467

沒認真讀題啊,失誤好多

題目本身很簡單,注意判斷即可

順帶:如果%n之後是0,得特判賦值成n

程式碼:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<climits>
using namespace std;
typedef long long ll;
inline int read()
{
  int ans = 0,op = 1
; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') op = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { (ans *= 10) += ch - '0'; ch = getchar(); } return ans * op; } ll m; int a[10]; bool vis[12]; bool ex[12]; int invert(int
x) { int tot = 0; memset(a,0,sizeof(a)); memset(ex,0,sizeof(ex)); while(x) { a[++tot] = x % 10; if(ex[a[tot]]) return -1; ex[a[tot]] = 1; x /= 10; } for(int i = 1;i <=tot / 2;i++) swap(a[i],a[tot- i + 1]); return tot; } bool dfs(int x) { int cur = 1; memset(vis,
0,sizeof(vis)); int len = invert(x); if(len == -1) return 0; for(int i = 1;i <= len;i++) { if(vis[cur] || a[cur] == 0) return 0; vis[cur] = 1; cur += a[cur]; if(cur > len) cur %= len; if(!cur) cur = len; } if(cur != 1) return 0; return 1; } int main() { m = read(); for(ll i = m + 1;i <= LONG_LONG_MAX;i++) { if(dfs(i)) { printf("%lld",i); return 0; } } }