【ZOJ】3609 Modular Inverse
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的更多相关文章
- zjuoj 3609 Modular Inverse
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3609 Modular Inverse Time Limit: 2 Seco ...
- 【ZOJ】4012 Your Bridge is under Attack
[ZOJ]4012 Your Bridge is under Attack 平面上随机n个点,然后给出m条直线,问直线上有几个点 \(n,m \leq 10^{5}\) 由于共线的点不会太多,于是我们 ...
- 【线性代数】2-5:逆(Inverse)
title: [线性代数]2-5:逆(Inverse) toc: true categories: Mathematic Linear Algebra date: 2017-09-11 20:00:1 ...
- ZOJ 3609 Modular Inverse(拓展欧几里得求最小逆元)
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- ZOJ——3609 Modular Inverse
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- 【ZOJ】【3329】One Person Game
概率DP/数学期望 kuangbin总结题目中的第三道 看来还是没有进入状态啊……都说是DP了……当然是要找[状态之间的转移关系]了…… 本题中dp[i]跟 dp[i-(k1+k2+k3)] 到dp[ ...
- 【有上下界网络流】【ZOJ】2314 Reactor Cooling
[算法]有上下界网络流-无源汇(循环流) [题解]http://www.cnblogs.com/onioncyc/p/6496532.html //未提交 #include<cstdio> ...
- ZOJ 3609 Modular Inverse(扩展欧几里德)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712 The modular modular multiplicat ...
- ZOJ 3609 Modular Inverse
点我看题目 题意 : 这个题是求逆元的,怎么说呢,题目看着很别扭....就是给你a和m,让你求一个最小的x满足a-1≡x (mod m).或者ax≡1 (mod m).通俗点说呢,就是找一个最小的x, ...
随机推荐
- web组件新学--layer
在之前项目后台管理界面开发中,不知道有layer这样好用的组件,我的内心是这样的的...呀!这个框架有弹框,哈哈哈,好开心,不用自己写遮罩层,不用自己写弹框,好开森.. 当知道有layer之后.... ...
- nginx gzip filter模块分析
API:http://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/zlib-deflateinit2.html ...
- 获取当前<script>节点
/* get current JavaScript dom object. */ var all_js = document.getElementsByTagName("script&quo ...
- IOS苹果购买PHP服务器端验证(订阅购买和一次性购买通用)
// 正式环境验证地址 $ios_verify_url = 'https://buy.itunes.apple.com/verifyReceipt'; // 测试环境验证地址 $ios_sandbox ...
- Review PHP设计模式之——观测模式
观测模式: <?php class car implements SplSubject{ private $carName; //车的类型 private $carState=0; //车的状态 ...
- IO笔记
Java流操作有关的类或接口: Java流类图结构: 1.文件操作时,注意要使用File.separator来代替/分隔符实现跨平台 2.RandomAccessFile类并不是流体系中的一员,但常用 ...
- 《C和指针》 读书笔记 -- 第10章 结构和联合
1.聚合数据类型能够同时存储超过一个的单独数据,c提供了两种类型的聚合数据类型,数组和结构. 2.[1] struct SIMPLE { int a; }; struct SIMPLE x; [2] ...
- 程序使用嵌套的for循环找出2〜100中的素数
#import <Foundation/Foundation.h> int main () { /* local variable definition */ int i, j; ; i& ...
- C语言标记化结构初始化语法
C语言标记化结构初始化语法 (designated initializer),而且还是一个ISO标准. #include <stdio.h> #include <stdlib.h&g ...
- SRM 616 ColorfulCoins
题意:给定一个从小到大的货币面值,每一个面额都是其前面面额的倍数(倍数大于等于2),每一种货币面值对应一种颜色,目前不清楚面值与颜色的对应关系.要求用最少的查询次数来确定面额与颜色的对应关系.(一次查 ...