voidsolve(int n){ vector<int> vec; for (int i = 1; i <= n / i; i++) { if (n % i == 0) { vec.push_back(i); if (i != n / i) vec.push_back(n / i); } } sort(vec.begin(),vec.end()); for(auto i : vec) { cout << i << " "; } cout << endl; }
intmain(){ int n; cin >> n; while(n --) { int x; cin >> x; solve(x); } }
intmain(){ int n; cin >> n; unordered_map<int, int> cnt; while (n--) { int x; cin >> x; for (int i = 2; i <= x / i; i++) { while (x % i == 0) { x /= i; cnt[i]++; } } if (x > 1) //n也是一个质因子 cnt[x]++; } longlong ans = 1; for (auto c : cnt) { //res = (x1+1)(x2+1)(x3+1)…(xk+1) ans = ans * (c.second + 1) % mod; } cout << ans << endl; }
usingnamespace std; #define LL long long longlong mod = 1e9 + 7;
intmain(){ int n; cin >> n; unordered_map<int, int> cnt; while (n--) { int x; cin >> x; for (int i = 2; i <= x / i; i++) { while (x % i == 0) { x /= i; cnt[i]++; } } if (x > 1) //n也是一个质因子 cnt[x]++; } longlong res = 1;
for (auto p : cnt) { LL a = p.first, b = p.second; LL t = 1; while (b -- ) t = (t * a + 1) % mod; //t = 1 t = p + 1 t = p^2 + p + 1 res = res * t % mod; }
voidsolve() { int n; cin >> n; int res = n; for(int i = 2; i <= n / i; i++) { if(n % i == 0) { // res*=(p - 1) / p res = res / i * (i - 1); while(n % i == 0) //对n约分 { n /= i; } } } // 如果有剩余,则剩余是个质因子 if(n > 1) { res = res / n * (n - 1); } cout <<res << endl; } intmain() { int n; cin >> n; while(n--) { solve(); } }
intqmi(int a, int k, int p) { LL res = 1; while(k) { if(k & 1) res = (LL)res * a % p; //注意转型,因为我们传入的类型为int k = k >> 1; //更新a,a依次为a^{2^0},a^{2^1},a^{2^2},....,a^{2^logb} a = (LL)a * a % p; } return res; }
intmain() { int n; cin >> n; while(n--) { int a, k , p; cin >> a >> k >> p; cout << qmi(a, k, p) << endl; } }
876. 快速幂求逆元
费马定理+快速幂
[AcWing 876. 快速幂求逆元 - AcWing](https://www.acwing.com/solution/content/3054/)
a / b ≡ a * x (mod n)
两边同乘b可得 a ≡ a * b * x (mod n)
即 1 ≡ b * x (mod n)
同 b * x ≡ 1 (mod n)
由费马小定理可知,当n为质数时
b ^ (n - 1) ≡ 1 (mod n)
拆一个b出来可得 b * b ^ (n - 2) ≡ 1 (mod n)
故当n为质数时,b的乘法逆元 x = b ^ (n - 2)
//a^k%p intqmi(int a, int k, int p) { LL res = 1; while(k) { if(k & 1) res = (LL)res * a % p; a = (LL)a * a % p; k >>= 1; } return res; }
intmain() { int n; cin >> n; while(n--) { int a, p; cin >> a >> p; int res = qmi(a, p - 2, p); if(a % p) cout << res << endl; //互质存在逆元 else cout <<"impossible"<< endl; } }