当前位置:网站首页>Number theory knowledge
Number theory knowledge
2022-08-09 11:15:00 【Rachel caramel】
1.快速幂
2^10
= 4^5
=4*16^2
= 4 *256
eg.求a^b
int power(int a,int b)
{
int res=1;
while(b)
{
if(b&1) res*=a;
b>>=1;
a=a*a;
}
return res;
}
Casting may be required at some point模板例题
2.
a=4*a;//等价于a<<=2
a*=10;//等价于(a<<3)+(a<<1)
a*=15;//等价于(a<<4)-a
11111(30个1)等价于(1<<31)-1
//a,b交换位置
a^=b^=a^=b;
4.Yang Hui's triangle and permutations
5.质数筛
The most common judgment O(n*n^(1/2))
int prime[maxn];
int cnt=0;
prime[++cnt]=2;
prime[++cnt]=3;
for(int i=4;i<=n;i++)
{
int flag=1;
for(int j=2;j*j<=4;j++)
{
if(i%j==0)
{
flag=0;
break;
}
}
if(flag) prime[++cnt]=i;
}
garbage sieve(埃拉托斯特尼筛法)
bool isprime[maxn];
int prime[maxn];
int cnt=0;
int getprime(int n)
{
for(int i=1;i<=n;i++) isprime[i]=1;
isprime[1]=0;
for(int i=2;i<=n;i++)
{
if(isprime[i])
{
prime[++cnt]=i;
if((long long)i*i<n)
{
for(int j=i*i;j<=n;j+=i) isprime[j]=0;
}
}
}
rertun cnt;
}
线性筛(欧拉筛)
void getprime()
{
for(int i=2;i<=n;i++)
{
if (!notprime[i]) prime[++tot]=i;
for (int j=1;j<=tot&&i*prime[j]<=n;j++)
{
notprime[i*prime[j]]=1;
if (i%prime[j]==0) break;
}
}
}
理解:
if (i%prime[j]==0) break;
text{这一句是关键,如果i=4的话,当prime[j]=2时,标记8不是素数,Then it jumps out of the loop
If you do not jump out of the loop at this time,12will be screened at this time,但
4 ⋅ 3 = 6 ⋅ 2 = 12 4\cdot 3=6\cdot 2=12 4⋅3=6⋅2=12
实际上12应该被2筛掉,That is, it is screened out by his smallest prime number,因此当i是prime[j]should jump out of the loop directly
6.
^可以看成+(Simple notation
1^1=0 1+1=10=0
1^0=1 1+0=0
0^0=0 0+0=0
7.
例题
gcd (grand common divisor,即最大公约数)
(a,b)即求a和b的最大公约数
(a,b)=(b,b%a)
证明:
a = k 1 ⋅ c b = k 2 ⋅ c a=k_1\cdot c\\ b=k_2\cdot c a=k1⋅cb=k2⋅c
即(a,b)=( k 1 ⋅ c , b = k 2 ⋅ c k_1\cdot c,b=k_2\cdot c k1⋅c,b=k2⋅c)
所以( k 1 , k 2 k_1,k_2 k1,k2)=1
b = k 2 ⋅ c = k 3 ⋅ d a m o d b = ( k 1 , k 2 ) ⋅ c = c b=k_2\cdot c=k_3\cdot d\\ a\bmod b =(k_1,k_2)\cdot c=c b=k2⋅c=k3⋅damodb=(k1,k2)⋅c=c
int gcd(int a,int b)
{
if (!b) return a;
return gcd(b,a%b);
}
int gcd(int a,int b)
{
while (b)
{
int t=b;
b=a%b;
a=t;
}
return a;
}
#include <algorithm>
__gcd(x,y);
9.还需理解
逆元
eg.
3*4=1(mod11)
这里4是3的逆元,相当于1/3
oiwikiThe inverse element explanation of
边栏推荐
猜你喜欢
程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果
去除蜂窝状的噪声(matlab实现)
图片查看器viewer
electron 应用开发优秀实践
Preparation for gold three silver four: how to successfully get an Ali offer (experience + interview questions + how to prepare)
信号量SIGCHLD的使用,如何让父进程得知子进程执行结束,如何让父进程区分多个子进程的结束
CentOS6.5 32bit安装Oracle-11gR2步骤说明
人物 | 从程序员到架构师,我是如何快速成长的?
x86异常处理与中断机制(3)中断处理过程
备战金三银四:如何成功拿到阿里offer(经历+面试题+如何准备)
随机推荐
聚类了解
faster-rcnn学习
End-to-End Object Detection with Fully Convolutional Network学习笔记
gdb tui的使用
程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果
Getting Started with MNIST Machine Learning
学长告诉我,大厂MySQL都是通过SSH连接的
学习阶段总结(背包问题)
Error: Cannot find module ‘./application‘
Netscope: Online visualization tool for neural network structures
PTA 指定位置输出字符串(c)
PTA 找出不是两个数组共有的元素
centos7.5 设置Mysql开机自启动
CSDN的markdown编辑器语法完整大全
【精华文】C语言结构体特殊情况分析:结构体指针 / 基本数据类型指针,指向其他结构体
Jmeter BeanShell post processor
激光条纹中心提取——Steger
MySQL传统方案和通过SSH连接哪个好?
Official explanation, detailed explanation and example of torch.cat() function
MySQL查询性能优化七种武器之索引潜水