求助:BSGS WA#12~20
查看原帖
求助:BSGS WA#12~20
68882
灵华楼主2022/5/12 09:37

应该是第三部分的bsgs错了。在线求助QWQ

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
using namespace std ;

#define int long long

int read ( ) {
	char ch = getchar ( ) ;
	int x = 0 ;
	while ( ch < '0' || ch > '9' )
		ch = getchar ( ) ;
	while ( ch >= '0' && ch <= '9' )
		x = x * 10 + ch - 48 , ch = getchar ( ) ;
	return x ;
}

inline int qpow ( int x , int y , int P ) {
	int res = 1 ;
	x %= P ;
	while ( y ) {
		if ( y & 1 )
			res = res * x % P ;
		x = x * x % P ;
		y >>= 1 ;
	}
	return res ;
}

int exgcd ( int a , int b , int &x , int &y ) {
//	cout << " gcd : " << a << " , " << b << "\n" ;
	if ( ! b ) {
		x = 1 , y = 0 ;
//		cout << " ret : " << a << "\n" ;
		return a ;
	}
	int d = exgcd ( b , a % b , x , y ) ;
	int t = x ;
	x = y ;
	y = t - y * ( a / b ) ;
//	cout << " gcd ( " << a << " , " << b << " ) = " << d << " : " << x << " , " << y << "\n" ;
	return d ;
}

map < int , int > mp ;

signed main ( ) {
	int T = read ( ) , op = read ( ) ;
	while ( T -- ) {
		int x = read ( ) , y = read ( ) , p = read ( ) ;
		if ( op == 1 ) {
			cout << qpow ( x , y , p ) << "\n" ;
		}
		else if ( op == 2 ) {
			int a , b ;
			int d = exgcd ( x , p , a , b ) ;
			if ( y % d == 0 )
				cout << ( a % p * ( y / d ) % p + p ) % p << "\n" ;
			else puts ( "Orz, I cannot find x!" ) ;
		}
		else {
			y %= p ;
			x %= p ;
			if ( ! y && ! x ) {
				puts ( "1" ) ;
				continue ;
			}
			if ( ! y || ! x ) {
				puts ( "Orz, I cannot find x!" ) ;
				continue ;
			}
			int sp = sqrt ( p ) + 1 , tp = 0 , tmp = 0 ;
			mp .clear ( ) ;
			for ( int i = sp ; i >= 0 ; -- i )
				mp [ y * qpow ( x , i , p ) % p ] = i + 1 ;
			for ( int i = 0 ; i <= sp ; ++ i )
				if ( tmp = mp [ qpow ( x , i * sp , p ) ] ) {
					cout << i * sp + tmp - 1 << "\n" ;
					tp = 1 ;
					break ;
				}
			if ( ! tp )
				puts ( "Orz, I cannot find x!" ) ;
		}
	}
	return 0 ;
}

2022/5/12 09:37
加载中...