题解:P11496 [ROIR 2019 Day 1] 完全平方
题目分析
若
若
若
注意判断无解。时间复杂度为
参考代码
#include<iostream>
#define int long long
using namespace std;
int k;
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>k;
if(k==0) cout<<0;
else if(k>0){
int ans=1e18;
for(int i=1;i*i<=k;i++){
if(k%i==0){
if((i+k/i)%2==0) ans=min(ans,(i+k/i)/2);
}
}
if(ans==1e18) cout<<"none";
else cout<<ans;
}else{
k*=-1;
int ans=1e18;
for(int i=1;i*i<=k;i++){
if(k%i==0){
if((i+k/i)%2==0) ans=min(ans,k/i-(i+k/i)/2);
}
}
if(ans==1e18) cout<<"none";
else cout<<ans;
}
return 0;
}