当前位置:网站首页>AcWing 1298. Solution to Cao Chong's pig raising problem
AcWing 1298. Solution to Cao Chong's pig raising problem
2022-07-06 11:16:00 【Octopus loving monster】
AcWing 1298 . Cao Chong raises pigs Answer key
Title Description
Since Cao Chong got the elephant , Cao Cao began to think about letting his son do something , So he was sent to Zhongyuan pig farm to raise pigs , But Cao Chong was very unhappy , So in the work of careless , Once Cao Cao wanted to know the number of sows , So Cao Chong wanted to play with Cao Cao .
for instance , If there is 16 16 16 A sow , If it's built 3 3 3 A pigsty , be left over 1 1 1 There's no place for a pig to settle down ; If you build 5 5 5 A pigsty , But there is still 1 1 1 A pig has no place to go ; If you build 7 7 7 A pigsty , also 2 2 2 The head has no place to go .
As Mr. Cao's personal secretary, you should report the accurate pig number to Mr. Cao , What are you gonna do? ?
Input format
The first line contains an integer n n n, Indicates the number of times to establish a pigsty ;
Next n n n That's ok , Two integers per line a i , b i a_i,b_i ai,bi Indicates that the a i a_i ai A pigsty , Yes b i b_i bi There's no place for a pig .
You can assume that a i , a j a_i,a_j ai,aj Coprime
Output format
The output contains only one positive integer , That is, at least the number of pigs raised by Cao Chong
Data range
1 ≤ b ≤ 10 1\le b \le 10 1≤b≤10
1 ≤ b i ≤ a i ≤ 1100000 1\le b_i\le a_i\le 1100000 1≤bi≤ai≤1100000
all a i a_i ai The product of does not exceed 1 0 18 10^{18} 1018
Answer key
The test point of this question is Chinese remainder theorem
We describe the Chinese surplus in the following form
Here we give the method of calculating Chinese Remainder Theorem and prove
Calculate the product of all modules n n n
For the first i i i An equation :
a. Calculation m i = n n i m_i=\dfrac{n}{n_i} mi=nin
b. Calculation m i m_i mi In the mold n i n_i ni Inverse element in sense m i − 1 m_i^{-1} mi−1
c. Calculation c i = m i × m i − 1 c_i =m_i\times m_i^{-1} ci=mi×mi−1 ( incorrect n i n_i ni modulus )
The unique solution of the system of equations is : x = ∑ i = 1 k a i c i ( m o d n i ) x=\sum^k_{i=1}a_i~c_i (mod ~n_i) x=∑i=1kai ci(mod ni)
prove :
We can prove the result obtained by this method x x x For arbitrary i = 1 , 2 , … , k i=1,2, \dots,k i=1,2,…,k All meet x ≡ a i ( m o d n i ) x ~\equiv~ a_i~(mod~n_i) x ≡ ai (mod ni)
When i ≠ j i\neq j i=j when , Yes
m j ≡ 0 ( m o d n i ) m_j \equiv 0~(mod~ n_i) mj≡0 (mod ni)
For
c j ≡ m j ≡ 0 ( m o d n i ) c_j \equiv m_j \equiv 0 ~(mod ~n_i) cj≡mj≡0 (mod ni)
According to
c i = m i × m i − 1 ≡ 1 ( m o d n i ) c_i = m_i\times m_i^{-1}\equiv 1~(mod~n_i) ci=mi×mi−1≡1 (mod ni)
For x x x Yes
x ≡ ∑ j = 1 k a j c j ( m o d n i ) ≡ a i c i ( m o d n i ) ≡ a i × 1 ( m o d n i ) ≡ a i ( m o d n i ) x\equiv\displaystyle\sum_{j=1}^ka_j c_j ~~~~~~(mod ~n_i) \\ \equiv a_ic_i~~\qquad(mod n_i) \\ \equiv a_i\times1~~~~~~(mod n_i) \\ \equiv a_i~~~~~\qquad(mod n_i) x≡j=1∑kajcj (mod ni)≡aici (modni)≡ai×1 (modni)≡ai (modni)
Obtain evidence
Find the inverse element in the sense of module
Then the idea of this topic becomes very clear , Now the question is how Find the inverse element in the sense of module
Let's look at a formula
a a − 1 ≡ 1 ( m o d c ) aa^{-1} \equiv 1~(mod ~c) aa−1≡1 (mod c)
Deformation can be obtained
a a − 1 = k c + 1 aa^{-1} = kc+1 aa−1=kc+1
transposition
a a − 1 − k c = 1 aa^{-1} -kc =1 aa−1−kc=1
It is observed that it is shaped like Euclidean equation
a x + b y = c ax+by=c ax+by=c
Therefore, it can be solved by extended Euclidean
Complete code
import java.io.*;
import java.util.*;
class Main
{
static long sum=1;
static int n;
static int[] a=new int[10];
static int[] c=new int[10];
static long x;
static long y;
static long res=0;
public static long exgcd(long a,long b)
{
if(b==0)
{
x=1;
y=0;
return a;
}
long d=exgcd(b,a%b);
long tmp=x;
x=y;
y=tmp-(a/b)*x;
return d;
}
public static void main(String[] agrs)throws IOException
{
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(reader.readLine());
for(int i=0;i<n;i++)
{
String[] param=reader.readLine().split(" ");
c[i]=Integer.parseInt(param[0]);
a[i]=Integer.parseInt(param[1]);
sum*=c[i];
}
for(int i=0;i<n;i++)
{
long m=sum/c[i];
long re=exgcd(m,c[i]);
res=(res+a[i]*m*x%sum)%sum;
}
System.out.println((res%sum+sum)%sum);// Output positive numbers
}
}
边栏推荐
- Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
- 连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
- Attention apply personal understanding to images
- Database advanced learning notes -- SQL statement
- NPM an error NPM err code enoent NPM err syscall open
- [recommended by bloggers] C # generate a good-looking QR code (with source code)
- Deoldify project problem - omp:error 15:initializing libiomp5md dll,but found libiomp5md. dll already initialized.
- Record a problem of raspberry pie DNS resolution failure
- MySQL的一些随笔记录
- JDBC原理
猜你喜欢
自动机器学习框架介绍与使用(flaml、h2o)
QT creator specify editor settings
Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
neo4j安装教程
MySQL主從複制、讀寫分離
Install mysql5.5 and mysql8.0 under windows at the same time
图像识别问题 — pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your path
Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution
CSDN blog summary (I) -- a simple first edition implementation
Solution: log4j:warn please initialize the log4j system properly
随机推荐
Ansible practical Series II_ Getting started with Playbook
There are three iPhone se 2022 models in the Eurasian Economic Commission database
Did you forget to register or load this tag
学习问题1:127.0.0.1拒绝了我们的访问
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
CSDN question and answer module Title Recommendation task (II) -- effect optimization
[C language foundation] 04 judgment and circulation
QT creator test
Detailed reading of stereo r-cnn paper -- Experiment: detailed explanation and result analysis
Swagger, Yapi interface management service_ SE
SSM整合笔记通俗易懂版
Django running error: error loading mysqldb module solution
AcWing 1294.樱花 题解
Invalid default value for 'create appears when importing SQL_ Time 'error reporting solution
Install mysql5.5 and mysql8.0 under windows at the same time
In the era of DFI dividends, can TGP become a new benchmark for future DFI?
【博主推荐】asp.net WebService 后台数据API JSON(附源码)
自动机器学习框架介绍与使用(flaml、h2o)
[download app for free]ineukernel OCR image data recognition and acquisition principle and product application
QT creator custom build process