HZNUOJ2803神枪手JoneySun (Easy Version)(二维数组前缀和)

神枪手JoneySun (Easy Version)

Description
Easy Version 和 a little bit hard Version 只有数据范围的区别

JoneySun被邪恶的wifepie困在了一个由1×1的网格分割的监狱里,邪恶的wifepie为了看JoneySun的笑话,在监狱里的某处监视着JoneySun,幸运的是JoneySun手中有一把枪,不幸的是wifepie掌握了分身术。

我们认为这个监狱是一个2n×2m的网格,JoneySun被困在最中间,除了JoneySun所在处,网格其余的交点上都有一个wifepie的分身。JoneySun的枪射程是无限远,如果子弹射中分身,那么分身会消失,子弹继续沿直线行进。如果子弹射中wifepie本体,wifepie就会被JoneySun消灭。JoneySun想知道消灭wifepie最多需要多少子弹。

Input
第一行一个整数T(1≤T≤100)表示共有T组输入

第二行两个整数n,m(1≤n,m≤103),含义见题面

Samples
input
1
1 1
output
8
Hint

如图所示,红点表示JoneySun所在位置,蓝点表示wifepie的分身,JoneySun最多需要8颗子弹就可以消灭wifipie所有的分身
在这里插入图片描述

请注意复杂度,T很大,请试试你能想到的所有玄学优化

思路: 转化到第一象限计算,想要不重复,只要累计gcd值为1的就可以了,但存储的时候要利用二维前缀和,在查询的时候可以达到O(1)的时间复杂度,很优秀。

#include<list>
#include<string.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e3 + 10;
const int inf = 0x3f3f3f3f;
const int Base =131;
const ll INF = 1ll << 62;
//const double PI = acos(-1);
const double eps = 1e-7;
const int mod = 1e9+7;
#define speed {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); }

inline int gcd(int a, int b)
{
	while (b ^= a ^= b ^= a %= b);
	return a;
}

int save[maxn][maxn];//二维数组前缀和
void init()
{
	for(int i=1;i<maxn;i++)
	{
		for(int j=1;j<maxn;j++)
		{
			int f=0;
			if(gcd(i,j)==1)f=1;
			save[i][j]=save[i-1][j]+save[i][j-1]-save[i-1][j-1]+f;
		}		
	}
}

int main()
{
	int T;
	init();
	scanf("%d",&T);
	while(T--)
	{
		int n,m;
		scanf("%d %d",&n,&m);
		int res=save[n][m];
		res*=4;
		res+=4;
		printf("%d\n",res);
	}
	//system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值