当前位置:网站首页>Strange way of expressing integers (expanding Chinese remainder theorem)
Strange way of expressing integers (expanding Chinese remainder theorem)
2022-07-03 20:25:00 【MangataTS】
Topic link
https://www.acwing.com/problem/content/description/206/
Ideas
For this question , We have to solve n An equation that satisfies the condition , We first consider the case of satisfying two equations :
X ≡ m 1 m o d a 1 X≡m_1 \ mod \ a_1 X≡m1 mod a1
X ≡ m 2 m o d a 2 X≡m_2 \ mod \ a_2 X≡m2 mod a2
This equation can also be written in the following format , Assume an arbitrary integer k i k_i ki
X ≡ k i a i + m i m o d a i X≡k_i a_i + m_i \ mod \ a_i X≡kiai+mi mod ai
So we get :
k 1 a 1 + m 1 = k 2 a 2 + m 2 k_1a_1 + m_1 = k_2 a_2 + m_2 k1a1+m1=k2a2+m2
namely k 1 a 1 − k 2 a 2 = m 2 − m 1 k_1a_1-k_2a_2=m_2-m_1 k1a1−k2a2=m2−m1
set up d by g c d ( a 1 , a 2 ) gcd(a_1,a_2) gcd(a1,a2)
If d ∣ m 2 − m 1 d |m_2-m_1 d∣m2−m1 Then the equation has a solution , We can solve by extending Euclidean algorithm k1,k2 A solution of
set up p For any integer , And because the solution is not unique , So we can put k1 The general solution of is written in the form :
k 1 = k 1 + p a 2 d k1 = k1 + p\frac{a_2}{d} k1=k1+pda2
Empathy k2 The general solution of can be written as :
k 2 = k 2 + p a 1 d k2 = k2 + p\frac{a_1}{d} k2=k2+pda1
Then we will k1 The general solution formula can be substituted into the original equation :
X ≡ ( ( k 1 + p a 2 d ) a 1 + m 1 ) m o d a 1 X≡((k_1 + p\frac{a_2}{d})a_1 + m1) \ mod \ a_1 X≡((k1+pda2)a1+m1) mod a1
X ≡ ( P a 1 ∗ a 2 d + a 1 k 1 + m 1 ) m o d a 1 X≡ (P\frac{a_1*a_2}{d} + a_1k_1 + m_1) \ mod \ a_1 X≡(Pda1∗a2+a1k1+m1) mod a1
X ≡ ( P × l g m ( a 1 , a 2 ) + ( a 1 k 1 + m 1 ) ) m o d a 1 X≡(P\times lgm(a_1,a_2) + (a_1k_1 + m_1)) \ mod \ a_1 X≡(P×lgm(a1,a2)+(a1k1+m1)) mod a1
Then at this time, we were surprised to find that this is not the format of the formula we began to transform , So we found that we can merge the two equations through this operation , Then we will n The equations are merged into one equation , In the end X ≡ P X 0 + m 1 m o d X 0 X≡P X_0 + m_1 \ mod \ X_0 X≡PX0+m1 mod X0 So direct output m 1 m_1 m1 Just fine
Be careful :
1. Because the data is very limited, we should ensure that k1 Is a minimum positive number solution
2. final m 1 m_1 m1 It could be negative , It's best to take the mold first, then add the modulus, and then take the mold , Guarantee non negative
Code
#include<bits/stdc++.h>
using namespace std;
//---------------- Custom part ----------------
#define ll long long
#define mod 1000000007
#define endl "\n"
#define PII pair<int,int>
#define INF 0x3f3f3f3f
int dx[4] = {
-1, 0, 1, 0}, dy[4] = {
0, 1, 0, -1};
ll ksm(ll a,ll b) {
ll ans = 1;
for(;b;b>>=1LL) {
if(b & 1) ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
ll lowbit(ll x){
return -x & x;}
const int N = 2e6+10;
//---------------- Custom part ----------------
int t,n,m,q,a[N];
ll exgcd(ll a,ll b,ll &x,ll &y){
if(!b){
x = 1,y = 0;
return a;
}
ll d = exgcd(b,a%b,y,x);
y -= a/b * x;
return d;
}
void slove(){
cin>>n;
ll a1,m1;
cin>>a1>>m1;
bool fg = true;
for(int i = 1;i < n; ++i) {
ll a2,m2;
cin>>a2>>m2;
ll k1,k2;
ll d = exgcd(a1,a2,k1,k2);
if((m2 - m1) % d){
fg = false;
break;
}
k1 *= (m2-m1)/d;// Double
ll t = abs(a2 / d);
k1 = (k1 % t + t) % t;// Become the smallest positive integer solution, otherwise it will overflow
m1 = a1 * k1 + m1;
a1 = (a1 / d * a2);
}
if(fg){
cout<< (m1 % a1 + a1) % a1 <<endl;
}
else{
cout<< "-1"<<endl;
}
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
t = 1;
while(t--){
slove();
}
return 0;
}
边栏推荐
- 2166. Design bit set
- 6. Data agent object Defineproperty method
- Deep search DFS + wide search BFS + traversal of trees and graphs + topological sequence (template article acwing)
- 7. Data broker presentation
- Phpexcel import export
- Instructions for common methods of regular expressions
- Today's work summary and plan: February 14, 2022
- Pytorch sets the weight and bias of the model to zero
- AI enhanced safety monitoring project [with detailed code]
- [effective Objective-C] - block and grand central distribution
猜你喜欢
2022 Xinjiang latest construction eight members (standard members) simulated examination questions and answers
Geek Daily: the system of monitoring employees' turnover intention has been deeply convinced off the shelves; The meta universe app of wechat and QQ was actively removed from the shelves; IntelliJ pla
Machine learning support vector machine SVM
Change deepin to Alibaba image source
Q&A:Transformer, Bert, ELMO, GPT, VIT
Battle drag method 1: moderately optimistic, build self-confidence (1)
Derivation of decision tree theory
Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro
Sightseeing - statistics of the number of shortest paths + state transfer + secondary small paths
Virtual machine installation deepin system
随机推荐
2022 Xinjiang latest construction eight members (standard members) simulated examination questions and answers
In 2021, the global general crop protection revenue was about $52750 million, and it is expected to reach $64730 million in 2028
Blue Bridge Cup: the fourth preliminary - "simulated intelligent irrigation system"
HCIA-USG Security Policy
Microsoft: the 12th generation core processor needs to be upgraded to win11 to give full play to its maximum performance
Global and Chinese market of high temperature Silver sintering paste 2022-2028: Research Report on technology, participants, trends, market size and share
4. Data binding
MySQL dump - exclude some table data - MySQL dump - exclude some table data
Promethus
JMeter connection database
Implementation of stack
JMeter plug-in installation
thrift go
Cap and base theory
Point cloud data denoising
你真的知道自己多大了吗?
Introduction to golang garbage collection
Parental delegation mechanism
[effective Objective-C] - block and grand central distribution
Professional interpretation | how to become an SQL developer