|
题意是给你一个64位整型n
让你找出满足条件n%x==0且x可以表示为一个整数的平方的最大x并输出
看到群里面有人说这个题是Miller Roban的模板题
但是好像也有比较巧妙的方法来做
在别人的代码里找到了一份答案,大概是看懂了
可以这样思考:
把n分解为i 和 n/i
其中i的范围为[1, n^(1/3)],那么n/i的范围局势[n^(2/3), n]
我们采取这样的思路来做题:
首先判断n/i是否是一个平方数,如果是则判断当前的ans与n/i的大小,令ans取较大值
接着在判断(n/i)%i==0?
如果是,则说明n%(i*i) == 0
此时再判断ans 与 i*i的大小,令ans取较大值
依次遍历i即可保证遍历了n所有因子
代码如下:
#line 5 "SquareDivisor.cpp"
#include <map>
#include <set>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <cstdio>
#include <iostream>
#include <algorithm>
#define MAXN 10010
#define LL long long
#define INF 0x7fffffff
using namespace std;
class SquareDivisor {
public:
long long biggest(long long n) {
LL ret = 1;
for(LL i=1; i*i*i<=n; ++i) {
if(n % i == 0) {
LL tmp = n/i;
LL tg = sqrt(tmp*1.0)+0.5;
if(tg*tg == tmp) {
if(tmp > ret)
ret = tmp;
}
if(tmp % i == 0) {
tg = i*i;
if(tg > ret)
ret = tg;
}
}
}
return ret;
}
};
//this is for test
int main(void) {
LL n;
SquareDivisor test;
while(cin >> n)
cout << test.biggest(n) << endl;
return 0;
}
//test end