結果

問題 No.1255 ハイレーツ・オブ・ボリビアン
ユーザー okok
提出日時 2020-10-10 01:48:19
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 845 ms / 2,000 ms
コード長 2,309 bytes
コンパイル時間 5,117 ms
コンパイル使用メモリ 106,596 KB
実行使用メモリ 5,740 KB
最終ジャッジ日時 2023-09-30 14:46:39
合計ジャッジ時間 8,570 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 5 ms
4,376 KB
testcase_08 AC 395 ms
5,672 KB
testcase_09 AC 423 ms
5,640 KB
testcase_10 AC 424 ms
5,648 KB
testcase_11 AC 406 ms
5,740 KB
testcase_12 AC 415 ms
5,740 KB
testcase_13 AC 38 ms
5,712 KB
testcase_14 AC 845 ms
5,732 KB
testcase_15 AC 400 ms
5,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>
#include<map>
#include<unordered_map>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;

void pi(vector<int> &A){
    int N = A.size()/2;
    vector<int> B(N * 2);
    
    for(int i = 0; i < N; i++){
        B[i * 2] = A[i];
        B[i * 2 + 1] = A[N + i];
    }
    
    swap(A, B);
}

int gcd(int a, int b){
	return a % b ? gcd(b, a % b) : b;
}

int lcm(int a, int b){
	return a / gcd(a, b) * b;
}

long long power(long long x, long long n, long long mod){
	long long ans = 1;
	for(;n;n>>=1,x*=x,ans%=mod,x%=mod)
		if(n&1)ans*=x;
	return ans%mod;
}

void exgcd(long long a, long long b, long long &x, long long &y){
	if(b == 0){
		x = 1;
		y = 0;
		return ;
	}
	exgcd(b, a % b, y, x);
	y -= a / b * x;
}
	
long long modlog(long long a, long long b, long long mod){
	
    int ll = 1;
    
	a %= mod, b %= mod;
	
	long long low = -1, high = mod, mid;
	
	
	while(low + 1 < high){
		mid = (low + high) >> 1;
		
		if(mid * mid >= mod) high = mid;
		else low = mid;
	}
	
	long long sqrtM = high;
	unordered_map<long long,long long> mp;
	
	for(long long i = 0, x = 1; i < sqrtM; i++){
        if(x == b) {
            if(i >= ll) return i;
        }
		if(!mp.count(x)) mp[x] = i;
		x = x * a % mod;
	}
	
	long long x, y, temp;
	long long A;
	
	temp = power(a, sqrtM ,mod);
	
	if(gcd(temp, mod) != 1) return -1;
	
	exgcd(temp , mod, x, y);
	
	A = (x + mod) % mod;
	
    int minimum = INF;
    
	for(long long i = 0, bA = b; i < sqrtM; i++){
		if(mp.count(bA)){
			long long res = i * sqrtM + mp[bA];
			if(res >= ll) return res;
		}
		bA = bA * A % mod;
	}
	
    if(minimum != INF) return minimum;
     
	return -1;
}


signed main(){
	cout<<fixed<<setprecision(10);
	
	int T;
    
    cin>>T;
    
    for(int _ = 0; _ < T; _++){
        vector<int> A, B;
        int N;
        int k = -1;
        
        cin>>N;
        
        if(N == 1) cout<<1<<endl;
        else {
             k = modlog(2, 1, N*2-1);
             
             cout<<k<<endl;
        }
    }
	
	
	return 0;
}
0