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 ...
随机推荐
- ShowDialog函数与Form的Activated函数同时使用的陷阱
当我们需要在form启动之时,焦点显示在特定的控件“btn”上,我们可以先将btn的TabIndex设为0,然后要确保它visible=true,最后在Form的Activated事件方法中btn.G ...
- 第九课: - 导出到CSV / EXCEL / TXT
第 9 课 将数据从microdost sql数据库导出到cvs,excel和txt文件. In [1]: # Import libraries import pandas as pd import ...
- Java入门第一季——从此投身Java??
找工作告一段落. 最后的工作呢,和java紧密相关,也是阴差阳错,不过都是软件开发,都好了,不过以后侧重点肯定是在java这边,php有机会还是一直学下去的,那么美的说~ Java开发第一季 一.简 ...
- Jquery数字转盘:
项目中,在充值流程中,加入了1个抽奖环节,需要转盘显示抽中的虚拟货币.网上找了相关的特效,最后锁定在这个特效上:http://www.jb51.net/jiaoben/319636.html.因为用的 ...
- Java中类的定义
成员变量:对应事物的属性 成员方法:对应事物的行为 类定义的格式 定义类:就是定义类的成员,包括成员变量和成员方法 成员变量:和以前定义变量几乎是一样的.只不过位置发生了改变.在类中,方法外. 成员方 ...
- Elasticsearch学习(一)————简单命令
Elasticsearch一.简介**Elasticsearch 是一个分布式的搜索和分析引擎,可以用于全文检索.结构化检索和分析,并能将这三者结合起来.Elasticsearch 基于 Lucene ...
- 路飞学城Python-Day107
88-Ajax简介 Ajax是前端的JS技术,目前向服务器发送请求是通过1.向浏览器的地址栏发送请求的方式:2.form表单的请求方式是两种get和post方式:3.a标签的href属性对接地址 是一 ...
- awk手册
awk 手册 简体中文版由bones7456 (bones7456@gmail.com)整理. 原文:应该是 http://phi.sinica.edu.tw/aspac/reports/94/940 ...
- POJ 2115 C Looooops( 简单拓欧 + 快速幂 )
链接:传送门 题意:题目中给出一个循环 for (variable = A; variable != B; variable += C) ,这个东东还需要 mod 2^k 问至少多次能退出,如果进入死 ...
- [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper (状态压缩DP)
不打算把题目放着,给个空间传送门,读者们自己去看,传送门(点我) . 这题是自己做的第一道状态压缩的动态规划. 思路: 在这题中,我们设f[i]为i在二进制下表示的那些牛所用的最小电梯数. 设g ...