結果

問題 No.1648 Sum of Powers
ユーザー chocoruskchocorusk
提出日時 2021-08-13 21:40:00
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 5,421 bytes
コンパイル時間 4,346 ms
コンパイル使用メモリ 210,988 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 17:14:26
合計ジャッジ時間 6,398 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 3 ms
6,944 KB
testcase_12 AC 3 ms
6,940 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 3 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 3 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 3 ms
6,944 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 3 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 3 ms
6,940 KB
testcase_29 AC 3 ms
6,940 KB
testcase_30 AC 3 ms
6,940 KB
testcase_31 AC 3 ms
6,940 KB
testcase_32 AC 4 ms
6,940 KB
testcase_33 AC 3 ms
6,944 KB
testcase_34 AC 3 ms
6,940 KB
testcase_35 AC 2 ms
6,944 KB
testcase_36 AC 3 ms
6,944 KB
testcase_37 AC 3 ms
6,944 KB
testcase_38 AC 3 ms
6,944 KB
testcase_39 AC 3 ms
6,944 KB
testcase_40 AC 3 ms
6,944 KB
testcase_41 AC 3 ms
6,940 KB
testcase_42 AC 3 ms
6,944 KB
testcase_43 AC 3 ms
6,940 KB
testcase_44 AC 3 ms
6,940 KB
testcase_45 AC 3 ms
6,940 KB
testcase_46 AC 3 ms
6,944 KB
testcase_47 AC 3 ms
6,940 KB
testcase_48 AC 2 ms
6,944 KB
testcase_49 AC 3 ms
6,944 KB
testcase_50 AC 3 ms
6,940 KB
testcase_51 AC 2 ms
6,944 KB
testcase_52 AC 2 ms
6,940 KB
testcase_53 AC 2 ms
6,944 KB
testcase_54 AC 2 ms
6,940 KB
testcase_55 AC 3 ms
6,940 KB
testcase_56 AC 2 ms
6,944 KB
testcase_57 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long int ll;
typedef pair<ll, int> P;
ll p;
using arr=array<ll, 2>;
using Mat=array<arr, 2>;
Mat matmul(Mat a, Mat b){
	Mat c={};
	for(int i=0; i<2; i++){
		for(int j=0; j<2; j++){
			for(int k=0; k<2; k++){
				(c[i][j]+=a[i][k]*b[k][j])%=p;
			}
		}
	}
	return c;
}
Mat matpow(Mat a, ll k){
	Mat ap=a, ans={};
	ans[0][0]=ans[1][1]=1;
	while(k){
		if(k&1) ans=matmul(ap, ans);
		ap=matmul(ap, ap);
		k>>=1;
	}
	return ans;
}
Mat e;
vector<P> fac(bool myon){
	map<ll, int> mp;
	vector<P> ret;
	ll x=p-1;
	for(ll i=2; i*i<=x; i++){
		if(x%i==0){
			int f=0;
			while(x%i==0){
				x/=i;
				f++;
			}
			mp[i]+=f;
		}
	}
	if(x>1) mp[x]++;
	if(!myon){
		for(auto q:mp) ret.push_back(q);
		return ret;
	}
	x=p+1;
	for(ll i=2; i*i<=x; i++){
		if(x%i==0){
			int f=0;
			while(x%i==0){
				x/=i;
				f++;
			}
			mp[i]+=f;
		}
	}
	if(x>1) mp[x]++;
	for(auto q:mp) ret.push_back(q);
	return ret;
}
vector<ll> order(Mat a){
	ll x=(p-1)*(p+1);
	bool myon=0;
	if(matpow(a, x)!=e){
		x=p*(p-1);
	}else{
		myon=1;
	}
	vector<P> v=fac(myon);
	vector<ll> ret;
	for(auto q:v){
		ll r=q.first; int f=q.second;
		ll y=1;
		for(int i=0; i<f; i++) y*=r;
		for(int i=0; i<f; i++){
			x/=r, y/=r;
			if(matpow(a, x)!=e){
				x*=r;
				y*=r;
				break;
			}
		}
		if(y>1) ret.push_back(y);
	}
	if(!myon) ret.push_back(p);
	return ret;
}
ll discretelog(Mat a, Mat b, ll d){
	ll sq=1;
	while(sq*sq<d) sq++;
	vector<pair<Mat, int>> vp(sq);
	vp[0]=make_pair(e, 0);
	for(int i=1; i<sq; i++){
		vp[i]=make_pair(matmul(vp[i-1].first, a), i);
	}
	auto cp=[](pair<Mat, int> x, pair<Mat, int> y){
		for(int i=0; i<2; i++){
			for(int j=0; j<2; j++){
				if(x.first[i][j]!=y.first[i][j]) return x.first[i][j]<y.first[i][j];
			}
		}
		return false;
	};
	sort(vp.begin(), vp.end(), cp);
	Mat ap=matpow(matpow(a, d-1), sq);
	for(int i=0; i<sq; i++){
		int t=lower_bound(vp.begin(), vp.end(), make_pair(b, 0), cp)-vp.begin();
		if(t<sq && vp[t].first==b){
			if(i*sq+vp[t].second<d) return i*sq+vp[t].second;
		}
		b=matmul(b, ap);
	}
	return -1;
}
ll inv(ll a, ll m){
    ll b=m, x=1, y=0;
    while(b>0){
        ll t=a/b;
        swap(a-=t*b, b);
        swap(x-=t*y, y);
    }
    return (x%m+m)%m;
}
ll powmod(ll a, ll k, ll mod){
    ll ap=a, ans=1;
    while(k){
        if(k&1){
            ans*=ap;
            ans%=mod;
        }
        ap=ap*ap;
        ap%=mod;
        k>>=1;
    }
    return ans;
}
ll discretelog0(ll x, ll y, ll m){
    ll p=1%m;
    for(int i=0; i<=30; i++){
        if(p==y) return i;
        (p*=x)%=m;
    }
    ll g=gcd(x, m);
    ll m1=m;
    for(ll i=2; i*i<=g; i++){
        if(g%i==0){
            while(g%i==0) g/=i;
            while(m1%i==0) m1/=i;
        }
    }
    if(g>1){
        while(m1%g==0) m1/=g;
    }
    if(y%(m/m1)!=0) return -1;
    x%=m1, y%=m1;
    ll d=1;
    while(d*d<m1) d++;
    ll invx=inv(x, m1);
    unordered_map<ll, ll> mp; //yx^(-b)
    ll y1=y;
    for(ll i=0; i<d; i++){
        mp[y1]=i;
        (y1*=invx)%=m1;
        if(y1==y) break;
    }
    ll xd=powmod(x, d, m1);
    ll q=1%m1;
    for(ll i=0; i<d; i++){
        auto itr=mp.find(q);
        if(itr!=mp.end()){
            return i*d+itr->second;
        }
        (q*=xd)%=m1;
    }
    return -1;
}
int main()
{
	p=998244353;
    using mint=modint998244353;
    ll A, B, P, Q; cin>>A>>B>>P>>Q;
    if(A*A-4*B==0){
        if(A==0){
            cout<<2<<endl;
            return 0;
        }
        ll n=discretelog0((mint(A)/mint(2)).val(), (mint(P)/mint(2)).val(), p);
        if(n<=1) n+=p-1;
        cout<<n<<endl;
        return 0;
    }
	Mat a, b;
	//cin>>a[0][0]>>a[0][1]>>a[1][0]>>a[1][1]>>b[0][0]>>b[0][1]>>b[1][0]>>b[1][1];
    a[0][0]=A, a[0][1]=(p-B)%p, a[1][0]=1, a[1][1]=0;
    
    Mat b1;
    b1[0][0]=mint(A*P-B*Q).val(), b1[0][1]=P, b1[1][0]=P, b1[1][1]=Q;
    Mat b2;
    b2[0][0]=2, b2[0][1]=mint(-A).val(), b2[1][0]=b2[0][1], b2[1][1]=mint(A*A-2*B).val();
    mint D=mint(A*A-4*B);
    for(int i=0; i<2; i++) for(int j=0; j<2; j++){
        b2[i][j]=(mint(b2[i][j])/D).val();
    }
    b=matmul(b1, b2);
	if(a==b){
		cout<<2<<endl;
		return 0;
	}
	Mat a2=matmul(a, a);
	if(a2==b){
		cout<<3<<endl;
		return 0;
	}
	if(a2[0][0]==0 && a2[0][1]==0 && a2[1][0]==0 && a2[1][1]==0){
		cout<<-1<<endl;
		return 0;
	}
	if(a[0][0]*a[1][1]%p==a[0][1]*a[1][0]%p){
		e=matpow(a, p-1);
	}else{
		e[0][0]=e[1][1]=1;
	}
	vector<ll> vd=order(a);
	if(vd.empty()){
		cout<<-1<<endl;
		return 0;
	}
	ll d=1;
	for(auto x:vd) d*=x;
	//cout<<d<<endl;
	int k=vd.size();
	vector<ll> vf(k);
	for(int i=0; i<k; i++){
		ll x=vd[i];
		vf[i]=discretelog(matpow(a, d/x), matpow(b, d/x), x);
		if(vf[i]==-1){
			cout<<-1<<endl;
			return 0;
		}
	}
	ll s=vf[0], q=vd[0];
	for(int i=1; i<k; i++){
		ll c=(vf[i]-s%vd[i]+vd[i])%vd[i]*inv(q%vd[i], vd[i])%vd[i];
		s+=c*q;
		q*=vd[i];
	}
	ll ans=(s==0 ? q : s);
	if(matpow(a, ans)!=b) cout<<-1<<endl;
	else cout<<ans+1<<endl;
	return 0;
}
0