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号位置
开始新的循环。
这就是满足要求的数字。
题目没什么难度,就是确认下以后二分的写法
/*
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的更多相关文章
- USACO Section2.2 Runaround Numbers 解题报告 【icedream61】
runround解题报告---------------------------------------------------------------------------------------- ...
- 洛谷P1467 循环数 Runaround Numbers
P1467 循环数 Runaround Numbers 89通过 233提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 循环数是 ...
- USACO Runaround Numbers 模拟
根据题意的 Runaround 规则去找比当前数大的最近的一个 Runaround数字 模拟题~ Source code: /* ID: wushuai2 PROG: runround LANG: C ...
- 【USACO 2.2】Runaround Numbers
找出第一个大于n的数满足:每一位上的数都不同,且没有0,第一位开始每次前进当前这位上的数那么多位,超过总位数就回到开头继续往前进,最后能不能每个位都到过一次且回到第一位,$n<10^9$. 暴力 ...
- USACO Section 2.2 循环数 Runaround Numbers
OJ:http://www.luogu.org/problem/show?pid=1467 #include<iostream> #include<vector> #inclu ...
- USACO Section 2.2: Runaround Numbers
简单题 /* ID: yingzho1 LANG: C++ TASK: runround */ #include <iostream> #include <fstream> # ...
- USACO Runaround Numbers
题目大意:问最近的比n大的循环数是多少 思路:第n遍暴力大法好 /*{ ID:a4298442 PROB:runround LANG:C++ } */ #include<iostream> ...
- 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 ...
- p1467 Runaround Numbers
直接搜就行. #include <iostream> #include <cstdio> #include <cmath> #include <algorit ...
随机推荐
- IO流读取文件内容时,出现空格的问题(未找到原因)
import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOExceptio ...
- 优动漫PAINT个人版和EX版本差异
优动漫PAINT是一款功能强大的动漫绘图软件,适用于个人和专业团队创作,分为个人版和EX版,那么这两个版本有什么区别,应该如何去选择呢? 优动漫PAINT个人版即可满足基本的绘画创作需求,EX版在个人 ...
- Java的编译与运行
编译: 是指将我们编写的Java源文件翻译成JVM认识的 .class 文件, 在这个过程中,javac 编译器会检查我们所写的程序是否有错误,有错误就会提示出来,如果没有错误就会编译成功. 运行: ...
- mysql 最大连接数
方式一: 一次性修改 服务重启后还原 查看 show variables like 'max_connections%'; 修改 set GLOBAL max_connections=1024; ...
- vc++如何创建程序-构造和继承
#include<iostream.h>//定义一个动物类class Animal{public: void eat();//添加方法 { cout<<"animal ...
- JS 20180416考试
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- centos7编译安装mysql5.6
先安装如下依赖包: $ yum -y install make gcc-c++ cmake bison-devel ncurses-devel 下载MySQL5.6.14安装包,https://pa ...
- Linux的mysql搭建
1.centos7默认安装mariadb数据库 #yum remove mariadb* 2.wget mysql数据库地址 如果是普通用户,请提权 sudo提权 3.yum local ins ...
- Python JSON - 世界人口图
世界人口图 从https://datahub.io/网站搜索population,下载世界人口json数据. from pygal.maps.world import COUNTRIES def ge ...
- 解析xml文件,遍历输出xml文件中的所有节点, 最终模仿实现struts2框架自动封装参数的功能
程序结构:src文件夹下存放xml文件 该文件内容: <?xml version="1.0" encoding="UTF-8"?> <myst ...