求助,90pts
查看原帖
求助,90pts
461932
OIer_ljc楼主2022/1/26 20:01

===求助===
快速乘优化了一下

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>
#define ll long long
using namespace std;
map<ll ,ll> v,h;
ll k,m;
ll mul(ll a,ll b){
	ll l=a*(b>>25ll)%m*(1ll<<25)%m;
	ll r=a*(b&((1ll<<25)-1))%m;
	return (l+r)%m;
}
ll ksm(ll a,ll b){
	ll ans=1;
	while(b){
		if(b&1){
			ans=mul(ans,a)%m;
		}
		a=mul(a,a)%m;
		b>>=1;
	}
	return ans;
}
ll B(ll a,ll b){
	ll t=ceil(sqrt(m));
	b%=m;
	for(ll B=0;B<=t;B++){
		ll w=mul(b,ksm(a,B))%m;
		h[w]=B;
		v[w]=1;
	}
	a=ksm(a,t);
	for(ll A=0;A<=t;A++){
		ll w=ksm(a,A);
		if(v[w]){
			ll B=h[w];
			ll T=mul(A,t)-B;
			if(B>=0&&T>=0){
				return T;
			}
		}
	}
	return -1;
}
int main(){
	scanf("%lld%lld",&k,&m);
	printf("%lld",B(10,9*k+1));
	return 0;
}

谢谢

2022/1/26 20:01
加载中...