神枪手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;
}