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位整型,不过中间过程还是超了。

Problem - 3579

 #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 ;
}

Problem - 1573

 #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("");
}
}

Problem - 1930

 #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)的更多相关文章

  1. Bell(hdu4767+矩阵+中国剩余定理+bell数+Stirling数+欧几里德)

    Bell Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  2. 2013长春网赛1009 hdu 4767 Bell(矩阵快速幂+中国剩余定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4767 题意:求集合{1, 2, 3, ..., n}有多少种划分情况bell[n],最后结果bell[ ...

  3. POJ 1006 - Biorhythms (中国剩余定理)

    B - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Subm ...

  4. POJ 1006 Biorhythms (中国剩余定理)

    在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...

  5. POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd

    http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...

  6. poj 1006 Biorhythms (中国剩余定理模板)

    http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...

  7. POJ 2891 中国剩余定理(不互素)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 17877 ...

  8. poj 1006:Biorhythms(水题,经典题,中国剩余定理)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 Des ...

  9. POJ 1006 Biorhythms(中国剩余定理)

    题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...

随机推荐

  1. 查漏补缺&#183;补丁计划

    趁着神志清醒赶紧写一下. 多次考试暴露出各种问题.新的知识点先不去搞了,最近多做一些不擅长的类型的题查漏补缺一下吧. 唔,首先是比较考验思维的类型,我智商太低又刷题少不会什么套路,只能最近赶紧赶一下进 ...

  2. 2019-8-31-dotnet-Framework-源代码-类库的意思

    title author date CreateTime categories dotnet Framework 源代码 类库的意思 lindexi 2019-08-31 16:55:58 +0800 ...

  3. 手机号测吉凶python代码

    根据数理数来测电话后四位吉凶: 数理数 解释批注 0点特殊.......大吉 1大展鸿图.可获成功吉 2一盛一衰.劳而无功凶 3蒸蒸日上.百事顺遂吉 4坎坷前途.苦难折磨凶 5生意欣荣.名利双收吉 6 ...

  4. 悠星网络基于阿里云分析型数据库PostgreSQL版的数据实践

    说到“大数据”,当下这个词很火,各行各业涉及到数据的,目前都在提大数据,提数据仓库,数据挖掘或者机器学习,但同时另外一个热门的名词也很火,那就是“云”.越来越多的企业都在搭建属于自己的云平台,也有一些 ...

  5. bzoj2049: [Sdoi2008]Cave 洞穴探测

    bzoj2049: [Sdoi2008]Cave 洞穴探测 给n个点,每次连接两个点或切断一条边,保证是树结构,多次询问两个点是否联通 Lct裸题 //Achen #include<algori ...

  6. HTML input type=file文件选择表单的汇总(二)

    1. 原生file input大小.按钮文字等UI自定义 元素input的原生样式,不是太好看: 有一种方法是这样的:让file类型的元素透明度0,覆盖在我们好看的按钮上.然后我们去点击好看的按钮,实 ...

  7. svn利用钩子脚本功能实现代码同步到web目录

    一.hook简单介绍 为了方便管理员控制提交的过程 ,Subversion提供了hook机制.当特定的 事件发生时,相应的 hook会被调用,hook其实就相当于特定事件的处理函数.每个hook会得到 ...

  8. golang context用法详解

    背景 在go服务器中,对于每个请求的request都是在单独的goroutine中进行的,处理一个request也可能设计多个goroutine之间的交互, 使用context可以使开发者方便的在这些 ...

  9. python3.7 安装gensim使用word2Vec库

    应用的文章(个人试验过,完全正确):https://radimrehurek.com/gensim/index.html#install

  10. C#中时间差的计算

    /// <summary> /// 已重载.计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值. /// </summary> /// <param name=&q ...