[湖大热身赛]Tractor(数学?)

这篇博客探讨了Bessie如何在FJ的农场中移动并到达内部点的问题,通过分析其独特的移动规则来计算能够到达的点的数量。通过简化问题和找到规律,作者提供了一种解决方法来计算农场内可能到达的点数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

1. Each of her movements is in the same direction as either the positive x-axis or the positive y-axis.

2. Her nth movement takes her 2^(n−1) units forward in her chosen direction. (On her first movement, n = 1, so she moves 1 unit.)

Farmer John’s farm is on the coordinate plane, in the shape of a rectangle with corners at (0,0), (A,0), (0,B) and (A,B). If Bessie starts at (0,0), how many points inside the farm, including the boundary, could she reach?


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.

In the first test case of the sample, Bessie can reach the following six points: (0,0), (0,1), (1,0), (1,2), (2,1) and (3,0).


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;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值