题目原址

http://codeforces.com/contest/1082/problem/A

题目内容

一共n页书,现在位于第x位,想要看第y页,每次只能翻d页,注意总能翻到第1页和第n页。

Vasya想知道自己能否在经过无数次翻书后 看到第y页的内容。

如果可以,请给出最少需要翻几次书。

题目解析

类似一个模除的问题。

首先思考无法翻到第y页的情况,即:

①直接翻翻不到

②在到达第1页和第n页后也翻不到

对应的式子引出的是:

直接翻不到:

abs(y - x) % d != ;

第1页翻不到:

abs(y - ) % d != 

第n页翻不到:

abs(n - y) % d != 

因此翻不到的情况很好判断。注:以下简记 左不通 和 右不通 分别代表第一页翻不到和第n页翻不到。

再看最小翻阅次数,这里可以详尽的判断所有情况,也可以统一完成,本人采用的是详尽的判断。

将所有可能性列举出来,即左不通而右通,那么最短翻阅只有右侧的一种方式。

对应的代码部分为:

if(abs(y - ) % d != ){    //左侧不通
//int temp = need(x, y, d);
if(abs(n - y) % d != ){ //右侧不通
puts("-1");
continue;
}
else {
count += need(x, n, d);
count += need(n, y, d);
cout << count << endl;
continue;
}
}

其中,need()函数用来计算从x到y在每次翻阅d的情况下直接到达需要的翻阅次数,如下:

int need(int x, int y, int d){    //3 - 6 3
if(abs(y - x) % d == ){
return abs(y - x) / d;
}
else {
return abs(y - x) / d + ;
}
}

当右侧不通,而左侧通的时候,情况类似上述:

else if(abs(n - y) % d != ){    //右侧不通
//int temp = need(x, y, d);
count += need(x, , d);
count += need(, y, d);
cout << count << endl;
continue;
}

而当左右都可通过的时候,只需计算左右两种方式中翻阅数的最小者即可。

else{
int left = need(x, , d) + need(, y, d);
int right = need(x, n, d) + need(n, y, d);
count = min(left, right);
cout << count << endl;
continue;
}

题目也可以将不通返回正无穷,从而在返回最小值的过程中失效,减少判断。

题目总结

很简单的题目,没有考察到算法知识,只是考验编程者对于多种判断情况的处理。做题时应先想好思路再完成。

A. Vasya and Book的更多相关文章

  1. Milliard Vasya's Function-Ural1353动态规划

    Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning mathematician. He decided to make ...

  2. CF460 A. Vasya and Socks

    A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. 递推DP URAL 1353 Milliard Vasya's Function

    题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...

  4. Codeforces Round #281 (Div. 2) D. Vasya and Chess 水

    D. Vasya and Chess time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分

    C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. codeforces 676C C. Vasya and String(二分)

    题目链接: C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input sta ...

  7. Where is Vasya?

    Where is Vasya? Vasya stands in line with number of people p (including Vasya), but he doesn't know ...

  8. Codeforces Round #324 (Div. 2) C. Marina and Vasya 贪心

    C. Marina and Vasya Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/pr ...

  9. Codeforces Round #322 (Div. 2) A. Vasya the Hipster 水题

    A. Vasya the Hipster Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/p ...

  10. Codeforces Codeforces Round #319 (Div. 2) C. Vasya and Petya's Game 数学

    C. Vasya and Petya's Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...

随机推荐

  1. 20145238-荆玉茗 《Java程序设计》第9周学习总结

    20145238第九周<Java学习笔记> 第十六章 整合数据库 JDBC入门 ·数据库本身是个独立运行的应用程序 ·撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 ...

  2. MyBatis的优缺点以及特点

    特点: mybatis是一种持久层框架,也属于ORM映射.前身是ibatis. 相比于hibernatehibernate为全自动化,配置文件书写之后不需要书写sql语句,但是欠缺灵活,很多时候需要优 ...

  3. Subversion简介

    作为一名编程人员,SVN经常作为代码.项目的版本控制,殊不知SVN也可作为其他领域的版本控制,例如对文档.音频.视频等 . SVN可以看成一种文件系统,为了使工作人员提高工作效率,可以进行并行的工作, ...

  4. java mysql多次事务 模拟依据汇率转账,并存储转账信息 分层完成 dao层 service 层 client层 连接池使用C3p0 写入库使用DBUtils

    Jar包使用,及层的划分 c3p0-config.xml <?xml version="1.0" encoding="UTF-8"?> <c3 ...

  5. 【iOS】史上最全的iOS持续集成教程 (上)

    :first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...

  6. JQuery发起ajax请求,并在页面动态的添加元素

    页面html代码: <li> <div class="coll-tit"><span class="coll-icon">& ...

  7. 通过ABAP程序创建透明表

    最近在解决用户账号问题的时候,需要通过ABAP程序创建透明表,查询了相关资料,总结如下. 通过ABAP程序创建透明表,主要利用了4个函数: DDIF_TABL_ACTIVATE: 激活透明表 GOX_ ...

  8. 转:SpringCloud服务注册中心比较:Consul vs Zookeeper vs Etcd vs Eureka

    原文链接地址:http://luyiisme.github.io/2017/04/22/spring-cloud-service-discovery-products/ 这里就平时经常用到的服务发现的 ...

  9. Maven - 修改本地仓库位置

    默认的本地仓库是在:当前的用户目录/.m2/repository 修改位置: 1. 打开maven的conf/settings.xml,找到如下图这一段: 2. 把<localRepositor ...

  10. thinkphp 跳转外网代码(php通用)

    thinkphp 提供了一个重定向但是在跳转外部网站的时候就会比较麻烦 下面一种方法还不错, < ?php //重定向浏览器 header("Location: http://www. ...