結果

問題 No.1140 EXPotentiaLLL!
ユーザー takumattakumat
提出日時 2020-07-31 22:18:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,772 bytes
コンパイル時間 718 ms
コンパイル使用メモリ 88,904 KB
実行使用メモリ 47,640 KB
最終ジャッジ日時 2023-09-20 23:52:32
合計ジャッジ時間 7,879 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <bitset>

typedef unsigned long long ULLONG;
typedef long long LLONG;
static const LLONG MOD_NUM = 1000000007; //998244353;

template<class _T> static void get(_T& a) {
	std::cin >> a;
}
template<class _T> static void get(_T& a, _T& b) {
	std::cin >> a >> b;
}
template<class _T> static void get(_T& a, _T& b, _T& c) {
	std::cin >> a >> b >> c;
}
template <class _T> static _T tp_abs(_T a) {
	if (a < (_T)0) {
		a *= (_T)-1;
	}
	return a;
}

static void exec();

int main()
{
	exec();
	fflush(stdout);
	return 0;
}

static void eratosthenes(int targ, std::set<int>& primes)
{
	std::vector<int> is_prime(targ + 1, 1);

	for (int i = 2; i * i <= targ; i++) {
		if (is_prime[i]) {
			for (int j = 0; (i * (j + 2)) <= targ; j++) {
				is_prime[i * (j + 2)] = 0;
			}
		}
	}

	for (int i = 2; i <= targ; i++) {
		if (is_prime[i]) primes.insert(i);
	}
}
template <class _T> static _T tp_modpow(_T base, _T exp, _T mod = (_T)1) {
	_T ans = 1;
	while (exp > 0) {
		if (exp & 1) {
			ans = (ans * base) % mod;
		}
		base = (base * base) % mod;
		exp >>= 1;
	}
	return ans;
}
template <class _T> static _T tp_modinv(_T a, _T mod) {
	return tp_modpow<_T>(a, mod - 2, mod);
}

static void exec()
{
	int T;
	get(T);

	std::set<int> primes;
	eratosthenes(5 * 1000000 + 1, primes);

	LLONG A;
	int P;

	while (T--) {
		scanf("%lld %d", &A, &P);

		LLONG ans = -1;
		if (primes.find(P) != primes.end()) {
			ans = 1;

			for (int p = P; p > 0; p--) {
				ans *= tp_modpow<LLONG>(A, p, P);
				ans %= P;
			}
		}
		printf("%lld\n", ans);
	}
}
0