1 试除法求约数
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
vector<int> get_divisors(int n)
{
vector<int> res;
for (int i = 1; i <= n / i; i++)
{
if (n % i == 0)
{
res.push_back(i);
if (i != n / i)res.push_back(n / i);
}
}
sort(res.begin(), res.end());
return res;
}
int main()
{
int n;
cin >> n;
while (n--)
{
int x;
cin >> x;
auto res = get_divisors(x);
for (auto t : res)cout << t << ' ';
cout << endl;
}
return 0;
}
2 约数个数
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<queue>
#include<unordered_map>
using namespace std;
typedef long long LL;
const int mod = 1e9 + 7;
int main()
{
int n;
cin >> n;
unordered_map<int, int>primes;
while (n--)
{
int x;
cin >> x;
for (int i = 2; i <= x / i; i++)
while (x % i == 0)
{
x /= i;
primes[i]++;
}
if (x > 1)primes[x]++;//大于更号x的质因子(n中最多包含一个大于sqrt(n)的质因子)
}
LL res = 1;
for (auto prime : primes)res = res * (prime.second + 1) % mod;
cout << res << endl;
return 0;
}
3 约数之和
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<queue>
#include<unordered_map>
using namespace std;
typedef long long LL;
const int mod = 1e9 + 7;
int main()
{
int n;
cin >> n;
unordered_map<int, int>primes;
while (n--)
{
int x;
cin >> x;
for (int i = 2; i <= x / i; i++)
while (x % i == 0)
{
x /= i;
primes[i]++;
}
if (x > 1)primes[x]++;//大于更号x的质因子(n中最多包含一个大于sqrt(n)的质因子)
}
LL res = 1;
for (auto prime : primes)
{
int p = prime.first, a = prime.second;
LL t = 1;
while (a--)t = (t * p + 1) % mod;
res = res * t % mod;
}
cout << res << endl;
return 0;
}
4 最大公约数
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int main()
{
int n;
scanf("%d", &n);
while (n--)
{
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", gcd(a, b));
}
return 0;
}
5 欧拉函数
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
int n;
cin >>n;
while (n--)
{
int a;
cin >> 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);
cout << res << endl;
}
return 0;
}
6 筛法求欧拉函数
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
using namespace std;
const int N = 1000010;
int primes[N], cnt;//primes表示质因子
int phi[N];//互质的数的个数
bool st[N];
typedef long long LL;
LL get_eulers(int n)
{
phi[1] = 1;
for (int i = 2; i <= n; i++)
{
if (!st[i])
{
primes[cnt++] = i;
phi[i] = i - 1;
}
for (int j = 0; primes[j] <= n / i; j++)
{
st[primes[j] * i] = true;
if (i % primes[j] == 0)
{
phi[primes[j] * i] = phi[i] * primes[j];
break;
}
phi[primes[j] * i] = phi[i] * (primes[j] - 1);
}
}
LL res = 0;
for (int i = 1; i <= n; i++)res += phi[i];
return res;
}
int main()
{
int n;
cin >> n;
cout << get_eulers(n) << endl;
return 0;
}
p为质数
7 快速幂
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
using namespace std;
typedef long long LL;
//a^k%p
int qmi(int a, int k, int p)
{
int res = 1;
while (k)
{
if (k & 1)res = (LL)res * a % p;
k >>= 1;
a = (LL)a * a % p;
}
return res;
}
int main()
{
int n;
scanf("%d", &n);
while (n--)
{
int a, k, p;
scanf("%d%d%d", &a, &k, &p);
printf("%d\n", qmi(a, k, p));
}
return 0;
}
8 快速幂求逆元
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
using namespace std;
typedef long long LL;
//a^k%p p是质数
int qmi(int a, int k, int p)
{
int res = 1;
while (k)
{
if (k & 1)res = (LL)res * a % p;
k >>= 1;
a = (LL)a * a % p;
}
return res;
}
int main()
{
int n;
scanf("%d", &n);
while (n--)
{
int a, p;
scanf("%d%d", &a, &p);
int res = qmi(a, p - 2, p);
if (a % p)printf("%d\n", res);
else puts("impossible");
}
return 0;
}
9 扩展欧几里得算法
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
using namespace std;
int exgcd(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;
}
int main()
{
int n;
scanf("%d", &n);
while (n--)
{
int a, b, x, y;
scanf("%d%d", &a, &b);
exgcd(a, b, x, y);
printf("%d %d\n", x, y);
}
return 0;
}
10 线性同余方程
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
using namespace std;
typedef long long LL;
int exgcd(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;
}
int main()
{
int n;
scanf("%d", &n);
while (n--)
{
int a, b, x, y,m;
scanf("%d%d%d", &a, &b,&m);
int d=exgcd(a, m, x, y);
if (b % d)puts("impossible");
else printf("%d\n",(LL)x*(b/d)%m);
}
return 0;
}
11 高斯消元解线性方程组
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
using namespace std;
const int N = 110;
const double eps = 1e-6;
int n;
double a[N][N];
int gauss()
{
int c, r;//c列 r行
for (c = 0, r = 0; c < n; c++)
{
int t = r;
for (int i = r; i < n; i++)
if (fabs(a[i][c]) > fabs(a[t][c]))
t = i;
if (fabs(a[t][c] < eps))continue;
for (int i = c; i <= n; i++)swap(a[t][i], a[r][i]);
for (int i = n; i >= c; i--)a[r][i] /= a[r][c];
for (int i = r + 1; i < n; i++)
if (fabs(a[i][c] > eps))
for (int j = n; j >= c; j--)
a[i][j] -= a[r][j] * a[i][c];
r++;
}
if (r < n)
{
for (int i = r; i < n; i++)
if (fabs(a[i][n]) > eps)
return 2;//无解
return 1;//有无穷多组解
}
for (int i = n - 1; i >= 0; i--)
for (int j = i + 1; j < n; j++)
a[i][n] -= a[i][j] * a[j][n];
return 0;//有唯一解
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n + 1; j++)
cin >> a[i][j];
}
int t = gauss();
if (t == 0)
{
for (int i = 0; i < n; i++)printf("%.2lf\n", a[i][n]);
}
else if (t == 1)puts("Infinite group solutions");
else puts("No solution");
return 0;
}
12 求组合数1
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
using namespace std;
const int N = 2010, mod = 1e9 - 7;
int c[N][N];
void init()
{
for (int i = 0; i < N; i++)
for (int j = 0; j <= i; j++)
if (!j)c[i][j] = 1;
else c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
}
int main()
{
init();
int n;
scanf("%d", &n);
while (n--)
{
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", c[a][b]);
}
return 0;
}
13 求组合数2
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
using namespace std;
typedef long long LL;
const int N = 100010, mod = 1e9 + 7;
int fact[N], infact[N];
int qmi(int a, int k, int p)
{
int res = 1;
while (k)
{
if (k & 1)res = (LL)res * a % p;
a = (LL)a * a % p;
k >>= 1;
}
return res;
}
int main()
{
fact[0] = infact[0] = 1;
for (int i = 1; i < N; i++)
{
fact[i] = (LL)fact[i - 1] * i % mod;
infact[i] = (LL)infact[i - 1] * qmi(i, mod - 2, mod) % mod;
}
int n;
scanf("%d", &n);
while (n--)
{
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", (LL)fact[a] * infact[b] % mod * infact[a - b] % mod);
}
return 0;
}
14 求组合数3
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
using namespace std;
typedef long long LL;
int p;
int qmi(int a, int k)
{
int res = 1;
while (k)
{
if (k & 1)res = (LL)res * a % p;
a = (LL)a * a % p;
k >>= 1;
}
return res;
}
int C(int a, int b)
{
int res = 1;
for (int i = 1, j = a; i <= b; i++, j--)
{
res = (LL)res * j % p;
res = (LL)res * qmi(i, p - 2) % p;
}
return res;
}
int lucas(LL a, LL b)
{
if (a < p && b < p)return C(a, b);
return (LL) C(a % p, b % p) * lucas(a / p, b / p) % p;
}
int main()
{
int n;
cin >> n;
while (n--)
{
LL a,b;
cin >> a >> b >> p;
cout << lucas(a, b) << endl;
}
return 0;
}
15 求组合数4
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
#include<vector>
using namespace std;
const int N = 5010;
int primes[N], cnt;
int sum[N];
bool st[N];
void get_primes(int n)
{
for (int i = 2; i <= n; i++)
{
if (!st[i])primes[cnt++] = i;
for (int j = 0; primes[j] <= n / i; j++)
{
st[primes[j] * i] = true;
if (i % primes[j] == 0)break;
}
}
}
int get(int n, int p)
{
int res = 0;
while (n)
{
res += n / p;
n /= p;
}
return res;
}
vector<int>mul(vector<int>a, int b)
{
vector<int>c;
int t = 0;
for (int i = 0; i < a.size(); i++)
{
t += a[i] * b;
c.push_back(t % 10);
t /= 10;
}
while (t)
{
c.push_back(t % 10);
t /= 10;
}
return c;
}
int main()
{
int a, b;
cin >> a >> b;
get_primes(a);
for (int i = 0; i < cnt; i++)
{
int p = primes[i];
sum[i] = get(a, p) - get(b, p) - get(a - b, p);
}
vector<int>res;
res.push_back(1);
for (int i = 0; i < cnt; i++)
for (int j = 0; j < sum[i]; j++)
res = mul(res, primes[i]);
for (int i = res.size() - 1; i >= 0; i--)printf("%d", res[i]);
puts(" ");
return 0;
}
16 满足条件的01序列
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
#include<vector>
using namespace std;
typedef long long LL;
const int mod = 1e9 + 7;
int qmi(int a, int k, int p)
{
int res = 1;
while (k)
{
if (k & 1)res = (LL)res * a % p;
a = (LL)a * a % p;
k >>= 1;
}
return res;
}
int main()
{
int n;
cin >> n;
int a = 2 * n, b = n;
int res = 1;
for (int i = a; i > a - b; i--)res = (LL)res * i % mod;
for (int i = 1; i <= b; i++)res = (LL)res * qmi(i, mod - 2, mod) % mod;
res = (LL)res * qmi(n + 1, mod - 2, mod) % mod;
cout << res << endl;
return 0;
}
17 能被整除的数
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;
typedef long long LL;
const int N = 20;
int n, m;
int p[N];
int main()
{
cin >> n >> m;
for (int i = 0; i < m; i++)cin >> p[i];
int res = 0;
for (int i = 1; i < 1 << m; i++)
{
int t = 1, cnt = 0;
for(int j=0;j<m;j++)
if (i >> j & 1)
{
cnt++;
if ((LL)t * p[j] > n)
{
t = -1;
break;
}
t *= p[j];
}
if (t!= -1)
{
if (cnt % 2)res += n / t;
else res -= n / t;
}
}
cout << res << endl;
}
18 Nim游戏
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
int n;
int res = 0;
scanf("%d", &n);
while (n--)
{
int x;
scanf("%d", &x);
res ^= x;
}
if (res)puts("Yes");
else puts("No");
return 0;
}
19 集合–Nime游戏
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
#include<unordered_set>
using namespace std;
const int N = 110, M = 10010;
int n, m;
int s[N], f[M];
int sg(int x)
{
if (f[x] != -1)return f[x];
unordered_set<int>S;
for (int i = 0; i < m; i++)
{
int sum = s[i];
if (x >= sum)S.insert(sg(x - sum));
}
for (int i = 0; ; i++)
if (!S.count(i))
return f[x] = i;
}
int main()
{
cin >> m;
for (int i = 0; i < m; i++)cin >> s[i];
cin >> n;
memset(f, -1, sizeof f);
int res = 0;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
res ^= sg(x);
}
if (res)puts("Yes");
else puts("No");
return 0;
}