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. QlikSense系列(2)——QlikSense安装和升级

    继上篇对QlikSense进行总体介绍之后,想必大家想体验下产品,下面介绍安装的过程和注意点. 注:我的系统环境Windows Server 2008R2,QlikSense版本为3.1SR1 Qli ...

  2. nginx 集群简述

    1.负载均衡介绍: 负载均衡是由多台服务器以对称的方式组成一个服务器集合,每台服务器都具有等价的地位,都可以单独对外提供服务而无须其他服务器的辅助.其工作模式为将外部发送来的请求均匀分配到对称结构中的 ...

  3. 原型模式(Prototype)C++实现

    意图:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 实用性:1.当要实例化的类是在运行时刻指定时. 2.为了避免创建一个与产品类层次平行的工厂类层次时. 3.当一个类的实例只能有几 ...

  4. map 和 for

    一.map 函数封装. ; [,,,,,,,].map(function(elem){ sum += elem; }) console.log('sum='+sum); 二.平时用for的写法. ,, ...

  5. vs中的正则替换

    老版本: String {\w+} => Public String \1 2012中: String (\w+) => Public String $1

  6. 基于连接的Java网络编程

    实现了基于TCP的Java Socket编程,功能很简单:客户端向服务器端输出一名话"connect",服务器端接收输出到控制台并向客户端输出一名话"Hello" ...

  7. Caffe+UbuntuKylin14.04_X64+CUDA 6.5配置

    在编译Caffe的漫长过程中,经过了一个又一个坑,掉进去再爬出来,挺有趣的.对比原文有修改! LInux下配置安装:(本文档使用同一块NVIDIA显卡进行显示与计算, 如分别使用不同的显卡进行显示和计 ...

  8. mvc cshtml 中赋值

    @{ var str = ""; str = item.ApplyStatus == ? ? ? ? "申请驳回" : ""; } < ...

  9. Zbrush 4R7中的镜像功能是怎么使用的?

    ZBrush一款3D图形绘制软件,功能十分强大,在雕刻建模的时候镜像工具是我们经常要用到的,它可以方便快捷雕刻对称的模型,那么ZBrush®中怎样镜像呢,本文小编将做详细介绍. ZBrush 3D镜像 ...

  10. C#学习 第六节

    什么是类型(Type)? 类型在C#中的作用 C#语言的类型系统 变量.对象与内存 类型(Type):数据类型 性质相同的值得集合:内存:内部存储单元,计算机运行程序的空间:外存:扩展存储器,硬盘: ...