Tractor |
Time Limit: 6000ms, Special Time Limit:15000ms, Memory Limit:65536KB |
Total submit users: 15, Accepted users: 13 |
Problem 13241 : No special judgement |
Problem description |
Bessie the Cow has stolen Farmer John’s tractor and is running wild on the coordinate plane! She, however, is a terrible driver, and can only move according to the following rules: |
Input |
The input begins with an integer N (1 ≤ N ≤ 100) on a line by itself, indicating the number of test cases that follow. Each of the following N lines contains two space separated integers A and B (1 ≤ A,B ≤ 10^8), describing the upper-right corner of Farmer John’s farm. |
Output |
Output N lines, with the Nth line containing the number of points that Bessie could possibly reach in the Nth test case. |
Sample Input |
2 2 3 7 7 |
Sample Output |
6 15 |
刚开始这题给的是错误的。说什么第n次移动是2*n-1,纠结题意了半天,后来darkdream发现其实是2^(n-1)好吧。然后就找规律了。
想着脑袋有点懵,但是感觉还是找对了方向,仔细推一下还是能解出来的,可能不是最优的那种把。
矩形取最短边,然后看最接近2^(n)-1,然后这里就是一个完整的二叉树=2*t+1;(t是边长了),然后看最短边还剩下几个,肯定每条边上是能到一个点的,然后就是长边减去二叉树叶子节点的个数,然后以2的次方递增能加上几个叶子节点+最短边剩下的边的个数,好吧,解释的有点乱,估计也就我自己能看懂了。但是最后还是AC了。这种方法也没错。据说应该有更优的把。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define eps 1e-8
#define pi acos(-1)
using namespace std;
int main(){
int T,a,b,i;
scanf("%d",&T);
while (T--){
int ans=0;
scanf("%d%d",&a,&b);
if (a>b){
a=a+b;
b=a-b;
a=a-b;
}
int g=a;
int t=0;
int s=1;
for (i=1;g>=s;i++){
g-=s;
s*=2;
t++;
}
ans=(s-1)*2+1+g;
b=b-(s-1);
int k=s;
while (b&&k){
ans++;
b--;
k--;
}
k=s+g;
s*=2;
while(b){
if (b>=s){
ans+=k;
b-=s;
}
else {
if (b+k>s) ans=ans+(b+k-s);
break;
}
s*=2;
}
cout<<ans<<endl;
}
return 0;
}