【ZOJ】3609 Modular Inverse
2016-02-29 12:41
316 查看
1. 题目描述
求乘法逆元。
2. 基本思路
利用扩展gcd求逆元,模板题目。
3. 代码
4. 代码生成器
求乘法逆元。
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 = 1; y = 0; } 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 != 1) return -1; return x%n==0 ? 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 == -1) { puts ("Not Exist"); } else { printf("%lld\n", ans); } } #ifndef ONLINE_JUDGE printf("time = %d.\n", (int)clock()); #endif return 0; }
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)
相关文章推荐
- 鸡兔同笼代码
- magento log问题
- 动态规划(DP),递推,最大子段和,POJ(2479,2593)
- OCR/Vote disk 维护操作: (添加/删除/替换/移动) (文档 ID 1674859.1) 转到底部转到底部
- js中单引号和双引号的区别
- [自定义组件之四]利用Quartz2D实现圆形图片显示
- 岁月无痕人有迹,时光无影人有踪
- Response.AddHeader实现下载_解决下载界面中文文件名乱码问题
- Item (Mage_Catalog_Model_Product) with the same id ”xxx“ already exist
- JDK5新特性之泛型
- 传入输入流即可将流中的数据转成String
- Android-性能优化、UI优化、代码优化(1)
- 【转】史上最全的CSS hack方式一览
- 当存放表决盘的磁盘组发生磁盘丢失时,启动CRS的步骤 (文档 ID 2055530.1)
- 个人介绍和GitHub
- JSON 之GSON 解析
- 23种设计模式
- 设计模式大全
- mysql优化之thread_cache_size
- 理解js中this的指向