惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
AWS News Blog
AWS News Blog
V
Vulnerabilities – Threatpost
D
Darknet – Hacking Tools, Hacker News & Cyber Security
量子位
博客园 - 叶小钗
AI
AI
T
Tor Project blog
Forbes - Security
Forbes - Security
W
WeLiveSecurity
博客园_首页
爱范儿
爱范儿
J
Java Code Geeks
B
Blog
G
GRAHAM CLULEY
aimingoo的专栏
aimingoo的专栏
Cloudbric
Cloudbric
C
CXSECURITY Database RSS Feed - CXSecurity.com
TaoSecurity Blog
TaoSecurity Blog
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
有赞技术团队
有赞技术团队
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News
H
Help Net Security
博客园 - 三生石上(FineUI控件)
C
Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园 - 司徒正美
The Last Watchdog
The Last Watchdog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
S
Secure Thoughts
Spread Privacy
Spread Privacy
F
Fortinet All Blogs
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
About on SuperTechFans
Security Latest
Security Latest
Webroot Blog
Webroot Blog
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog

博客园 - 逍遥子

MSN空间上的以往技术贴整理 Link Java to Maple - 逍遥子 C++的多态的被覆盖的问题 类引用机制 - 逍遥子 - 博客园 防止盗链的简单技术 验证码(CAPTCHA)的破解技术 校内网狗狗刷骨机完全版(不再维护) - 逍遥子 - 博客园 HTTP外挂的简单实现 C++的中英文字符串表示(string,wstring) C++学习之2--10.09题目答案 C++学习之一--基本介绍 下面内容为C++的教学 VC6的C++标准支持不完全 XML基础学习1 QuickSort和MergeSort算法及其效率比较源代码 继承、重载运算符、虚函数与向量、矩阵运算 数据库时间段分组查询解决方法和数据转储方法 Java 通配符匹配查找文件 动态加载Java运行环境和运行实例
数论中求公因子、求模逆的算法
逍遥子 · 2008-06-12 · via 博客园 - 逍遥子

数论中求公因子、求模逆的算法

都是采用欧几里德辗转算法,第一个还好理解,后一个比较复杂,具体的数学原理就不具体介绍了,这是专业的数学问题而不是计算机算法问题。有兴趣可以查看一下数论的书《初等数论》(闵嗣鹤、严士健),对一般人而言够用就行。

  1import java.util.Vector;
  2
  3
  4/**
  5 * 数论计算
  6 * @author xyz
  7 *
  8 */

  9
 10public class ShuLun {
 11
 12    /**
 13     * Get GCD
 14     * @param a
 15     * @param b
 16     * @return
 17     */

 18    static long gcd(long a,long b)
 19    {
 20        if(a<b)
 21        {
 22            long temp=a;
 23            a=b;
 24            b=temp;
 25        }

 26        
 27        long c = a;
 28        long d = b;
 29        long test = 0;
 30        while((test = c%d) > 0)
 31        {            
 32            c = d;
 33            d = test;
 34        }

 35        
 36        return d;
 37    }

 38    
 39    /**
 40     * Equation ax+by=1,GCD(a,b)=1
 41     * @param a
 42     * @param b
 43     * @return long[] x,y
 44     */

 45    static long[] getEquationXY(long a,long b)
 46    {
 47        boolean isChange = false;
 48        if(a<b)
 49        {
 50            long temp=a;
 51            a=b;
 52            b=temp;
 53            isChange = true;
 54        }

 55        
 56        int n=0;
 57        long k,p0=1,p1=0,q0=0,q1=1,temp;
 58        
 59        long c = a;
 60        long d = b;
 61        long test = 0;
 62        while((test = c%d) > 0)
 63        {  
 64            n++;
 65            k = c/d;
 66            
 67            if(n==1)
 68            {
 69                p1 = k;
 70                q1 = 1;
 71            }
else
 72            {
 73                temp = p1;
 74                p1 = p1*k+p0;
 75                p0 = temp;
 76                
 77                temp = q1;
 78                q1 = q1*k+q0;
 79                q0 = temp;
 80            }

 81            
 82            c = d;
 83            d = test;
 84        }

 85        
 86        if(n%2==1)
 87        {
 88            p1 = 0 - p1;
 89        }
else
 90        {
 91            q1 = 0 - q1;
 92        }

 93        
 94        if(isChange)
 95        {
 96            temp = p1;
 97            p1 = q1;
 98            q1 = temp;
 99        }

100        
101        return new long[]{q1,p1};
102    }

103    
104    /**
105     * EQ: ax=1 (mod m)
106     * @param a
107     * @param mod
108     * @return inverse,if -1 there's no inverse!
109     */

110    static long getInverse(long a,long m)
111    {
112        if(gcd(a,m)==1)
113        {
114            long xy[] = getEquationXY(a,m);
115            long temp = xy[0]%m;
116            if(temp<0)
117            {
118                temp+=m;
119            }

120            return temp;
121        }

122        return -1;
123    }

124    
125    
126    public static void main(String arg[])
127    {
128    }

129    
130}

131