#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <limits.h>
#include <math.h>
#define ll long long
#define mod 66132
using namespace std;
//龟速乘
inline ll Qmul(ll a, ll b, ll mod)
{
ll res = 0;
while (b) {
if (b & 1) res = (res + a) % mod;
a = (a + a) % mod;
b >>= 1;
}
return res;
}
//快速幂
ll qpow(ll a, ll b)
{
if (b == 0)
return 1;
if (b % 2) //奇数的话
{
return qpow(a, b - 1) * a;
}
else //偶数的话
{
ll t = qpow(a, b / 2);//一半的幂
return t * t;
}
}
inline ll Qpow(ll a, ll b)
{
a %= mod;
ll res = 1;
while (b) {
if (b & 1) res = (res % mod) * (a % mod) % mod;
a = (a % mod) * (a % mod) % mod;
b >>= 1;
}
return res;
}
inline ll Qplus(ll a, ll b)
{ a %= mod;
ll res = 0;
while (b) {
if (b & 1) res = res + a % mod;
a = a * 2 % mod;
b >>= 1;
}
return res;
}
ll qpow(ll a, ll b)
{
ll ans = 1;
while (b)
{
if (b & 1) //取二进制的末尾一位,如果是1,则
ans = ans * a % mod;//ans * a
a = a * a % mod;
b >>= 1;//二进制右移一位。
}
return ans;
}
//递归更相减损法最大公约数
ll gcd(ll a, ll b)
{
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
else
return gcd(b - a, a);
}
// 递归辗转相除法
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
ll gcd(ll a, ll b)
{
return b == 0 ? a : gcd(b, a % b);
}
// 非递归辗转相除法
ll gcd(ll a, ll b)
{
while (b)
{
ll t = b;
b = a % b;
a = t;
}
return a;
}
// 求最小公倍数
ll lcm(ll a, ll b)
{
return a * b / gcd(a, b);
}
int main()
{
ll a, b;
scanf("%lld %lld", &a, &b);
printf("%lld\n", qpow(a, b));
printf("%lld\n", gcd(a, b));
printf("%lld\n", lcm(a, b));
return 0;
}
素数:
欧拉筛
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <limits.h>
#include <math.h>
#define ll long long
#define mod 66132
#define N 10005
using namespace std;
int main()
{
ll n;
scanf("%lld", &n);
ll prime[N];//储存素数
ll cnt = 0;
bool vis[N] = {0};
vis[1] = vis[0] = 1;
int i;
for (i = 2; i <= n; i++)
{
if (vis[i] == 0)
prime[cnt++] = i;
int j;
for (j = 0; j < cnt && i * prime[j] <= n; j++)
{
vis[i * prime[j]] = 1;
if (i % prime[j] == 0)
break;
}
}
for (i = 0; i < cnt; i++)
printf("%d%c", prime[i], i == cnt - 1 ? '\n' : ' ');
return 0;
}
素数埃氏筛
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <limits.h>
#include <math.h>
#define ll long long
#define mod 66132
#define N 10005
using namespace std;
int main()
{
ll n;
scanf("%lld", &n);
ll prime[N]; //储存素数
ll cnt = 0;
bool vis[N] = {0};
vis[1] = vis[0] = 1;
int i;
for (i = 2; i * i <= n; i++)
{
int j;
if (vis[i] == 0)
{
for (j = i * i; j <= n; j += i)
vis[j] = 1;
}
}
for (i = 2; i <= n; i++)
{
if (vis[i] == 0)
prime[cnt++] = i;
}
for (i = 0; i < cnt; i++)
printf("%d%c", prime[i], i == cnt - 1 ? '\n' : ' ');
return 0;
}