题解:P8255 [NOI Online 2022 入门组] 数学游戏
Sharkat_Star · · 题解
题意简述
已知:
现给出 -1;若存在,输出
解题思路
显而易见的,这是一道数学题。
遇到数学题我们怎么办呢?
推!
所以,我们接下来就试着推一推这道题的答案。
首先拆解原式:
要想从一个式子中找到某些规律或性质,这个式子首先必须是直观易计算的。
原式中的
已经有了一个
由于
考虑把已知量放在一起,使等式一边为定值:
现在我们看到等式另外一边的两个值。
对于
对于
现在我们把这些信息略加组合:
可以发现,
有了新的已知量,反带回算式中:
现在,我们已经可以表示
由于都是整数运算,显然,当
我们还可以发现,由于上面关于
AC Code
#include<bits/stdc++.h>
using namespace std;
long long x,y,z;
int T;
inline long long read(){
char ch=getchar();
long long res=0;
int f=1;
while(!isdigit(ch)){
if(ch=='-'){
f=-1;
}
ch=getchar();
}
while(isdigit(ch)){
res=(res<<1)+(res<<3)+(ch^48);
ch=getchar();
}
return res*f;
}
inline void print(long long x){
if(x<0){
putchar('-');
x=-x;
}
if(x>9){
print(x/10);
}
putchar((x%10)^48);
}
int main(){
T=read();
while(T--){
x=read();
z=read();
if(z%x!=0){
y=-1;
}
else{
long long temp=__gcd(z/x,x*x),sq=sqrtl(temp);
if(sq*sq!=temp){
y=-1;
}
else{
y=z/(x*sq);
}
}
print(y);
putchar('\n');
}
return 0;
}
/*
____ _ _
| __| | | | | __ _ __/\__
| |__ | |___ _____ ___ | | / / _____ | | \ /
|__ | | _ | | _ | | _| | |/ / | _ | | | /____\
__| | | | | | | |_| | | | | |\ \ | |_| | | |
|____| |_| |_| |___|_| |_| |_| \_\ |___|_| |_| */