【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     
时间: 2024-08-12 07:08:33

【ZOJ】3609 Modular Inverse的相关文章

ZOJ 3609 Modular Inverse (水题)

Modular Inverse Time Limit: 2 Seconds      Memory Limit: 65536 KB The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x (mod m). This is equivalent to ax≡1 (mod m). Input There are multiple test cases. Th

ZOJ 3609 Modular Inverse 解线性模方程

点击打开链接 Modular Inverse Time Limit: 2 Seconds      Memory Limit: 65536 KB The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x (mod m). This is equivalent to ax≡1 (mod m). Input There are multiple test ca

【ZOJ】3380 Patchouli&#39;s Spell Cards

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3957 题意:m个位置,每个位置填1~n的数,求至少有L个位置的数一样的概率(1<=n,m,l<=100) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct inum { static const int N=205,

【ZOJ】3785 What day is that day? ——浅谈KMP应用之ACM竞赛中的暴力打表找规律

首先声明一下,这里的规律指的是循环,即找到最小循环周期.这么一说大家心里肯定有数了吧,“不就是next数组性质的应用嘛”. 先来看一道题 ZOJ 3785 What day is that day? Time Limit: 2 Seconds      Memory Limit: 65536 KB It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days? Input There are multiple tes

【ZOJ】3430 Detect the Virus

动态建树MLE.模仿别人的代码模板各种原因wa后,终于AC. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 515*70 8 #define NXTN 256 9 10 bool visit[515]; 11 char str[3005]; 12 unsigned

ZOJ 3609 Modular Inverse

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3609 题面: Modular Inverse Time Limit: 2 Seconds      Memory Limit: 65536 KB The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x (mod m).

【有上下界网络流】【ZOJ】2314 Reactor Cooling

[算法]有上下界网络流-无源汇(循环流) [题解] 无源汇网络流相当于重建图后跑最大流. 循环流要求每个点(或边)的流入量和流出量相等. http://www.cnblogs.com/liu-runda/p/6262832.html http://hzwer.com/3356.html 入度>出度时(in[x]>0)时,需要流出去,所以从源向点引一条边,引诱它流出去. 入度<出度时(in[x]<0)时,需要流进来,所以从点向汇引一条边,引诱它流进来. 为何这样正确?源和汇的作用只是

【ZOJ】【3329】One Person Game

概率DP kuangbin总结题目中的第三道 看来还是没有进入状态啊……都说是DP了……当然是要找[状态之间的转移关系]了…… 本题中dp[i]跟 dp[i-(k1+k2+k3)] 到dp[i-1]都有关系……然后所有的dp[i]都跟dp[0]即ans有关…… 用[系数]进行转移……sigh最近越来越水了 1 //BZOJ 1000 2 #include<cmath> 3 #include<vector> 4 #include<cstdio> 5 #include<

ZOJ - 3609 —— Modular Inverse 【乘法逆,扩展欧几里得】

题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712 1. 这题数据范围太小,直接暴力即可 2. 不过其实这题也是很“直白的”求乘法逆的题目,即当b=1的特殊的模线性方程问题ax≡b mod(n),可以通过扩展欧几里得算法求解:   ax≡b mod(n) => (ax) mod n = b mod n => ax=k1*n+r ... (1) b=k2n+r    ... (2)  (1)-(2)=>ax-