【ZOJ】3609 Modular Inverse
1. 题目描述
求乘法逆元。
2. 基本思路
利用扩展gcd求逆元,模板题目。
3. 代码
1 /* 3609 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector> 10 #include <deque> 11 #include <bitset> 12 #include <algorithm> 13 #include <cstdio> 14 #include <cmath> 15 #include <ctime> 16 #include <cstring> 17 #include <climits> 18 #include <cctype> 19 #include <cassert> 20 #include <functional> 21 #include <iterator> 22 #include <iomanip> 23 using namespace std; 24 //#pragma comment(linker,"/STACK:102400000,1024000") 25 26 #define sti set<int> 27 #define stpii set<pair<int, int> > 28 #define mpii map<int,int> 29 #define vi vector<int> 30 #define pii pair<int,int> 31 #define vpii vector<pair<int,int> > 32 #define rep(i, a, n) for (int i=a;i<n;++i) 33 #define per(i, a, n) for (int i=n-1;i>=a;--i) 34 #define clr clear 35 #define pb push_back 36 #define mp make_pair 37 #define fir first 38 #define sec second 39 #define all(x) (x).begin(),(x).end() 40 #define SZ(x) ((int)(x).size()) 41 #define lson l, mid, rt<<1 42 #define rson mid+1, r, rt<<1|1 43 44 #define LL long long 45 46 void egcd(LL a, LL b, LL& g, LL& x, LL& y) { 47 if (!b) { 48 g = a; 49 x = 1; 50 y = 0; 51 } else { 52 egcd(b, a%b, g, y, x); 53 y -= a/b*x; 54 } 55 } 56 57 int inv(LL a, LL n) { 58 LL g, x, y; 59 60 egcd(a, n, g, x, y); 61 if (g != 1) 62 return -1; 63 64 return x%n==0 ? n:(x%n+n)%n; 65 } 66 67 int main() { 68 ios::sync_with_stdio(false); 69 #ifndef ONLINE_JUDGE 70 freopen("data.in", "r", stdin); 71 freopen("data.out", "w", stdout); 72 #endif 73 74 int t; 75 LL a, m; 76 LL ans; 77 78 scanf("%d", &t); 79 while (t--) { 80 scanf("%lld%lld", &a, &m); 81 ans = inv(a, m); 82 if (ans == -1) { 83 puts ("Not Exist"); 84 } else { 85 printf("%lld\n", ans); 86 } 87 } 88 89 #ifndef ONLINE_JUDGE 90 printf("time = %d.\n", (int)clock()); 91 #endif 92 93 return 0; 94 }
4. 代码生成器
1 import sys 2 import string 3 from random import randint 4 5 6 def GenData(fileName): 7 with open(fileName, "w") as fout: 8 t = 1000 9 fout.write("%d\n" % (t)) 10 for tt in xrange(t): 11 n = randint(1, 1000) 12 m = randint(1, 1000) 13 fout.write("%d %d\n" % (n, m)) 14 15 16 def MovData(srcFileName, desFileName): 17 with open(srcFileName, "r") as fin: 18 lines = fin.readlines() 19 with open(desFileName, "w") as fout: 20 fout.write("".join(lines)) 21 22 23 def CompData(): 24 print "comp" 25 srcFileName = "F:\Qt_prj\hdoj\data.out" 26 desFileName = "F:\workspace\cpp_hdoj\data.out" 27 srcLines = [] 28 desLines = [] 29 with open(srcFileName, "r") as fin: 30 srcLines = fin.readlines() 31 with open(desFileName, "r") as fin: 32 desLines = fin.readlines() 33 n = min(len(srcLines), len(desLines))-1 34 for i in xrange(n): 35 ans2 = int(desLines[i]) 36 ans1 = int(srcLines[i]) 37 if ans1 > ans2: 38 print "%d: wrong" % i 39 40 41 if __name__ == "__main__": 42 srcFileName = "F:\Qt_prj\hdoj\data.in" 43 desFileName = "F:\workspace\cpp_hdoj\data.in" 44 GenData(srcFileName) 45 MovData(srcFileName, desFileName) 46 47