数论基础
来源:互联网 发布:对人工智能的看法 编辑:程序博客网 时间:2024/06/10 09:14
单一素数判定,求约数个数
#include<cstdio>#include<vector>#include<map>#include<cmath>using namespace std;int is_prime(int a){ int b; for(b=2;b*b<=a;b++) if(a%b==0) return 0; return a!=1; //非常重要,1是一个例外}//返回整除该数的向量vector<int> divisor(int a){ vector<int>res; for(int b=1;b*b<=a;b++) { if(a%b==0) { if(b!=a/b) { res.push_back(b); res.push_back(a/b); } else res.push_back(b); } } return res;}map<int,int>prime_factor(int n){ map<int,int>res; for(int i=2;i*i<=n;i++) while(n%i==0) { ++res[i]; n/=i; } if(n!=1) res[n]=1; return res;}int main(){ int n; scanf("%d",&n); printf("%s\n",is_prime(n)?"yes":"no"); vector<int>d=divisor(n); printf("%d\n",d.size());}
快速幂,以及快速幂求模
#include<cstdio>#include<algorithm>using namespace std;long long quickmod(long long a,long long b,long long m){ long long ans=1; while(b) //用一个循环从右到左遍历b所有的二进制位 { if(b&1) //判断此时b[i]的二进制位是否为1 { ans=(ans*a)%m; b--; } b/=2; a=a*a%m; } return ans;}int main(){ //求a的b次方在mod c下的余数值 long long a,b; scanf("%lld%lld",&a,&b); long long c; scanf("%lld",&c); printf("%lld\n",quickmod(a,b,c)); return 0;}
拓展欧几里得算法
#include<cstdio>int extgcd(int a,int b,int& x,int& y){ int d=a; if(b!=0) { d=extgcd(b,a%b,y,x); y-=(a/b)*x; } else { x=1;y=0; } return d;}int main(){ int x;int y; scanf("%d%d",&x,&y); int m,n; extgcd(x,y,m,n); printf("%d %d",m,n);}欧几里得算法
#include<cstdio>int lcd(int a,int b){ if(b==0) return a; else return lcd(b,a%b);}int main(){ int a,b; scanf("%d%d",&a,&b); printf("%d\n",lcd(a,b));}
0 0
- 数论基础
- 数论基础
- 数论 基础
- 数论基础
- 数论基础
- 数论基础
- 基础数论
- 基础数论
- 数论基础
- 数论基础
- 数论基础
- 数论基础
- 数论基础
- 数论基础
- 数论基础
- 数论基础
- 数论基础
- 同余(数论基础)
- 设计模式之装饰器模式
- Httpclient post和get请求
- C++ 线程 共享资源保护
- eclipse出错打不开:Failed to load the JNI shared library
- Java实现链表结构的头插法与尾插法
- 数论基础
- C语言入门——多个数学问题求解
- Hadoop 图处理
- 开发环境变量配置
- Windows 10 不能搜索本地应用程序
- 关于用Eclipse的JSP的成功连接SQL server 2008代码分享
- 关于Eclipse出现Address already in use的几个建议
- 克隆二叉树
- Web学习-jsp实现servlet过程赏析