Eat Candy(暴力,水)
2016-05-17 14:16
351 查看
Eat Candy
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 8 Solved: 6
[Submit][Status][Web Board]
Description
There is a box with infinite volume. At first there are ncandies in the box. Then every second you will eat some candies, left half of candies (round down) in the box. Then add k candies into the box. How many candies there are in the box after 109+7seconds?Input
There are multiple test cases. In each test case, there are only one line contains two integers n,k(1≤n,k≤109+7)Output
For each test case, output the answer in one line.Sample Input
4 5 2 3
Sample Output
9 5
HINT
In the first test case:1st second, 4->2, 2+5 = 7
2nd second, 7->3, 3+5 = 8
3rd second, 8->4, 4+5 = 9
4th second, 9->4, 4+5 = 9
…
1000000007th 9
So there are 9 candies in the box after 1000000007 seconds.
题解:每次n为n的一半加k,问经过1e9+7次后的值;
代码:
import java.util.Scanner; public class EatCandy { public static void main(String[] args){ int n, k; Scanner cin = new Scanner(System.in); while(cin.hasNext()){ n = cin.nextInt(); k = cin.nextInt(); while(n != (n/2 + k)){ n = n / 2 + k; } System.out.println(n); } } }
相关文章推荐
- JS中的call()和apply()方法
- SQL编程:合并字符串不求人 ---> group_concat函数就能行
- 买卖股票的最佳时机 II
- 大数 (整数)乘法,除法
- c:forEach varStatus属性
- 正则表达式语法
- AFNetwork 作用和用法详解
- C#验证子网掩码的正确性
- IBM X3650 服务器更换内存的过程记录
- 一些关于dagger2的理解(二)
- git开源库——视图控件——左侧抽屉侧边栏
- iOS雷达图 iOS RadarChart实现
- JavaScript 设置、读取Cookie
- 推荐系统概论
- 【第十二章】零配置 之 12.1 概述 ——跟我学spring3
- 【bug记录】mysql5.7.9-win64 的3534问题
- 跟我学机器视觉-HALCON学习例程中文详解-FUZZY检测用于开关引脚测量
- PAT乙级—1029. 旧键盘(20)-native
- 英伟达GPU 嵌入式开发平台
- [转发]Oauth 1.0 1.0a 和 2.0 的之间的区别有哪些?