exCRT & 骆克强乘法
exCRT & 骆克强乘法
只是丢两个板子啦。
exCRT的做法就是每次拿两个方程合并成一个,合并的过程推下式子就是个 exgcd。具体可以在 zjk 的 ptt 里面找到。
先放个 $ O(1) $ 慢速乘
ll mul( ll a , ll b , ll p ) { a %= p , b %= p; return ( (a * b - (ll)( (ll)( (long double)a / p * b + 0.5 ) * p )) % p + p ) % p; }
然后一个 exgcd
void exgcd( ll a , ll b , ll& d , ll& x , ll& y ) {
if( !b ) { d = a , x = 1 , y = 0; return; }
else exgcd( b , a % b , d , y , x ) , y -= x * ( a / b );
}
最后是 excrt
Luogu 板子题和 PTT 上的 ab 居然是反着的。。。毒瘤
#include "iostream"
#include "algorithm"
#include "cstring"
#include "cstdio"
using namespace std;
#define MAXN 100006
typedef long long ll;
ll mul( ll a , ll b , ll p ) { a %= p , b %= p; return ( (a * b - (ll)( (ll)( (long double)a / p * b + 0.5 ) * p )) % p + p ) % p; }
int n;
ll A[MAXN] , B[MAXN];
ll gcd( int a , int b ) { return b ? a : gcd( b , a % b ); }
void exgcd( ll a , ll b , ll& d , ll& x , ll& y ) {
if( !b ) { d = a , x = 1 , y = 0; return; }
else exgcd( b , a % b , d , y , x ) , y -= x * ( a / b );
}
bool crt( ll& a1 , ll a2 , ll& b1 , ll b2 ) {
ll d = a2 - a1;
ll g , k1 , k2;
exgcd( b1 , b2 , g , k1 , k2 );
if( d % g ) return 0;
else {
ll r = b2 / g;
k1 = mul( k1 , d / g , r );
a1 = k1 * b1 + a1;
b1 = ( b1 * r );
return 1;
}
}
ll excrt( ) {
ll a1 = A[0] , b1 = B[0] , a2 , b2;
for( int i = 1 ; i < n ; ++ i ) {
a2 = A[i] , b2 = B[i];
if( !crt( a1 , a2 , b1 , b2 ) ) return -1;
}
return a1;
}
int main() {
cin >> n;
for( int i = 0 ; i < n ; ++ i ) scanf("%lld%lld",&B[i],&A[i]);
cout << excrt( ) << endl;
}
exCRT & 骆克强乘法的更多相关文章
- Data-independent acquisition mass spectrometry in metaproteomics of gut microbiota - implementation and computational analysis DIA技术在肠道宏蛋白质组研究中的方法实现和数据分析 (解读人:闫克强)
文献名:Data-independent acquisition mass spectrometry in metaproteomics of gut microbiota - implementat ...
- Fast and accurate bacterial species identification in urine specimens using LC-MS/MS mass spectrometry and machine learning (解读人:闫克强)
文献名:Fast and accurate bacterial species identification in urine specimens using LC-MS/MS mass spectr ...
- 解读人:闫克强,Metabolic and gut microbial characterization of obesity-prone mice under high-fat diet(高脂饮食下易胖倾向小鼠的代谢和肠道微生物菌群特征分析)
单位: 上海中医药大学 蚌埠医学院 上海交通大学附属第六人民医院 夏威夷大学癌症中心 第二军医大学 技术:非靶向代谢组学,16S rRNA测序技术 一. 概述: 本研究对小鼠进行高脂饮食,根据体重增长 ...
- 洛谷4245:【模板】任意模数NTT——题解
https://www.luogu.org/problemnew/show/P4245 给两个多项式,求其乘积,每个系数对p取模. 参考: 代码与部分理解参考https://www.luogu.org ...
- 李洪强iOS经典面试题上
李洪强iOS经典面试题上 1. 风格纠错题 修改完的代码: 修改方法有很多种,现给出一种做示例: // .h文件 // http://weibo.com/luohanchenyilong/ / ...
- 【学习笔记】OI模板整理
CSP2019前夕整理一下模板,顺便供之后使用 0. 非算法内容 0.1. 读入优化 描述: 使用getchar()实现的读入优化. 代码: inline int read() { int x=0; ...
- [转] ACM中国国家集训队论文集目录(1999-2009)
国家集训队1999论文集 陈宏:<数据结构的选择与算法效率——从IOI98试题PICTURE谈起>来煜坤:<把握本质,灵活运用——动态规划的深入探讨>齐鑫:<搜索方法中的 ...
- NOI 国家集训队论文集
鉴于大家都在找这些神牛的论文.我就转载了这篇论文合集 国家集训队论文分类 组合数学 计数与统计 2001 - 符文杰:<Pólya原理及其应用> 2003 - 许智磊:<浅谈补集转化 ...
- ACM/IOI 历年国家集训队论文集和论文算法分类整理
国家集训队1999论文集 陈宏:<数据结构的选择与算法效率--从IOI98试题PICTURE谈起> 来煜坤:<把握本质,灵活运用--动态规划的深入探讨> 齐鑫:<搜索方法 ...
随机推荐
- clock时钟
①时钟的偏移(skew):时钟分支信号在到达寄存器的时钟端口过程中,都存在有线网等延时,由于延时,到达寄存器时钟端口的时钟信号存在有相位差,也就是不能保证每一个沿都对齐,这种差异称为时钟偏移(cloc ...
- repartition导致的广播失败,关于错误Failed to get broadcast_544_piece0 of broadcast_544
今天一个生产环境任务出现了性能问题,,经过仔细检查发现是在一个join操作时,原设定广播右表数据广播失败,导致后续步骤进行缓慢,,报错信息 java.io.IOException: org.apach ...
- Less-(26~28) preg_replace3
Less-26: 核心语句: 各种回显均存在. 本题相比Less-25,多屏蔽了很多符号: 首先是各种注释符 --+,#,/**/ . /[]/表示字符集合:任何出现在里面的字符均会被替换. 屏蔽 ...
- 【Spring】Spring重要类层次图
- [软工顶级理解组] Beta阶段团队贡献分评分
贡献分评分依据 下述表格适用于前端.后端.爬虫开发者的评分,在此基础上进行增减. 类别 程度 加减分 准时性 提前完成 +0 按时完成 +0 延后完成,迟交时间一天内或未延误进度 -2 延后完成,迟交 ...
- netty传输java bean对象
在上一篇博客(netty入门实现简单的echo程序)中,我们知道了如何使用netty发送一个简单的消息,但是这远远是不够的.在这篇博客中,我们来使用netty发送一个java bean对象的消息,但是 ...
- sort方法和自定义比较器的写法
摘要 在做一些算法题时常常会需要对数组.自定义对象.集合进行排序. 在java中对数组排序提供了Arrays.sort()方法,对集合排序提供Collections.sort()方法.对自定义对象排序 ...
- 到底能不能用 join
互联网上一直流传着各大公司的 MySQL 军规,其中关于 join 的描述,有些公司不推荐使用 join,而有些公司则规定有条件的使用 join, 它们都是教条式的规定,也没有详细说其中的原因,这就很 ...
- RecyclerView使用详解
使用RecyclerView要引用对应的jar包,但最新版的项目中,不用引用也可以使用. implementation 'com.android.support:recyclerview-v7:27. ...
- OpenWrt编译报错:Package airfly_receiver is missing dependencies for the following libraries
今天在编译一个OpenWrt测试用例的时候出现报错 Package airfly_receiver is missing dependencies for the following librarie ...