题目原址

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. 使用node.js + socket.io + redis实现基本的聊天室场景

    在这篇文章Redis数据库及其基本操作中介绍了Redis及redis-cli的基本操作. 其中的publish-subscribe机制应用比较广泛, 那么接下来使用nodejs来实现该机制. 本文是对 ...

  2. chrome 浏览器插件开发(二)—— 通信 获取页面变量 编写chrome插件专用的库

    在chrome插件的开发过程中,我遇到了一些问题,在网上找了不少文章,可能是浏览器升级的原因,有一些是有效的也有无效的.下面我简单的分享一下我遇到的坑,以及我把这些坑的解决方案整理而成的js库 —— ...

  3. AngularJS THML DOM

    AngularJS为HTML Dom元素属性提供了绑定应用数据的指令. data-ng-disabled指令直接提供了绑定应用程序的数据到HTML元素的disabled属性. <!DOCTYPE ...

  4. 瓣呀,一个基于豆瓣api仿网易云音乐的开源项目

    整体采用material design 风格,本人是网易云音乐的粉丝,所以界面模仿了网页云音乐,另外,项目中尽量使用了5.0之后的新控件. 项目整体采用mvp+rxjava+retrofit 框架,使 ...

  5. 详解LinkedHashMap如何保证元素迭代的顺序

    大多数情况下,只要不涉及线程安全问题,Map基本都可以使用HashMap,不过HashMap有一个问题,就是迭代HashMap的顺序并不是HashMap放置的顺序,也就是无序.HashMap的这一缺点 ...

  6. ios核心动画(基础动画)

    一.简单介绍 CAPropertyAnimation的子类 属性解析: fromValue:keyPath相应属性的初始值 toValue:keyPath相应属性的结束值 随着动画的进行,在长度为du ...

  7. js、jquery初始化加载顺序

    // ready 这个方法只是在页面所有的DOM加载完毕后就会触发 // 方式1 $(function(){ // do something }); // 方式2 $(document).ready( ...

  8. Mybatis与Hibernate区别

    Mybatis与Hibernate区别 mybatis: 1. 入门简单,即学即用,提供了数据库查询的自动对象绑定功能,而且延续了很好的SQL使用经验,对于没有那么高的对象模型要求的项目来说,相当完美 ...

  9. linux下Tomcat配置提示权限不够解决办法

    在终端输入命令 sudo chmod -R 777 /opt/Tomcat,那么Tomcat文件夹和它下面的所有子文件夹的属性都变成了777(读/写/执行权限)

  10. 访问远程mysql数据库,出现报错,显示“1130 - Host'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server“

    在使用Navicat for MySQl访问远程mysql数据库,出现报错,显示“1130 - Host'xxx.xxx.xxx.xxx' is not allowed to connect to t ...