結果

問題 No.251 大きな桁の復習問題(1)
ユーザー koyumeishikoyumeishi
提出日時 2015-07-25 00:06:27
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 13 ms / 5,000 ms
コード長 1,496 bytes
コンパイル時間 795 ms
コンパイル使用メモリ 81,840 KB
実行使用メモリ 4,436 KB
最終ジャッジ日時 2023-08-22 16:55:12
合計ジャッジ時間 2,062 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 7 ms
4,376 KB
testcase_03 AC 7 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 12 ms
4,376 KB
testcase_10 AC 11 ms
4,436 KB
testcase_11 AC 12 ms
4,380 KB
testcase_12 AC 11 ms
4,376 KB
testcase_13 AC 12 ms
4,380 KB
testcase_14 AC 12 ms
4,380 KB
testcase_15 AC 13 ms
4,376 KB
testcase_16 AC 11 ms
4,376 KB
testcase_17 AC 12 ms
4,376 KB
testcase_18 AC 10 ms
4,376 KB
testcase_19 AC 12 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 5 ms
4,380 KB
testcase_22 AC 6 ms
4,380 KB
testcase_23 AC 4 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

#define MOD 129402307

long long rem(string X, long long Y){
	long long b = 0;

	for(int i=0; i<X.size(); i++){
		b *= 10;
		b += X[i] - '0';
		b %= Y;
	}
	return b;
}

long long power_mod(long long x, long long y, long long mod){
	long long ret = 1;
	while(y){
		if(y&1) ret = (ret*x) % mod;
		x = (x*x) % mod;
		y >>= 1;
	}
	return ret;
}

//find k such that x^k = y (mod z)
//x and z are co-prime
//x^(aH-b) = y (mod z)
//x^aH = y * x^b (mod z)
long long baby_step_giant_step(long long x, long long y, long long z){
	long long H = sqrt(z) + 1;
	set<pair<long long,long long>> S;

	for(long long i=0, w=y; i<H; i++, w=(w*x)%z){
		S.insert({w,i});
	}

	long long k = -1;
	long long x_H = power_mod(x, H, z);
	for(long long i=1, w=x_H; i<=H; i++, w=(w*x_H)%z){
		auto itr = S.lower_bound({w+1, 0});
		if(itr == S.begin()) continue;

		itr--;
		if(itr->first == w){
			return H * i - itr->second;

		}
	}

	return k;
}

int main(){
	string n,m;
	cin >> n >> m;

	if(n == "0"){
		cout << 0 << endl;
		return 0;
	}
	if(m == "0"){
		cout << 1 << endl;
		return 0;
	}

	long long x = rem(n, MOD);

	if(x == 0){
		cout << 0 << endl;
		return 0;
	}

	long long ord = baby_step_giant_step(x, 1, MOD);
	long long y = rem(m, ord);

	long long ans = power_mod(x, y, MOD);
	cout << ans << endl;

	return 0;
}
0