当前位置:网站首页>Introduction to number theory (greatest common divisor, prime sieve, inverse element)
Introduction to number theory (greatest common divisor, prime sieve, inverse element)
2022-07-06 08:00:00 【D αī s ч】
One 、 greatest common divisor (gcd)
ll gcd(ll x, ll y)
{
return y ? gcd(y, x % y) : x;
}
gcd(a,b,c)=gcd(a,b-a,c-b) Most of them can be promoted
Two 、 Prime sieve ( Euler screen , For Euler function )
int least[maxn], prime[maxn], phi[maxn], tot;
void euler(int n)
{
int i, j;
for (i = 2; i <= n; i++)
{
if (!least[i])
{
least[i] = i;
prime[++tot] = i;
phi[i] = i - 1;
}
for (j = 1; j <= tot && i * prime[j] <= n; j++)
{
least[i * prime[j]] = prime[j];
if (i % prime[j] == 0)
{
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
else
{
phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
}
}
}
3、 ... and 、 Fast power inverse ( Used for finding large combinatorial numbers )
// Calculate large numbers (a/b)mod c
ll quick_pow(ll a, ll b)
{
ll ans = 1;
while (b)
{
if (b & 1)
ans = (ans * a) % mod;
b >>= 1;
a = (a * a) % mod;
}
return ans;
}
ll inv(ll a, ll b)
{
return (a * quick_pow(b, mod - 2)) % mod;
}
ll fa[maxn], fb[maxn];
ll power(ll a, ll b)
{
ll ans = 1;
while (b)
{
if (b & 1)
ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
ll C(ll n, ll k)
{
if (k<0 || k>n)
return 0;
return fa[n] * fb[k] % mod * fb[n - k] % mod;
}
void init()
{
int i;
fa[0] = 1;
fb[0] = 1;
for (i = 1; i < maxn; i++)
{
fa[i] = fa[i - 1] * i % mod;
fb[i] = power(fa[i], mod - 2) % mod;
}
}
边栏推荐
- C语言自定义类型:结构体
- MFC sends left click, double click, and right click messages to list controls
- Le chemin du navigateur Edge obtient
- Nft智能合约发行,盲盒,公开发售技术实战--拼图篇
- (lightoj - 1410) consistent verbs (thinking)
- Nc204382 medium sequence
- Data governance: 3 characteristics, 4 transcendence and 3 28 principles of master data
- In the era of digital economy, how to ensure security?
- MySQL view tablespace and create table statements
- Binary tree creation & traversal
猜你喜欢
Mex related learning
Document 2 Feb 12 16:54
Make learning pointer easier (3)
File upload of DVWA range
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
ROS learning (IX): referencing custom message types in header files
Secure captcha (unsafe verification code) of DVWA range
解决方案:智慧工地智能巡檢方案視頻監控系統
Golang DNS 随便写写
[count] [combined number] value series
随机推荐
Solution: système de surveillance vidéo intelligent de patrouille sur le chantier
Wireshark grabs packets to understand its word TCP segment
How to prevent Association in cross-border e-commerce multi account operations?
Epoll and IO multiplexing of redis
Convolution, pooling, activation function, initialization, normalization, regularization, learning rate - Summary of deep learning foundation
PHP - Common magic method (nanny level teaching)
Generator Foundation
[untitled]
Notes on software development
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
使用 BR 备份 TiDB 集群数据到兼容 S3 的存储
Description of octomap averagenodecolor function
Asia Pacific Financial Media | designer universe | Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers
C语言 - 位段
MEX有关的学习
Webrtc series-h.264 estimated bit rate calculation
NFT smart contract release, blind box, public offering technology practice -- contract
[cf gym101196-i] waif until dark network maximum flow
使用 TiUP 升级 TiDB
[redis] Introduction to NoSQL database and redis