#include <bits/stdc++.h>
typedef long long ll;
ll gcd(ll n, ll m)
{
if (!m)
return n;
return gcd(m, n % m);
}
int main(int argc, char **argv)
{
std::ios_base::sync_with_stdio(false);
ll n, m;
std::cin >> n >> m;
std::cout << gcd(n, m) << std::endl;
return 0;
}
ExGcd
#include <bits/stdc++.h>
typedef long long ll;
void ExGcd(ll n, ll m, ll &d, ll &x, ll &y)
{
if (!m)
{
d = n;
x = 1;
y = 0;
return;
}
ExGcd(m, n % m, d, y, x);
y -= (n / m) * x;
}
int main(int argc, char **argv)
{
std::ios_base::sync_with_stdio(false);
ll n, m, d, a, b;
std::cin >> n >> m;
ExGcd(n, m, d, a, b);
std::cout << d << ' ' << a << ' ' << b << std::endl;
return 0;
}
快速幂/快速乘取模
#include <bits/stdc++.h>
typedef long long ll;
ll kysumi(ll x, ll y, const ll p = 998244353ll)
{
ll ans = 1;
while (y)
{
if (y & 1)
ans = ans * x % p;
x = x * x % p;
y >>= 1;
}
return ans;
}
int main(int argc, char **argv)
{
std::ios_base::sync_with_stdio(false);
ll n, m;
std::cin >> n >> m;
std::cout << kysumi(n, m) << std::endl;
return 0;
}
素数筛法
#include <bits/stdc++.h>
const size_t _Siz = 210002;
bool notPrime[_Siz] = { true, true };
int prime[_Siz] = { 0 }, tot = 0, phi[_Siz]= { 0 };
void ul(const int n)
{
for (int i = 2; i <= n; i++)
{
if (!notPrime[i])
{
prime[tot++] = i;
phi[i] = i - 1;
}
for (int j = 0; j < tot && i * prime[j] < n; j++)
{
notPrime[i * prime[j]] = true;
if (i % prime[j] == 0)
{
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
else
phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
}
}
int main(int argc, char **argv)
{
std::ios_base::sync_with_stdio(false);
ll n, m;
std::cin >> n;
ul(n);
for (int i = 1; i <= tot; i++)
std::cout << prime[i] << '\t';
return 0;
}