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]);

最新文章

  1. Responsive设计——不同设备的分辨率设置
  2. Microsoft Azure 云存储服务概念
  3. MySql学习 (一) —— 基本数据库操作语句、三大列类型
  4. 解决:NoSuchAlgorithmException: Algorithm HmacSHA1 not available
  5. 【C#】第3章学习要点(一)--整体把握
  6. 《慕客网:IOS动画案例之会跳动的登入界面(下)》学习笔记 -Sketch的使用
  7. hdu4638 group 树状数组
  8. 搜索引擎的提示效果完整的JavaScript代码
  9. 如何将你牛逼的iOS代码分享到CocoaPod(转)
  10. EasyUI - 后台管理系统 - 增加,删除,修改
  11. 乐在其中设计模式(C#) - 状态模式(State Pattern)
  12. 201521123097《Java程序设计》第九周学习总结
  13. Redis学习笔记之二 :在Java项目中使用Redis
  14. 浅谈python的对象的三大特性之继承
  15. 中断下半部处理之tasklet
  16. MySQL的常用操作命令详解
  17. Android为什么使用Binder-android学习之旅(101)
  18. Python之find命令中的位置的算法
  19. [Ting&#39;s笔记Day3]解决Git常见错误non-fast-forward问题
  20. ECharts图表引用json数据

热门文章

  1. C#中关于XML与对象,集合的相互转换
  2. 初学 Ajax(涉及 php)
  3. Android适配文件dimen自动生成代码
  4. 【SQL】SELECT 语句
  5. 应用四:Vue之VUEX状态管理
  6. 超实用的JavaScript代码段
  7. 采用requests库构建简单的网络爬虫
  8. Shoot the Bullet ZOJ - 3229有上下界网络流
  9. python与图灵机器人交互(WXPY版本)
  10. jmeter录制