第一次使用快速乘,原来是这样的。
注意Put快速输出要改一下类型,(ll),注意快速输出后面要手打换行。
注意熟悉快速幂配合快速乘的模板。
#pragma warning(disable:4996) #include<iostream> #include<algorithm> #include<bitset> #include<tuple> #include<unordered_map> #include<fstream> #include<iomanip> #include<string> #include<cmath> #include<cstring> #include<vector> #include<map> #include<set> #include<list> #include<queue> #include<stack> #include<sstream> #include<cstdio> #include<ctime> #include<cstdlib> #define pb push_back #define INF 0x3f3f3f3f #define inf 0x7FFFFFFF #define moD 1000000003 #define pii pair<ll,ll> #define eps 1e-8 #define equals(a,b) (fabs(a-b)<eps) #define bug puts("bug") #define re register #define fi first #define se second typedef long long ll; typedef unsigned long long ull; const ll MOD = 1e6 + 7; const int maxn = 2e4 +5; const double Inf = 10000.0; const double PI = acos(-1.0); using namespace std; ll mul(ll a, ll b, ll m) { ll res = 0; while (b) { if (b & 1) res = (res + a) % m; a = (a + a) % m; b >>= 1; } return res % m; } ll quickPower(ll a, ll b, ll m) { ll base = a; ll ans = 1ll; while (b) { if (b & 1) ans = mul(ans, base , m); base = mul(base, base , m); b >>= 1; } return ans; } int readint() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } ll readll() { ll x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } void Put(ll x) { if (x > 9) Put(x / 10); putchar(x % 10 + '0'); } bool is_prime(ll tmp) { if (tmp == 1ll) return false; for (ll i = 2; i * i <= tmp; i++) { if (tmp % i == 0) return false; } return true; } int main() { int T; ll p; T = readint(); while (T--) { p = readll(); ll tmp = p - 1; ll res = p - 1; while (!is_prime(tmp)) { res =mul(res, quickPower(tmp, p - 2, p),p); res %= p; tmp--; } Put(res); puts(""); } }