国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

擴(kuò)展歐幾里得專(zhuān)題(一)

2019-11-08 19:30:53
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

HDU 1222

鏈接 : HDU 1222

題解

很容易推出如果n, m互質(zhì), 則不存在安全洞 擴(kuò)展歐幾里得求gcd


code

//package adrui;import java.util.*;public class Main{ public static void main(String...args){ Scanner in = new Scanner(System.in); int t = in.nextInt(); while(t-- > 0){ long m = in.nextLong(), n = in.nextLong(); long ans = expgcd(m, n); String tmp = "YES"; if(ans == 1) tmp = "NO"; System.out.PRintln(tmp); } in.close(); } public static long expgcd(long a, long b){ if(b == 0){ return a; } return expgcd(b, a % b); }}

HDU 1576

鏈接:HDU 1576

題解

推一下式子就成, 注意B的系數(shù)要取反


code

//package adrui;import java.util.*;public class Main{ static long c; public static void main(String...args){ Scanner in = new Scanner(System.in); int t = in.nextInt(); while(t-- > 0){ c = in.nextLong(); long[] x = new long[1];/**數(shù)組實(shí)現(xiàn)引用*/ long[] y = new long[1]; long n = in.nextLong(); expgcd(9973, n, x, y); y[0] = -y[0]; long ans = y[0] % 9973; if(ans < 0) ans += 9973; System.out.println(ans); } in.close(); } public static void expgcd(long a, long b, long[] x, long[] y){ if(b == 0){ x[0] = -c; y[0] = 0; return; } expgcd(b, a % b, y, x); y[0] -= a / b * x[0]; }}

HDU 2669

鏈接 : HDU 2669

題解

擴(kuò)歐模板, 注意要是整數(shù)解, 至于x要滿(mǎn)足最小非負(fù), 對(duì)b / gcd(a, b)取模就好了 容易得知如果有解, a, b互質(zhì), 所以有解時(shí)直接對(duì)b取模


code

/** *@author : adrui */import static java.lang.System.*;import java.util.*;public class Main{ static boolean f;/**靜態(tài)全局*/ public static void main(String...args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ f = false; int a = in.nextInt(), b = in.nextInt(); int[] x = new int[1]; int[] y = new int[1]; expgcd(a, b, x, y); if(!f) out.println("sorry"); else{ int ans = x[0] % b; if(ans < 0) ans += b;/** b = b / gcd(a, b) */ out.println(ans + " " + (1 - (long)ans * a) / b);/** ans * a 可能會(huì)爆int, 用通解也可以*/ } } } public static void expgcd(int a, int b, int[] x, int[] y){ if(b == 0){ if(a == 1) { x[0] = 1; y[0] = 0; f = true;/**有解標(biāo)記*/ } return; } expgcd(b, a % b, y, x); y[0] -= a / b * x[0]; }}

HDU 2504

鏈接 : HDU 2504

裸題

code

/** *@author : adrui */import static java.lang.System.*;import java.util.*;public class Main{ public static void main(String...args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); while(n-- > 0){ int a = in.nextInt(), b = in.nextInt(); for(int i = b + b; ; i += b){ if(expgcd(a, i) == b){ out.println(i); break; } } } } public static int expgcd(int a, int b){ if(b == 0){ return a; } return expgcd(b, a % b); }}

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 饶阳县| 九龙坡区| 牡丹江市| 呼伦贝尔市| 布尔津县| 安吉县| 清河县| 武胜县| 桂东县| 彩票| 中牟县| 贺兰县| 大洼县| 婺源县| 定陶县| 涟水县| 长治市| 读书| 高陵县| 大悟县| 新泰市| 德州市| 余江县| 凭祥市| 扎兰屯市| 池州市| 临潭县| 吉林市| 桃源县| 会宁县| 安丘市| 宜都市| 姚安县| 大港区| 合肥市| 镇坪县| 潞城市| 舒兰市| 孟津县| 吴川市| 哈密市|