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. solr高亮及摘要

    修改了原文的一点内容:原文地址为:http://www.cnblogs.com/rainbowzc/p/3680343.html 高亮显示 两种方法: 1.在程序里通过设置query返回高亮信息 pu ...

  2. CSS的color属性并非只能用于文本显示

    虽然CSS并不是一种很复杂的技术,但就算你是一个使用CSS多年的高手,仍然会有很多CSS用法/属性/属性值你从来没使用过,甚至从来没听说过. 对于CSS的color属性,相信所有Web开发人员都使用过 ...

  3. 2019.8.13 NOIP模拟测试19 反思总结

    最早写博客的一次∑ 听说等会儿还要考试[真就两天三考啊],教练催我们写博客… 大约是出题最友好的一次[虽然我还是炸了],并且数据也非常水…忽视第三题的锅的话的确可以这么说.但是T3数据出锅就是你的错了 ...

  4. day18 14.连接池介绍

    数据源在软件编程行业有两种概念:一种数据源指的是存储数据的源头(数据库啊文件啊叫数据源),一种指的是连接池(连接池的英文单词叫做DataSource,直译就是数据源).数据源可以指数据库,也可以指连接 ...

  5. SPFA(Bellman-Ford队列优化)

    原理:队列+松弛操作 将源点加入队尾,每一步读取队头顶点u,并将队头顶点u出队(记得消除标记):将与点u相连的所有点v进行松弛操作,如果能更新距离(即令d[v]变小),那么就更新,另外,如果点v没有在 ...

  6. Leetcode669.Trim a Binary Search Tree修建二叉树

    给定一个二叉搜索树,同时给定最小边界L 和最大边界 R.通过修剪二叉搜索树,使得所有节点的值在[L, R]中 (R>=L) .你可能需要改变树的根节点,所以结果应当返回修剪好的二叉搜索树的新的根 ...

  7. Sorry, the page you are looking for is currently unavailable. Please try again later. Nginx

    访问html可以正常访问,但是访问PHP则错误,原因: nginx不能正常通过FastCGI结果访问PHP 查看php-fpm是否正常运行: 果然没有,重启php-fpm: /etc/init.d/p ...

  8. Vue2.0史上最全入坑教程(下)—— 实战案例

    书接上文 前言:经过前两节的学习,我们已经可以创建一个vue工程了.下面我们将一起来学习制作一个简单的实战案例. 说明:默认我们已经用vue-cli(vue脚手架或称前端自动化构建工具)创建好项目了 ...

  9. Linux常用命令操作详解

    https://mp.weixin.qq.com/s/IR4yy7Q0mOA_XV16R21CdQ 一:Linux下tomcat服务的启动.关闭与错误跟踪 使用PuTTy远程连接到服务器以后,通常通过 ...

  10. MaxCompute 助力衣二三构建智能化运营工具

    摘要:本文由衣二三CTO程异丁为大家讲解了如何基于MaxCompute构建智能化运营工具.衣二三作为亚洲最大的共享时装平台,MaxCompute是如何帮助它解决数据提取速度慢.数据口径差异等问题呢?程 ...