1. 题目描述
求乘法逆元。

2. 基本思路
利用扩展gcd求逆元,模板题目。

3. 代码

 /* 3609 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 #define LL long long void egcd(LL a, LL b, LL& g, LL& x, LL& y) {
if (!b) {
g = a;
x = ;
y = ;
} else {
egcd(b, a%b, g, y, x);
y -= a/b*x;
}
} int inv(LL a, LL n) {
LL g, x, y; egcd(a, n, g, x, y);
if (g != )
return -; return x%n== ? n:(x%n+n)%n;
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t;
LL a, m;
LL ans; scanf("%d", &t);
while (t--) {
scanf("%lld%lld", &a, &m);
ans = inv(a, m);
if (ans == -) {
puts ("Not Exist");
} else {
printf("%lld\n", ans);
}
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

4. 代码生成器

 import sys
import string
from random import randint def GenData(fileName):
with open(fileName, "w") as fout:
t = 1000
fout.write("%d\n" % (t))
for tt in xrange(t):
n = randint(1, 1000)
m = randint(1, 1000)
fout.write("%d %d\n" % (n, m)) def MovData(srcFileName, desFileName):
with open(srcFileName, "r") as fin:
lines = fin.readlines()
with open(desFileName, "w") as fout:
fout.write("".join(lines)) def CompData():
print "comp"
srcFileName = "F:\Qt_prj\hdoj\data.out"
desFileName = "F:\workspace\cpp_hdoj\data.out"
srcLines = []
desLines = []
with open(srcFileName, "r") as fin:
srcLines = fin.readlines()
with open(desFileName, "r") as fin:
desLines = fin.readlines()
n = min(len(srcLines), len(desLines))-1
for i in xrange(n):
ans2 = int(desLines[i])
ans1 = int(srcLines[i])
if ans1 > ans2:
print "%d: wrong" % i if __name__ == "__main__":
srcFileName = "F:\Qt_prj\hdoj\data.in"
desFileName = "F:\workspace\cpp_hdoj\data.in"
GenData(srcFileName)
MovData(srcFileName, desFileName)

【ZOJ】3609 Modular Inverse的更多相关文章

  1. zjuoj 3609 Modular Inverse

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3609 Modular Inverse Time Limit: 2 Seco ...

  2. 【ZOJ】4012 Your Bridge is under Attack

    [ZOJ]4012 Your Bridge is under Attack 平面上随机n个点,然后给出m条直线,问直线上有几个点 \(n,m \leq 10^{5}\) 由于共线的点不会太多,于是我们 ...

  3. 【线性代数】2-5:逆(Inverse)

    title: [线性代数]2-5:逆(Inverse) toc: true categories: Mathematic Linear Algebra date: 2017-09-11 20:00:1 ...

  4. ZOJ 3609 Modular Inverse(拓展欧几里得求最小逆元)

    Modular Inverse Time Limit: 2 Seconds      Memory Limit: 65536 KB The modular modular multiplicative ...

  5. ZOJ——3609 Modular Inverse

    Modular Inverse Time Limit: 2 Seconds      Memory Limit: 65536 KB The modular modular multiplicative ...

  6. 【ZOJ】【3329】One Person Game

    概率DP/数学期望 kuangbin总结题目中的第三道 看来还是没有进入状态啊……都说是DP了……当然是要找[状态之间的转移关系]了…… 本题中dp[i]跟 dp[i-(k1+k2+k3)] 到dp[ ...

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

    [算法]有上下界网络流-无源汇(循环流) [题解]http://www.cnblogs.com/onioncyc/p/6496532.html //未提交 #include<cstdio> ...

  8. ZOJ 3609 Modular Inverse(扩展欧几里德)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712 The modular modular multiplicat ...

  9. ZOJ 3609 Modular Inverse

    点我看题目 题意 : 这个题是求逆元的,怎么说呢,题目看着很别扭....就是给你a和m,让你求一个最小的x满足a-1≡x (mod m).或者ax≡1 (mod m).通俗点说呢,就是找一个最小的x, ...

随机推荐

  1. Linux C 程序 指针和字符串函数(11)

    指向字符串的指针 C语言访问字符串很多方法:1.用字符数组存放一个字符串 char string[] = "Linux C"; printf("%s\n".st ...

  2. javascript 面向对象技术

    面向对象术语 对象 ECMA-262 把对象(object)定义为“属性的无序集合,每个属性存放一个原始值.对象或函数”.严格来说,这意味着对象是无特定顺序的值的数组. 尽管 ECMAScript 如 ...

  3. 多线程的单元测试工具 - GroboUtils

    写过Junit单元测试的同学应该会有感觉,Junit本身是不支持普通的多线程测试的,这是因为Junit的底层实现上,是用System.exit退出用例执行的.JVM都终止了,在测试线程启动的其他线程自 ...

  4. max_size, capacity and size 的区别

    The max_size() function returns the maximum number of elements that the container can hold. The max_ ...

  5. Python执行效率测试模块timei的使用方法与与常用Python用法的效率比较

    timeit模块用于测试一段代码的执行效率 1.Timer类 Timer 类: __init__(stmt="pass", setup="pass", time ...

  6. String Subtraction

    Given two strings S1 and S2, S = S1 - S2 is defined to be the remaining string after taking all the ...

  7. 关于Eigen库在Visual Studio2013中传参对齐报错问题

    Error as follow: 具体问题及解决办法描述如下: (引自http://www.fx114.net/qa-278-97757.aspx) /************************ ...

  8. mysql建表且某字段内不允许出现重复值

    CREATE TABLE `admin` ( `id` ) NOT NULL AUTO_INCREMENT , `username` varchar() NOT NULL , `password` v ...

  9. jQuery的优点——(一)

    稍微学点js然后就用起jQuery,用久了,就好像自己都成高手了,想得少了,都觉得自己就是js高手了.哗啦啦的惭愧啊=.= 面试GY的时候,因为在简历上写着大大的会用jQuery快速开发原型页面,然后 ...

  10. uva 1103

    弄懂题意后  其实就是一个dfs /************************************************************************* > Aut ...