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号位置
开始新的循环。
这就是满足要求的数字。
题目没什么难度,就是确认下以后二分的写法
 /*
ID:fffgrdc1
PROB:runround
LANG:C++
*/
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
long long ans[];
int tot=;
int a[];
bool vis[];
bool check(int n)
{
bool bo[];
memset(bo,,sizeof(bo));
int nn=;bo[]=;int cnt=;
while()
{
nn+=a[nn];
nn=(nn-)%n+;
if(bo[nn])
{
return cnt==n&&nn==;
}
bo[nn]=;
cnt++;
}
}
void addans(int n)
{
long long temp=;
for(int i=;i<=n;i++)
{
temp*=;
temp+=a[i];
}
ans[++tot]=temp;
return ;
}
void dfs(int x,int n)
{
if(x==n+)
{
if(check(n))
addans(n);
return ;
}
for(int i=;i<;i++)
{
if(!vis[i])
{
vis[i]=;
a[x]=i;
dfs(x+,n);
vis[i]=;
}
}
}
int main()
{
freopen("runround.in","r",stdin);
freopen("runround.out","w",stdout);
a[]=;
memset(vis,,sizeof(vis));
for(int i=;i<;i++)
{
dfs(,i);
}
int m;
scanf("%d",&m);
int l=,r=tot+;
while(l<r)
{
int mid=(l+r)/;
if(ans[mid]<=m)l=mid+;
else r=mid;
}
printf("%d\n",ans[r]);
return ;
}

二分

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\n",ans[r]);

USACO 2.2 Runaround Numbers的更多相关文章

  1. USACO Section2.2 Runaround Numbers 解题报告 【icedream61】

    runround解题报告---------------------------------------------------------------------------------------- ...

  2. 洛谷P1467 循环数 Runaround Numbers

    P1467 循环数 Runaround Numbers 89通过 233提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 循环数是 ...

  3. USACO Runaround Numbers 模拟

    根据题意的 Runaround 规则去找比当前数大的最近的一个 Runaround数字 模拟题~ Source code: /* ID: wushuai2 PROG: runround LANG: C ...

  4. 【USACO 2.2】Runaround Numbers

    找出第一个大于n的数满足:每一位上的数都不同,且没有0,第一位开始每次前进当前这位上的数那么多位,超过总位数就回到开头继续往前进,最后能不能每个位都到过一次且回到第一位,$n<10^9$. 暴力 ...

  5. USACO Section 2.2 循环数 Runaround Numbers

    OJ:http://www.luogu.org/problem/show?pid=1467 #include<iostream> #include<vector> #inclu ...

  6. USACO Section 2.2: Runaround Numbers

    简单题 /* ID: yingzho1 LANG: C++ TASK: runround */ #include <iostream> #include <fstream> # ...

  7. USACO Runaround Numbers

    题目大意:问最近的比n大的循环数是多少 思路:第n遍暴力大法好 /*{ ID:a4298442 PROB:runround LANG:C++ } */ #include<iostream> ...

  8. USACO 3.1 Humble Numbers

    Humble Numbers For a given set of K prime numbers S = {p1, p2, ..., pK}, consider the set of all num ...

  9. p1467 Runaround Numbers

    直接搜就行. #include <iostream> #include <cstdio> #include <cmath> #include <algorit ...

随机推荐

  1. 第5章分布式系统模式 Data Transfer Object(数据传输对象)

    正在设计一个分布式应用程序,为了满足单个客户端请求,您发现自己对一个远程接口发出了多个调用,而这些调用所增加的响应时间超出了可接受的程度. 影响因素 在与远程对象通信时,请考虑下列需要权衡的因素: 远 ...

  2. C++关键字简述

    ID 范畴 关键字 说明 1 数据类型 bool 基本类型—-布尔类型 2 数据类型 char 基本类型—-字符类型 3 数据类型 wchar_t 基本类型—-宽字符类型 4 数据类型 double ...

  3. Java中从控制台输入数据的几种常用方法(转转)

    原文博客地址:https://www.cnblogs.com/SzBlog/p/5404246.html 一.使用标准输入串System.in  //System.in.read()一次只读入一个字节 ...

  4. Android-crop:漂亮的图片裁切工具

    主要特点 Gradle构建和AAR 现代化的UI 向后兼容到SDK 10 配置简单 示例项目 使用 首先,在你的 manifest 文件中申明 CropImageActivity : <acti ...

  5. Arduino UNO R3

    Arduino 常见型号 当然还有 LilyPad,附图: 最常见的自然是UNO,最新版是第三版R3: 国内也有一些改进的板子.我用的是一般的板子,拿到货也只能默默了. 简介 The Uno is a ...

  6. Spring AOP理解

    Spring的核心思想的IOC和AOP.最近学习AOP,对切面的学习有了进一步的认识. Spring用代理类包裹切面,把他们织入到Spring管理的bean中.也就是说代理类伪装成目标类,它会截取对目 ...

  7. window窗口操作

    打开新窗口 window.open([url],[窗口名称],[参数字符串]) window.open("http://baidu.com","_balnk", ...

  8. 克隆CentOS 6.9 配置静态IP,重启网络服务时报错

    克隆的CentOS 6.9 第一次开机时,VMware workstation会为新虚拟机自动生成新mac地址,导致虚拟机配置文件中mac地址与虚拟机新mac地址不一致. 解决方法:1. 修改网卡配置 ...

  9. MAVEN 的常用命令

    1.清除命令:mvn clean 2.编译命令:mvn  conpile 3.打包命令:mvn package 4.跳过单元测试命令:mvn clean package -Dmaven.test.sk ...

  10. OSI层次介绍

    1.应用层:为应用软件提供接口,使应用程序能够使用网络服务. 2.表示层:①数据的解码和编码,②数据的加密和解密,③数据的压缩和解压缩. 3.会话层:建立.维护.管理应用程序之间的会话. 功能:对话控 ...