1474B - Different Divisors - CodeForces Solution


binary search constructive algorithms greedy math number theory *1000

Please click on ads to support us..

Python Code:

def Sieve(n):
    prime = [True for i in range(n + 1)]
    p = 2
    while (p * p <= n):
        if (prime[p] == True):
            for i in range(p ** 2, n + 1, p):
                prime[i] = False
        p += 1
    prime[0]= False
    prime[1]= False
    return(prime)

prime=Sieve(100000)
for _ in range(int(input())):
    n=int(input())
    a=n+1
    for i in range(a,len(prime)):
        if(prime[i]):
            p=i
            break
    for i in range(p+n,len(prime)):
        if(prime[i]):
            q=i
            break
    print(p*q)

C++ Code:

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll arr[100000];
void sieve(vector<ll>&v){
    arr[0]=0;
    arr[1]=0;
    fill(arr,arr+100000,1);
    for(ll i=2;i*i<=100000;i++){
        if(arr[i]==1){
            v.push_back(i);
            for(ll j=i*i;j<=100000;j+=i){
                arr[j]=0;
            }
        }
    }
}
int main() {
 vector<ll>v;
 sieve(v);
    ll t;cin>>t;
    while(t--){
       ll d;cin>>d;
       ll p1=1;
       ll p2=1;
       ll i=d+1;
       while(arr[i]!=1){
           i++;
       }
       p1=i;
       i=p1+d;
        while(arr[i]!=1){
           i++;
       }
       p2=i;
       ll ans=p1*p2;
       cout<<ans<<"\n";
}
}


Comments

Submit
0 Comments
More Questions

1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion