Kattis之旅——Chinese Remainder
Input
The first line of input consists of an integers T where 1≤T≤1000, the number of test cases. Then follow T lines, each containing four integers a, n, b, m satisfying 1≤n,m≤10e9, 0≤a<n, 0≤b<m. Also, you may assume gcd(n,m)=1.
Output
For each test case, output two integers x, K, where K=n*m and 0≤x<K, giving the solution x(mod K) to the equations x=a(mod n),x=b(mod m).
| Sample Input 1 | Sample Output 1 |
|---|---|
2 |
5 6 |
感谢Pursuit_大神的一波支援。
由 ( x ≡ a )%n 以及 (x≡ b)%m这两个同余方程。可以联立得出一个二元一次方程—— k0*m+k1*(-n) = a-b。
然后就是解这个二元一次方程,得出最优解。对n*m取余。
直接上扩展欧几里德就好。
//Asimple
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m, a, b; void ex_gcd( ll a , ll b , ll &g , ll &x , ll &y ) { if( b == ) {
x = ; y = ;
g = a ;
}
ex_gcd( b , a%b , g , y , x ) ;
y-= x*(a/b);
} void slove(){
ll x , y , g ;
ex_gcd( m , -n , g , x , y ) ;
x =( x*(a-b)/g %(-n /g ) - n/g )%(-n/g);
printf( "%lld %lld\n" , ((x * m + b)%(n*m)+ n*m )%(n*m) , n*m ) ;
} void input(){
int t ;
scanf( "%d" , &t ) ;
while( t-- ) {
scanf( "%lld%lld%lld%lld" , &a , &n , &b , &m ) ;
slove( ) ;
}
} int main(){
input();
return ;
}
Kattis之旅——Chinese Remainder的更多相关文章
- hdu 1788 Chinese remainder theorem again(最小公倍数)
Problem Description 我知道部分同学最近在看中国剩余定理,就这个定理本身,还是比较简单的: 假设m1,m2,-,mk两两互素,则下面同余方程组: x≡a1(mod m1) x≡a2( ...
- Chinese remainder theorem again(中国剩余定理)
C - Chinese remainder theorem again Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:% ...
- DHU 1788 Chinese remainder theorem again 中国剩余定理
Chinese remainder theorem again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 中国剩余定理(Chinese Remainder Theorem)
我理解的中国剩余定理的含义是:给定一个数除以一系列互素的数${p_1}, \cdots ,{p_n}$的余数,那么这个数除以这组素数之积($N = {p_1} \times \cdots \tim ...
- HDU 1788 Chinese remainder theorem again
题目链接 题意 : 中文题不详述. 思路 : 由N%Mi=(Mi-a)可得(N+a)%Mi=0;要取最小的N即找Mi的最小公倍数即可. #include <cstdio> #include ...
- Kattis之旅——Prime Reduction
A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...
- Kattis之旅——Fractional Lotion
Freddy practices various kinds of alternative medicine, such as homeopathy. This practice is based o ...
- Kattis之旅——Factovisors
The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * (n-1)! (n & ...
- Kattis之旅——Rational Arithmetic
Input The first line of input contains one integer, giving the number of operations to perform. Then ...
随机推荐
- python的static方法和class方法
class Caculator(object): name = "caculator" def __init__(self, x, y): self._x = x self._y ...
- 模仿以太坊 ERC20 规范的 Hyperledger Fabric 实现 Token 通证
合约: package main /* -------------------------------------------------- Author: netkiller <netkill ...
- pycharm tips
批量更改变量名,就在该变量名上shift+f6 ../data 两个点,就是上一级目录,一个点就是当前目录 unhashable type: 'list' 使用set进行去重 a = [1,2,2,3 ...
- java编写的Http协议的多线程下载器
断点下载器还在实现中...... //////////////////////////////////界面/////////////////////////////////////////// pac ...
- Python才排第8名!2018增速最快TOP 10编程语言盘点
在技术前沿的硅谷,开发者们不仅要学习多种热门的编程语言,还要时时盯着新的编程语言的崛起,生怕自己掉队. 作为世界最大开源软件社区,Github每年都会发布年度Octoverse报告,向大家展示年度最流 ...
- github pages搭建网站(三)
一.个人站点 访问 https://用户名.github.io 搭建步骤 (1)创建个人站点 ->新建仓库(注:仓库名必须是[用户名.github.io]) (2)在仓库下新建index.htm ...
- python使用cx_Oracle在Linux和Windows下的一点差异
1. 主要是线程方面的差异. Windows下,把cx_Oracle.connect(connectedId)得到的handle传给定时器线程,主线程和和定时器可以用同一个handle. 但Linux ...
- React对比Vue(05 生命周期的对比)
先来vue的吧,先上图,生命周期就好比一个人重出生到青少年再到青年再到中年在到老年到死亡的一个过程,不同的过程做不同的事情. 好了,上代码 beforeCreate :数据还没有挂载呢,只是一个空壳 ...
- 设置sqlplus不显示除查询结果外的信息
背景:客户提出一个需求,写SQL脚本的时候,内容是拼接的,如何将这个拼接SQL执行的结果取出来调用执行呢? 我想到的方案是先把结果取出来,存为一个中间文件,再调用该文件即可. 知识点:如何将sqlpl ...
- “0x00,0x08”两个十六进制字符串,转换为整形
int m_length=0;char buf[2]=={0x00,0x08};memcpy(&m_length,&buf[0],2); m_length=m_length<&l ...