結果
| 問題 |
No.300 平方数
|
| コンテスト | |
| ユーザー |
alpha_virginis
|
| 提出日時 | 2015-12-27 16:26:01 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 7 ms / 1,000 ms |
| コード長 | 4,150 bytes |
| コンパイル時間 | 1,198 ms |
| コンパイル使用メモリ | 162,480 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-09-19 07:24:05 |
| 合計ジャッジ時間 | 2,768 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 43 |
ソースコード
#include <bits/stdc++.h>
#include <math.h>
#include <stdint.h>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
const int INF = (1 << 28);
const long INFL = (1LL << 50);
class MillerRabin {
public:
u64 modmul_64(u64 a, u64 b, u64 mod) {
u64 res = 0;
a %= mod;
for(s32 i = 63; i >= 0; --i) {
res = (res << 1) % mod;
if( ((b >> i) & 0x01) == 1 ) {
res = (res + a) % mod;
}
}
return res;
}
u64 modpow_64(u64 a, u64 b, u64 mod) {
u64 res = 1, p = a;
a %= mod;
b %= mod;
while( b != 0 ) {
if( (b & 0x01) != 0 ) {
res = modmul_64(res, p, mod);
}
p = modmul_64(p, p, mod);
b >>= 1;
}
return res;
}
bool operator () (u64 N) {
u32 k = 16;
if( N == 2 ) return true;
if( N <= 1 or N % 2 == 0 ) return false;
u64 d = (N - 1);
u64 s = 0;
while( d % 2 == 0 ) {
d >>= 1;
s += 1;
}
for(s32 i = 0; i < k; ++i) {
u64 a = (((u64)rand() << 32) | (u64)rand()) % (N - 2) + 1;
u64 r = modpow_64(a, d, N);
if( r == 1 ) continue;
if( r == N - 1 ) continue;
for(s32 j = 1; j < s; ++j) {
r = modpow_64(r, 2, N);
if( r == N - 1 ) {
goto label_1;
}
}
return false;
label_1:;
}
return true;
}
};
template<typename T>
void Swap(T& arg1, T& arg2) {
T temp = arg1;
arg1 = arg2;
arg2 = temp;
}
u32 gcd(u32 a, u32 b) {
if( a < b ) Swap(a, b);
while( b != 0 ) {
a = a % b;
Swap(a, b);
}
return a;
}
u64 gcd(u64 a, u64 b) {
if( a < b ) Swap(a, b);
while( b != 0 ) {
a = a % b;
Swap(a, b);
}
return a;
}
template<typename T>
T gcd(T a, T b) {
if( a < b ) Swap(a, b);
while( b != 0 ) {
a %= b;
Swap(a, b);
}
return a;
}
class PollardsRho {
public:
u64 modmul_64(u64 a, u64 b, u64 mod) {
u64 res = 0;
a %= mod;
for(s32 i = 63; i >= 0; --i) {
res = (res << 1) % mod;
if( ((b >> i) & 0x01) == 1 ) {
res = (res + a) % mod;
}
}
return res;
}
u64 operator () (u64 N) {
u64 x = 2;
u64 y = 2;
u64 d = 1;
while( d == 1 ) {
x = modmul_64(x, x, N);
u64 ty = modmul_64(y, y, N);
y = modmul_64(ty, ty, N);
d = gcd(llabs(x - y), N);
}
if( d == N ) {
return 0;
}
return d;
}
};
class PrimeFactorization {
public:
u64 primes_[1024];
u64 p_num_[1024];
u64 num_;
bool isnotprime[1000000];
public:
void operator () (u64 N) {
num_ = 0;
//for(s32 i = 0; i < 1000000; ++i) isprime[i] = true;
isnotprime[0] = isnotprime[1] = true;
for(s32 i = 0; i < 1000; ++i) {
if( not isnotprime[i] ) {
for(s32 j = i + i; j < 1000000; j+=i) {
isnotprime[j] = true;
}
}
}
for(s32 i = 2; i < 1000000; ++i) {
if( isnotprime[i] ) continue;
if( N % i == 0 and N != 0 ) {
primes_[num_] = i;
p_num_[num_] = 1;
num_ += 1;
N /= i;
while( N % i == 0 and N != 0 ) {
p_num_[num_ - 1] += 1;
N /= i;
}
}
}
if( N == 1 ) return;
split(N);
}
void split(u64 N) {
if( N == 1 ) return;
MillerRabin mr;
for(s32 i = 0; i < num_; ++i) {
if( N % primes_[i] == 0 ) {
p_num_[i] += 1;
split(N / primes_[i]);
return;
}
}
u64 root = (u64)sqrt(N);
if( root * root == N ) {
split(root);
split(root);
return;
}
if( mr(N) ) {
primes_[num_] = N;
p_num_[num_] = 1;
num_ += 1;
return;
}
u64 a, b;
split2(N, a, b);
split(a);
split(b);
}
void split2(u64 N, u64& a, u64& b) {
PollardsRho pr;
a = pr(N);
b = N / a;
}
};
int main() {
PrimeFactorization pf;
long N;
std::cin >> N;
pf(N);
long res = 1;
for(int i = 0; i < pf.num_; ++i) {
if( pf.p_num_[i] % 2 != 0 ) {
res *= pf.primes_[i];
}
}
std::cout << res << std::endl;
return 0;
}
alpha_virginis