結果

問題 No.3522 冪乗乗
コンテスト
ユーザー 👑 binap
提出日時 2025-02-09 17:12:39
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
RE  
実行時間 -
コード長 4,154 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 7,924 ms
コンパイル使用メモリ 531,796 KB
実行使用メモリ 30,320 KB
平均クエリ数 30.75
最終ジャッジ日時 2026-05-01 21:00:12
合計ジャッジ時間 17,593 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 53 RE * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef long double ld;
typedef pair<int, int> P;

template <int m> ostream& operator<<(ostream& os, const static_modint<m>& a) {os << a.val(); return os;}
template <int m> ostream& operator<<(ostream& os, const dynamic_modint<m>& a) {os << a.val(); return os;}
template <int m> istream& operator>>(istream& is, static_modint<m>& a) {long long x; is >> x; a = x; return is;}
template <int m> istream& operator>>(istream& is, dynamic_modint<m>& a) {long long x; is >> x; a = x; return is;}
template<typename T> istream& operator>>(istream& is, vector<T>& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;}
template<typename U, typename T> ostream& operator<<(ostream& os, const pair<U, T>& p){os << p.first << ' ' << p.second; return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;}
template<typename T> ostream& operator<<(ostream& os, const set<T>& se){for(T x : se) os << x << " "; os << "\n"; return os;}
template<typename T> ostream& operator<<(ostream& os, const unordered_set<T>& se){for(T x : se) os << x << " "; os << "\n"; return os;}
template<typename S, auto op, auto e> ostream& operator<<(ostream& os, const atcoder::segtree<S, op, e>& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;}
template<typename S, auto op, auto e, typename F, auto mapping, auto composition, auto id> ostream& operator<<(ostream& os, const atcoder::lazy_segtree<S, op, e, F, mapping, composition, id>& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;}

template<typename T> void chmin(T& a, T b){a = min(a, b);}
template<typename T> void chmax(T& a, T b){a = max(a, b);}

// Start 14:59
// Fermat's little theorem and  Euler's theorem
//(I) N = 0 mod B
// M^L = 0 (M = 0 and L > 0)
// M^L != 0 (M != 0 or(M = 0 and L = 0))
// N^(M^L) = 1 (M = 0 and L > 0)
// N^(M^L) = 0 (M != 0 or(M = 0 and L = 0))

//(II) B = 1
// ans = 1

//(III) otherwise
// a^(phi(B)) = 1 mod(B)
// a^(phi(phi(B))) = 1 mod(phi(B))

// Start Implementation 15:15

// 15:41 Submission -> WA
// int -> long long
// 15:41 Submission -> WA
// Euler's theorem is ng! a and B are now always coprime!!

// regularity?
// a_i = n^i mod B
// at most B-length repetition
// the length L is always divides B-1 ? <- False

// B = _Prod p_i e_i
// CRT? <- ( ; ; )

// the length L is always divides phi(B) ? <- ?

// 16:22 break

// 16:50 Restart
// multiprecision
// simply calculate N^(M^L) O(log(M^L)) = O(L log M)

// 16:59 Submission -> WA
// corner??

#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
using Bint = mp::cpp_int;

template<typename T>
T pow_mod(T A, T N, T MOD){
	T res = 1 % MOD;
	A %= MOD;
	while(N){
		if(N & 1) res = (res * A) % MOD;
		A = (A * A) % MOD;
		N >>= 1;
	}
	return res;
}

template<typename T>
T pow_simple(T A, T N){
	T res = 1;
	while(N){
		if(N & 1) res = (res * A);
		A = (A * A);
		N >>= 1;
	}
	return res;
}

int main(){
	int n, m, l;
	cin >> n >> m >> l;
	
	
	int b = -1;
	{
		// ok >= b, ng < b
		int ok = 1000000001, ng = 0;
		while(ok - ng > 1){
			int mid = (ok + ng) / 2;
			cout << "? " << mid << ' ' << 1 << "\n";
			flush(cout);
			int res;
			cin >> res;
			if(res < mid) ok = mid;
			else ng = mid;
		}
		b = ok;
	}
	
	assert(n % b != 0);
	
	if(b == 1){
		cout << "! 0\n";
		return 0;
	}
	
	n %= b;
	if(n == 0){
		if(m == 0 and l > 0) cout << "! 1\n";
		else cout << "! 0 \n";
		return 0;
	}
	
	Bint e = pow_simple<Bint>(m, l);
	Bint ans = pow_mod<Bint>(n, e, b);
	cout << "! " << ans << "\n";
	
	return 0;
}
0