COPRIMEX - Editorial

PROBLEM LINK:

Practice
Contest: Division 2
Contest: Division 3

Author: Hriday G
Tester: Rahul Dugar
Editorialist: Aman Dwivedi

DIFFICULTY:

Simple

PREREQUISITES:

Observation

PROBLEM:

You’re given integers L, L+1,...R denoted by [L,R]. You need to find any integer X which is at most 2*10^9 and does not share any common factors greater than 1 with any integer in [L, R].

HINT:

Output any prime number which doesn’t divide any number in the given range [L, R] endpoints inclusive.

EXPLANATION:

There are several ways to do this problem and hence that’s why multiple answers are possible too. One way is to output a prime number that is greater than R let us say it X.

Since X is a prime number, that means the only factor of X that is greater than 1 is X itself. As X is greater than R, it won’t share any common factors greater than 1 with the given range [L, R]. Since a number can’t have factors greater than itself.

Now look at the maximum value of R i.e 10^6. And we are allowed to output a number X which can be as large as 2*10^9. Hence for every test case, we can output a prime number that is greater than 10^6.

What’re your favorite prime numbers greater than 10^6? Are they 1000000007 or 998244353 :stuck_out_tongue:

We can find a prime number greater than 10^6 by using Sieve of Eratosthenes and then output this prime number for all the test cases.

TIME COMPLEXITY:

O(1) per test case.

SOLUTIONS:

Setter
#include <iostream>
using namespace std;

int main() {
    int Q;
    for(cin >> Q; Q--; ) {
        int i, l, r; cin >> l >> r;
        while(++r, true) {
            for(i = 2; i*i <= r; i++)
                if(r % i == 0) break;
            if(i * i > r) break;
        } cout << r << '\n';
    }
} // ~W 
Tester
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#ifndef rd
#define trace(...)
#define endl '\n'
#endif
#define pb push_back
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double f80;
#define double long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sz(x) ((long long)x.size())
#define fr(a,b,c) for(int a=b; a<=c; a++)
#define rep(a,b,c) for(int a=b; a<c; a++)
#define trav(a,x) for(auto &a:x)
#define all(con) con.begin(),con.end()
const ll infl=0x3f3f3f3f3f3f3f3fLL;
const int infi=0x3f3f3f3f;
//const int mod=998244353;
const int mod=1000000007;
typedef vector<int> vi;
typedef vector<ll> vl;
 
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
auto clk=clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
int powm(int a, int b) {
	int res=1;
	while(b) {
		if(b&1)
			res=(res*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return res;
}
 
long long readInt(long long l, long long r, char endd) {
	long long x=0;
	int cnt=0;
	int fi=-1;
	bool is_neg=false;
	while(true) {
		char g=getchar();
		if(g=='-') {
			assert(fi==-1);
			is_neg=true;
			continue;
		}
		if('0'<=g&&g<='9') {
			x*=10;
			x+=g-'0';
			if(cnt==0) {
				fi=g-'0';
			}
			cnt++;
			assert(fi!=0 || cnt==1);
			assert(fi!=0 || is_neg==false);
 
			assert(!(cnt>19 || ( cnt==19 && fi>1) ));
		} else if(g==endd) {
			if(is_neg) {
				x=-x;
			}
			assert(l<=x&&x<=r);
			return x;
		} else {
			assert(false);
		}
	}
}
string readString(int l, int r, char endd) {
	string ret="";
	int cnt=0;
	while(true) {
		char g=getchar();
		assert(g!=-1);
		if(g==endd) {
			break;
		}
		cnt++;
		ret+=g;
	}
	assert(l<=cnt&&cnt<=r);
	return ret;
}
long long readIntSp(long long l, long long r) {
	return readInt(l,r,' ');
}
long long readIntLn(long long l, long long r) {
	return readInt(l,r,'\n');
}
string readStringLn(int l, int r) {
	return readString(l,r,'\n');
}
string readStringSp(int l, int r) {
	return readString(l,r,' ');
}
 
int sum_n=0;
int a[100005];
int tr[400005];
void solve() {
//	int n=readIntLn(2,100000);
//	sum_n+=n;
//	assert(sum_n<=1000'000);
//	fr(i,1,n)
//		if(i!=n)
//			a[i]=readIntSp(1,1000'000'000);
//		else
//			a[i]=readIntLn(1,1000'000'000);
	int l=readIntSp(2,1000'000),r=readIntLn(l,1000'000);
	cout<<998244353<<endl;
 
 
 
 
 
 
 
}
 
signed main() {
	ios_base::sync_with_stdio(0),cin.tie(0);
	srand(chrono::high_resolution_clock::now().time_since_epoch().count());
	cout<<fixed<<setprecision(10);
	int t=readIntLn(1,1000);
//	int t;
//	cin>>t;
	fr(i,1,t)
		solve();
#ifdef rd
	cerr<<endl<<endl<<endl<<"Time Elapsed: "<<((double)(clock()-clk))/CLOCKS_PER_SEC<<endl;
#endif
}
 
Editorialist
#include<bits/stdc++.h>
using namespace std;
 
#define int long long
 
int32_t main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
 
  int t;
  cin>>t;
 
  while(t--)
  {
    int l,r;
    cin>>l>>r;
 
    cout<<10000019<<"\n";
  }
 
return 0;
}

VIDEO EDITORIAL:

7 Likes

what if we don’t want to generalize and write some logic and code to solve this? what should be the logic and code???

2 Likes

sieve is not needed anyway just print a prime greater than r using simple primiality testing for each test case , it will work
my solution CodeChef: Practical coding for everyone

1 Like

The editorialist’s solution is even better it works in O(t), t=number of test cases.

1 Like

Yeah dude! better approach it worked thanks

just print 1000003

3 Likes

Impressive :slightly_smiling_face: