一些关于中国剩余定理的数论题(POJ 2891/HDU 3579/HDU 1573/HDU 1930)
2891 -- Strange Way to Express Integers
import java.math.BigInteger;
import java.util.Scanner; public class Main {
static final BigInteger ZERO = new BigInteger("0");
static final BigInteger ONE = new BigInteger("1");
static BigInteger gcd(BigInteger a, BigInteger b) {
if (b.equals(ZERO)) return a;
else return gcd(b, a.mod(b));
}
static BigInteger lcm(BigInteger a, BigInteger b) {
return a.divide(gcd(a, b)).multiply(b);
}
static void gcd(BigInteger a, BigInteger b, BigInteger p[]) {
if (b.equals(ZERO)) {
p[0] = a;
p[1] = new BigInteger("1");
p[2] = new BigInteger("0");
} else {
gcd(b, a.mod(b), p);
BigInteger tmp = p[1];
p[1] = p[2];
p[2] = tmp;
p[2] = p[2].subtract(a.divide(b).multiply(p[1]));
}
}
static BigInteger[] A = new BigInteger[11111];
static BigInteger[] R = new BigInteger[11111];
static BigInteger work(int n) {
BigInteger[] p = new BigInteger[3];
BigInteger a = A[0], r = R[0];
for (int i = 1; i < n; i++) {
BigInteger dr = R[i].subtract(r);
gcd(a, A[i], p);
if (dr.mod(p[0]).equals(ZERO) == false) return new BigInteger("-1");
BigInteger tmp = a.multiply(p[1]);
a = lcm(a, A[i]);
tmp = tmp.mod(a).add(a).mod(a);
tmp = tmp.multiply(dr).divide(p[0]).add(r);
r = tmp.mod(a).add(a).mod(a);
}
return r;
}
public static void main(String[] args) {
int n;
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
n = in.nextInt();
String tmp;
for (int i = 0; i < n; i++) {
tmp = in.next();
A[i] = new BigInteger(tmp);
tmp = in.next();
R[i] = new BigInteger(tmp);
}
System.out.println(work(n).toString());
}
}
}
因为懒得写大数,所以直接用java的大数类来做。用这个是因为,虽然输入输出不会超过64位整型,不过中间过程还是超了。
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring> using namespace std; typedef long long LL; const int N = ;
LL A[N], R[N];
inline LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a;}
inline LL lcm(LL a, LL b) { return a / gcd(a, b) * b;}
void gcd(LL a, LL b, LL &d, LL &x, LL &y) {
if (b) { gcd(b, a % b, d, y, x); y -= a / b * x;}
else { d = a, x = , y = ;}
} LL work(int n) {
LL x, y, d, a = A[], r = R[];
for (int i = ; i < n; i++) {
LL dr = R[i] - r;
gcd(a, A[i], d, x, y);
if (dr % d) return -;
//cout << x << ' ' << y << ' ' << d << endl;
LL tmp = a * x;
a = lcm(a, A[i]);
tmp = (tmp % a + a) % a * dr / d + r;
r = (tmp % a + a) % a;
//cout << a << ' ' << r << ' ' << tmp << endl;
}
return r ? r : a;
} int main() {
int n, T, cas = ;
cin >> T;
while (T-- && cin >> n) {
for (int i = ; i < n; i++) cin >> A[i];
for (int i = ; i < n; i++) cin >> R[i];
cout << "Case " << cas++ << ": " << work(n) << endl;
}
return ;
}
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring> using namespace std; typedef long long LL;
inline LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a;}
inline LL lcm(LL a, LL b) { return a / gcd(a, b) * b;}
void gcd(LL a, LL b, LL &d, LL &x, LL &y) {
if (b) { gcd(b, a % b, d, y, x); y -= a / b * x;}
else d = a, x = , y = ;
} LL A[], R[], LCM;
LL work(int n) {
LL d, x, y, a = A[], r = R[];
for (int i = ; i < n; i++) {
LL dr = R[i] - r;
gcd(a, A[i], d, x, y);
//cout << d << ' ' << x << ' ' << y << endl;
if (dr % d) return -;
LL tmp = a * x * dr / d + r;
a = lcm(a, A[i]);
r = (tmp % a + a) % a;
//cout << a << '~' << r << endl;
}
LCM = a;
return r ? r : a;
} int main() {
//freopen("in", "r", stdin);
int T, n;
LL r;
cin >> T;
while (cin >> r >> n) {
for (int i = ; i < n; i++) cin >> A[i];
for (int i = ; i < n; i++) cin >> R[i];
LL ans = work(n);
//cout << ans << ' ' << LCM << endl;
if (~ans) cout << max(0ll, (r - ans + LCM) / LCM) << endl;
else puts("");
}
}
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring> using namespace std; typedef long long LL;
inline LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a;}
inline LL lcm(LL a, LL b) { return a / gcd(a, b) * b;}
void gcd(LL a, LL b, LL &d, LL &x, LL &y) {
if (b) { gcd(b, a % b, d, y, x); y -= a / b * x;}
else d = a, x = , y = ;
} LL A[], R[]; bool check(LL a) {
for (int i = ; i < ; i++) {
if (a % > || a % <= ) return false;
a /= ;
}
return true;
} LL cal() {
LL d, x, y, a = A[], r = R[];
for (int i = ; i < ; i++) {
LL dr = R[i] - r;
gcd(a, A[i], d, x, y);
if (dr % d) {
puts("WTF??");
while () ;
}
LL tmp = a * x;
//a = lcm(a, A[i]);
a *= A[i];
tmp %= a;
tmp *= dr / d;
tmp += r;
r = (tmp % a + a) % a;
//cout << a << '~' << r << endl;
}
while (!check(r)) r += a;
//cout << r << ' ' << a << endl;
return r;
} LL cal(LL x) {
for (int i = ; i < ; i++) R[i] = x % , x /= ;
//for (int i = 0; i < 4; i++) cout << A[i] << ' ' << R[i] << endl;
return cal();
} const LL ep[] = { , , };
char out[]; int main() {
//freopen("in", "r", stdin);
int T, n, p;
LL tmp, x;
cin >> T;
while (T-- && cin >> n) {
p = ;
for (int i = ; i < ; i++) cin >> A[i];
reverse(A, A + );
for (int i = ; i < n; i++) {
cin >> tmp;
tmp = cal(tmp);
for (int i = ; i < ; i++) {
x = tmp / ep[i] % ;
out[p++] = x == ? ' ' : (x - + 'A');
}
}
out[p] = ;
while (p > && out[p - ] == ' ') out[--p] = ;
puts(out);
}
return ;
}
——written by Lyon
一些关于中国剩余定理的数论题(POJ 2891/HDU 3579/HDU 1573/HDU 1930)的更多相关文章
- Bell(hdu4767+矩阵+中国剩余定理+bell数+Stirling数+欧几里德)
Bell Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 2013长春网赛1009 hdu 4767 Bell(矩阵快速幂+中国剩余定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4767 题意:求集合{1, 2, 3, ..., n}有多少种划分情况bell[n],最后结果bell[ ...
- POJ 1006 - Biorhythms (中国剩余定理)
B - Biorhythms Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Subm ...
- POJ 1006 Biorhythms (中国剩余定理)
在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd
http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...
- poj 1006 Biorhythms (中国剩余定理模板)
http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...
- POJ 2891 中国剩余定理(不互素)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 17877 ...
- poj 1006:Biorhythms(水题,经典题,中国剩余定理)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110991 Accepted: 34541 Des ...
- POJ 1006 Biorhythms(中国剩余定理)
题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...
随机推荐
- XML解析器之JAXP与DOM4J
XML是一种数据格式,那么需要对XML文件进行操作就需要用到XML解析器---------针对dom方式和sax方式提供了不同的解析技术-----需要不同的XML解析器 dom方式:会把文档中所有元素 ...
- 【模板】 递归线段树 [2017年五月计划 清北学堂51精英班Day4]
P3372 [模板]线段树 1 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别 ...
- Leetcode46. Permutations全排列
给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1 ...
- rescue grub解决方案
症状: 开机显示:GRUB loading error:unknow filesystem grub rescue> 原因: 已经发现下面几种操作会导致这种问题: 1,想删除ubuntu,于是直 ...
- 手把手教你如何玩转消息中间件(ActiveMQ) https://blog.csdn.net/cs_hnu_scw/article/details/81040834
#情景引入小白:起床起床起床起床....快起床~我:怎么了又,大惊小怪,吓到我了.小白:我有事有事想找你,十万火急呢~~我:你能有什么事?反正我不信..那你说说看~~小白:就是我有两个小表弟,叫大白和 ...
- Android——系统权限
Android是一个特权分隔的操作系统,每一个应用程序运行在不同的系统身份中(Linux的user ID和group ID).系统部分和不同的身份被隔离开来.因此,Linux隔离了应用程序(与其它程序 ...
- python实现统计字符类型
#字符串处理统计类型def my_func(*str): list_num = [] list_big = [] list_small = [] list_qt = [] str = input('请 ...
- 为什么要用Spring的依赖注入
最近写了一些Spring项目,用到了依赖注入,但是不知道为甚么要用,后来在知乎上看了一些大家的回答,觉得很精辟,遂简单总结一下. 主要是实现类之间的解耦,假如A类依赖B类,在实例化A类的话也要new一 ...
- SurfaceFlinger与Surface概述
基本原理: SF一个Client对应一个app中的SurfaceComposerClient, 分别是Binder的n端和b端,主要用来CreateSurface 一个app中有多个Activity, ...
- JMeter 参数意义
样本数目:运行时得到的取样器响应结果个数 最新样本:最近一个取样器结果的响应时间 平均:所有取样器结果的响应时间平均值 偏离:所有取样器结果的响应时间标准差 吞吐量:每分钟响应的取样器结果个数 中值: ...