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;
    }
    }
}

猜你喜欢

转载自www.cnblogs.com/LM-LBG/p/9974111.html
2.2
今日推荐