1. 程式人生 > >[逆元] 1-n中改小一個 使 % p == q gym101620

[逆元] 1-n中改小一個 使 % p == q gym101620

 

 

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mn = 20000000 + 10;

ll n, p, q;

ll qpow(ll x, ll y)
{
	ll ans = 1;
	while (y)
	{
		if (y & 1)
			ans = ans * x % p;
		x = x * x % p;
		y >>= 1;
	}
	return ans;
}

ll jie[mn];
int main()
{
	cin >> n >> p >> q;
	
	jie[0] = 1;
	for (ll i = 1; i <= 2 * p; i++)
		jie[i] = jie[i - 1] * i % p;
	
	if (q == 0)
	{
		if (n < p || (n == 2 && p == 2))
			cout << "-1 -1" << endl;
		else if (n == p)
			cout << p - 1 << " 1" << endl;
		else if (n > p)
			cout << p + 1 << " 1" << endl;
	}
	else if (q != 0)
	{
		if (n < p)
		{
			ll temp = qpow(jie[n], p - 2);
			for (ll a = 2; a <= n; a++)
			{
				ll b = a * q % p * temp % p;
				if (a > b)
				{
					cout << a << ' ' << b << endl;
					return 0;
				}
			}
			cout << "-1 -1" << endl;
		}
		else if (n >= p && n < 2 * p)
		{
			ll temp = 1;
			for (int i = 1; i <= n; i++)
			{
				if (i == p)
					continue;
				temp = temp * i % p;
			}
			
			for (ll i = 1; i < p; i++)
			{
				if (temp * i % p == q)
				{
					cout << p << ' ' << i << endl;
					return 0;
				}
			}
			cout << "-1 -1" << endl;
		}
		else if (n >= 2 * p)
			cout << "-1 -1" << endl;
	}
	return 0;
}