histcat

histcat

The method of doubling large numbers to find (expand) the Chinese Remainder Theorem

Brute Force#

Brute force merges each number with another number, calculating how much needs to be added to meet the requirements.

The time complexity looks confusing, but in reality, template problems run quite fast.

However, there is a risk of getting stuck; according to zhx, the smaller n is, the easier it is to get stuck, but at most it will only get stuck at one point (

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;

void Merge(int &b1, int &a1, int b2, int a2)
{
	if(a1 < a2) swap(b1, b2), swap(a1, a2);
	while(b1 % a2 != b2)
	{
		b1 += a1;
	}
	a1 = a1 / __gcd(a1, a2) * a2;
}
//a is the modulus, b is the remainder 
signed main()
{
	ios::sync_with_stdio(0), cin.tie(0);

	int bstar = 0, astar = 1;
	int b, a;
	cin >> n;
	for(int i = 1;i <= n;i++)
	{
		cin >> a >> b;
		b%= a;
		Merge(bstar, astar, b, a);
	}

	printf("%lld", bstar);
	return 0;
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.