結果

問題 No.1648 Sum of Powers
ユーザー 👑 kmjpkmjp
提出日時 2021-08-14 00:42:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,793 bytes
コンパイル時間 3,059 ms
コンパイル使用メモリ 211,556 KB
実行使用メモリ 16,236 KB
最終ジャッジ日時 2024-04-14 22:11:01
合計ジャッジ時間 7,639 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,672 KB
testcase_01 AC 4 ms
9,796 KB
testcase_02 AC 15 ms
11,336 KB
testcase_03 AC 13 ms
11,372 KB
testcase_04 AC 21 ms
11,208 KB
testcase_05 AC 15 ms
11,120 KB
testcase_06 AC 15 ms
11,460 KB
testcase_07 AC 15 ms
11,460 KB
testcase_08 AC 20 ms
11,244 KB
testcase_09 AC 16 ms
11,240 KB
testcase_10 AC 16 ms
11,328 KB
testcase_11 AC 14 ms
11,248 KB
testcase_12 AC 23 ms
11,244 KB
testcase_13 AC 14 ms
11,372 KB
testcase_14 AC 12 ms
11,372 KB
testcase_15 AC 16 ms
11,208 KB
testcase_16 AC 16 ms
11,244 KB
testcase_17 AC 21 ms
11,244 KB
testcase_18 AC 14 ms
11,464 KB
testcase_19 AC 14 ms
11,376 KB
testcase_20 AC 14 ms
11,248 KB
testcase_21 AC 15 ms
11,244 KB
testcase_22 AC 84 ms
16,192 KB
testcase_23 AC 85 ms
15,980 KB
testcase_24 AC 82 ms
16,068 KB
testcase_25 AC 80 ms
16,112 KB
testcase_26 AC 82 ms
15,940 KB
testcase_27 AC 81 ms
15,944 KB
testcase_28 AC 88 ms
16,196 KB
testcase_29 AC 85 ms
16,196 KB
testcase_30 AC 86 ms
16,068 KB
testcase_31 WA -
testcase_32 AC 88 ms
15,936 KB
testcase_33 AC 88 ms
15,940 KB
testcase_34 AC 84 ms
15,936 KB
testcase_35 AC 83 ms
15,980 KB
testcase_36 AC 86 ms
16,236 KB
testcase_37 WA -
testcase_38 AC 87 ms
16,196 KB
testcase_39 WA -
testcase_40 AC 86 ms
15,940 KB
testcase_41 WA -
testcase_42 AC 86 ms
16,068 KB
testcase_43 AC 91 ms
16,108 KB
testcase_44 AC 84 ms
16,196 KB
testcase_45 AC 87 ms
16,108 KB
testcase_46 AC 87 ms
16,068 KB
testcase_47 AC 13 ms
11,368 KB
testcase_48 AC 3 ms
9,832 KB
testcase_49 AC 18 ms
11,332 KB
testcase_50 AC 21 ms
11,248 KB
testcase_51 AC 5 ms
10,092 KB
testcase_52 AC 4 ms
9,796 KB
testcase_53 AC 4 ms
9,964 KB
testcase_54 AC 4 ms
9,792 KB
testcase_55 AC 89 ms
16,236 KB
testcase_56 AC 86 ms
16,108 KB
testcase_57 AC 4 ms
9,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

ll A,B,P,Q;
const ll mo=998244353;

const int MAT=2;
struct Mat { ll v[MAT][MAT]; Mat(){ZERO(v);};};

Mat M[101010];
Mat RM[101010];



ll modpow(ll a, ll n = mo-2) {
	ll r=1;
	while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
	return r;
}


ll modlog(int g,int a) {  // return g^x=a mod a
	map<int,int> M;
	ll cur=1;
	int sq=sqrt(mo);
	int i;
	FOR(i,sq) {
		M[cur]=i;
		cur=cur*g%mo;
	}
	
	ll step=cur;
	cur=1;
	ll num=0;
	int lp=0;
	while(1) {
		ll x=a*modpow(cur)%mo;
		if(lp++>sq+5) return 1LL<<50;
		if(M.count(x)) return num+M[x];
		cur=cur*step%mo;
		num+=sq;
	}
}

Mat mulmat(Mat& a,Mat& b,int n=MAT) {
	ll mo2=4*mo*mo;
	int x,y,z; Mat r;
	FOR(x,n) FOR(y,n) r.v[x][y]=0;
	FOR(x,n) FOR(z,n) FOR(y,n) {
		r.v[x][y] += a.v[x][z]*b.v[z][y];
		if(r.v[x][y]>mo2) r.v[x][y] -= mo2;
	}
	FOR(x,n) FOR(y,n) r.v[x][y]%=mo;
	return r;
}

Mat powmat(ll p,Mat a,int n=MAT) {
	int i,x,y; Mat r;
	FOR(x,n) FOR(y,n) r.v[x][y]=0;
	FOR(i,n) r.v[i][i]=1;
	while(p) {
		if(p%2) r=mulmat(r,a,n);
		a=mulmat(a,a,n);
		p>>=1;
	}
	return r;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>A>>B>>P>>Q;
	
	if(B==0) {
		x = modlog(A,P);
		if(x<2) x+=mo-1;
		cout<<x<<endl;
	}
	else {
		map<pair<ll,ll>,int> mp;
		
		M[0].v[0][0]=M[0].v[1][1]=1;
		M[1].v[0][0]=A;
		M[1].v[1][0]=1;
		M[1].v[0][1]=mo-B;
		
		for(i=0;i<=100000;i++) {
			if(i>1) {
				M[i]=mulmat(M[i-1],M[1]);
			}
			
			ll p=(2*M[i].v[0][0]+A*modpow(B)%mo*M[i].v[0][1])%mo;
			ll q=(2*M[i].v[1][0]+A*modpow(B)%mo*M[i].v[1][1])%mo;
			
			if(i>=2&&p==P&&q==Q) {
				cout<<i<<endl;
				return;
			}
			mp[{p,q}]=i;
			
		}
		
		
		RM[0]=M[0];
		ll rb=modpow(B);
		RM[1].v[1][1]=A*rb%mo;
		RM[1].v[0][1]=1;
		RM[1].v[1][0]=(mo-1)*rb%mo;
		RM[1]=powmat(100000,RM[1]);
		for(i=1;i<=100000;i++) {
			if(i>1) {
				RM[i]=mulmat(RM[i-1],RM[1]);
			}
			ll p=(P*RM[i].v[0][0]+Q*RM[i].v[0][1])%mo;
			ll q=(P*RM[i].v[1][0]+Q*RM[i].v[1][1])%mo;
			
			if(mp.count({p,q})) {
				ll ret=i*100000+mp[{p,q}];
				cout<<ret<<endl;
				return;
			}
		}
	}
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0