intmain() { int t; cin >> t; while (t--) { int n; cin >> n; memset(num, 0, sizeof(num)); for (int i = 2; i <= n / i; i ++ ) { // 每一个合数都可以用比它小的质数表示 int s = 0; while(n % i == 0) // 所以第一次找到的一定是一个质数 { n /= i; s ++ ; } if (s) printf("%d %d\n", i, s); // i一定是质数,s为其出现的次数 } if (n > 1) printf("%d 1\n", n); // 优化利用性质:n中最多只有一个大于sqrt(n)的质因子 // 如果还有数那么一定只有这一个未被除过的,直接输出就好 printf("\n"); } return0; }
constint N = 1e6 + 5, mod = 1e9 + 7; //const double esp = 1e-6;
intmain() { int t; cin >> t; map<int, int> primes; while (t--) { int n; cin >> n; for(int i = 2; i <= n / i; i ++ ) { while (n % i == 0) { n /= i; primes[i] ++ ; } } if(n > 1) primes[n] ++ ; } LL res = 1; for (auto prime : primes) res = res * (prime.second + 1) % mod; printf("%lld\n", res); return0; }
constint N = 1e6 + 5, mod = 1e9 + 7; //const double esp = 1e-6; map<int, int> primes; intmain() { int t; cin >> t; while (t -- ) { int n; cin >> n; for (int i = 2; i <= n / i; i ++) { while (n % i == 0) { primes[i] ++ ; n /= i; } } if (n > 1) primes[n] ++ ; } LL res = 1; for (auto p : primes) // 按照求和的公式进行计算 { LL a = p.first, b = p.second; // a是底数,b是指数 LL t = 1; while (b -- ) t = (t * a + 1) % mod; // 利用秦九韶算法, 每次乘以加一---复杂度O(n) res = res * t % mod; } cout << res << endl; return0; }
最大公约数
欧几里得算法(辗转相除法)
时间复杂度:
核心原理:
1 2 3 4
intgcd(int a, int b) { return b ? gcd(b, a % b) : a; }
//const int N = 1e6 + 5, mod = 1e9 + 7; //const double esp = 1e-6;
intEular(int a){ int res = a; for (int i = 2; i <= a / i; i ++ ) { if(a % i == 0) { res = res / i * (i - 1); while (a % i == 0) a /= i; } } if (a > 1) res = res / a * (a - 1); return res; }
intmain() { int t; cin >> t; while (t -- ) { int a; cin >> a; printf("%d\n", Eular(a)); } return0; }
//const int N = 1e6 + 5, mod = 1e9 + 7; //const double esp = 1e-6;
intexgcd(int a, int b, int &x, int &y) { if(!b) { x = 1, y = 0; return a; } int d = exgcd(b, a % b, y, x); y -= a / b * x; return d; }
intmain() { int t; cin >> t; while (t--) { int a, b, m, x, y; cin >> a >> b >> m; int d = exgcd(a, m, x, y); if(b % d) printf("impossible\n"); else { x = (LL)x * b / d % m; cout << x << endl; } } return0; }
intmain() { LL n, mod; cin >> n >> mod; inv[1] = 1; for (int i = 2; i <= n; i ++ ) inv[i] = (mod - mod / i) * inv[mod % i] % mod; for (int i = 1; i <= n; i ++ ) printf("%ld\n", inv[i]); return0; }