Problem H: Eat Candy
Time Limit: 1 Sec
Memory Limit: 128 MB
Submit: 0
Solved: 0
Description
There is a box with infinite volume. At first there are n candies 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+7 seconds?
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和一个k,每秒执行一次n=n/2+k(四舍五入),问1000000007秒之后n为多少
因为操作固定,所以多次操作之后,n肯定会朝某个数单方向趋于一个稳定值,那我们只需要判断什么时候达到这个稳定值就可以了
简单一点的做法就是用STL里的map来标记已经出现过的数,当第二次访问到某一个数的时候就是那个稳定值
/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 100005;
const int M = 40;
const int inf = 100000000;
const int mod = 2009;
map<int,bool> m;
int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
m.clear();
while(!m[n])
{
m[n]=true;
n/=2;
n+=k;
}
printf("%d\n",n);
}
return 0;
}
/**************************************************************
Problem: 19
Language: C++
Result: Accepted
Time:17 ms
Memory:1512 kb
****************************************************************/
菜鸟成长记