結果

問題 No.1498 Factorization from -1 to 1
ユーザー chocoruskchocorusk
提出日時 2021-05-04 03:12:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 261 ms / 3,000 ms
コード長 2,589 bytes
コンパイル時間 3,776 ms
コンパイル使用メモリ 190,520 KB
実行使用メモリ 11,916 KB
最終ジャッジ日時 2023-09-30 14:58:20
合計ジャッジ時間 7,877 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
11,916 KB
testcase_01 AC 47 ms
11,776 KB
testcase_02 AC 48 ms
11,724 KB
testcase_03 AC 213 ms
11,676 KB
testcase_04 AC 246 ms
11,776 KB
testcase_05 AC 259 ms
11,704 KB
testcase_06 AC 256 ms
11,916 KB
testcase_07 AC 259 ms
11,676 KB
testcase_08 AC 260 ms
11,660 KB
testcase_09 AC 261 ms
11,720 KB
testcase_10 AC 51 ms
11,704 KB
testcase_11 AC 49 ms
11,772 KB
testcase_12 AC 52 ms
11,668 KB
testcase_13 AC 49 ms
11,728 KB
testcase_14 AC 51 ms
11,676 KB
testcase_15 AC 48 ms
11,712 KB
testcase_16 AC 46 ms
11,664 KB
testcase_17 AC 47 ms
11,676 KB
testcase_18 AC 48 ms
11,672 KB
testcase_19 AC 49 ms
11,724 KB
testcase_20 AC 50 ms
11,656 KB
testcase_21 AC 47 ms
11,708 KB
testcase_22 AC 47 ms
11,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
const int MAX=100010;
bitset<MAX> isprime;
void sieve(){
    for(int i=3; i<MAX; i++, i++) isprime[i]=1;
    isprime[2]=1;
    for(int i=3; i<MAX; i++){
        if(isprime[i]){
            for(int j=(i<<1); j<MAX; j+=i) isprime[j]=0;
        }
    }
}
ll powmod(ll a, ll k, ll MOD){
    ll ap=a, ans=1;
    while(k){
        if(k&1){
            ans*=ap;
            ans%=MOD;
        }
        ap=ap*ap;
        ap%=MOD;
        k>>=1;
    }
    return ans;
}
ll modsqrt(int p, int a){ //存在しないとき-1
	if(a==0) return 0;
	int q=p-1, s=0;
	while((q&1)==0){
		q>>=1;
		s++;
	}
	int z=2;
	while(1){
		if(powmod(z, (p-1)/2, p)==p-1) break;
		z++;
	}
	ll c=powmod(z, q, p);
	ll r=powmod(a, (q+1)/2, p), t=powmod(a, q, p);
	int m=s;
	while(t>1){
		ll tp=t;
		int k=-1;
		for(int i=1; i<m; i++){
			tp=tp*tp%p;
			if(tp==1){
				k=i; break;
			}
		}
		if(k==-1) return -1;
		ll cp=c;
		for(int i=0; i<m-k-1; i++) cp=cp*cp%p;
		c=cp*cp%p;
		t=c*t%p;
		r=cp*r%p;
		m=k;
	}
	return r;
}
ll v[100010];
vector<ll> f[100010];
int main()
{
    sieve();
    for(ll i=1; i<=100001; i++){
        v[i]=i*i+1;
    }
    for(int i=2; i<=100001; i++){
        if(!isprime[i] || i%4==3) continue;
        if(i==2){
            for(int j=1; j<=100001; j+=2){
                while(v[j]%2==0){
                    v[j]>>=1;
                    f[j].push_back(2);
                }
            }
            continue;
        }
        ll r=modsqrt(i, i-1);
        for(int j=r; j<=100001; j+=i){
            while(v[j]%i==0){
                v[j]/=i;
                f[j].push_back(i);
            }
        }
        r=i-r;
        for(int j=r; j<=100001; j+=i){
            while(v[j]%i==0){
                v[j]/=i;
                f[j].push_back(i);
            }
        }
    }
    for(int i=1; i<=100001; i++) if(v[i]>1) f[i].push_back(v[i]);
    int q; cin>>q;
    for(int i=0; i<q; i++){
        int x; cin>>x;
        for(auto p:f[x]) cout<<p<<" ";
        cout<<endl;
    }
    return 0;
}
0