zoukankan      html  css  js  c++  java
  • USACO 2.2 Runaround Numbers

    Runaround Numbers

    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

    题目大意:就是说给你一个m,输出大于m的最小的满足要求的数字,要求是这样的:比如
    81362
    第一个数字8,从1号位置循环走8格到达4号位置
    第四个数字6,从4号位置循环走6格到达5号位置
    第五个数字2,从5号位置循环走2格到达2号位置
    第二个数字1,从2号位置循环走1格到达3号位置
    第三个数字3,从3号位置循环走3格到达1号位置
    开始新的循环。
    这就是满足要求的数字。
    题目没什么难度,就是确认下以后二分的写法
     1 /*
     2 ID:fffgrdc1
     3 PROB:runround
     4 LANG:C++
     5 */
     6 #include<cstdio>
     7 #include<iostream>
     8 #include<cstring>
     9 using namespace std;
    10 long long ans[100000];
    11 int tot=0;
    12 int a[10];
    13 bool vis[10];
    14 bool check(int n)
    15 {
    16     bool bo[10];
    17     memset(bo,0,sizeof(bo));
    18     int nn=0;bo[0]=1;int cnt=0;
    19     while(1)
    20     {
    21         nn+=a[nn];
    22         nn=(nn-1)%n+1;
    23         if(bo[nn])
    24         {
    25             return cnt==n&&nn==1;
    26         }
    27         bo[nn]=1;
    28         cnt++;
    29     }
    30 }
    31 void addans(int n)
    32 {
    33     long long temp=0;
    34     for(int i=1;i<=n;i++)
    35     {
    36         temp*=10;
    37         temp+=a[i];
    38     }
    39     ans[++tot]=temp;
    40     return ;
    41 }
    42 void dfs(int x,int n)
    43 {
    44     if(x==n+1)
    45     {
    46         if(check(n))
    47             addans(n);
    48         return ;
    49     }
    50     for(int i=1;i<10;i++)
    51     {
    52         if(!vis[i])
    53         {
    54             vis[i]=1;
    55             a[x]=i;
    56             dfs(x+1,n);
    57             vis[i]=0;
    58         }
    59     }
    60 }
    61 int main()
    62 {
    63     freopen("runround.in","r",stdin);
    64     freopen("runround.out","w",stdout);
    65     a[0]=1;
    66     memset(vis,0,sizeof(vis));
    67     for(int i=1;i<10;i++)
    68     {
    69         dfs(1,i);
    70     }
    71     int m;
    72     scanf("%d",&m);
    73     int l=1,r=tot+1;
    74     while(l<r)
    75     {
    76         int mid=(l+r)/2;
    77         if(ans[mid]<=m)l=mid+1;
    78         else r=mid;
    79     }
    80     printf("%d
    ",ans[r]);
    81     return 0;
    82 }

    二分

    int l=1,r=tot+1;
    while(l<r)
    {
      int mid=(l+r)/2;
      if(ans[mid]<=m)l=mid+1;
      else r=mid;
    }
    printf("%d ",ans[r]);

  • 相关阅读:
    Tensorflow实现LSTM识别MINIST
    linux误删除恢复
    python使用工具简介介绍
    一个画ROC曲线的封装包
    Anaconda基本使用
    对于进程没杀死占用内存和cpu行为的方法
    Gluon
    原博客地址
    训练词向量
    TPU尝试
  • 原文地址:https://www.cnblogs.com/xuwangzihao/p/5058041.html
Copyright © 2011-2022 走看看