154B - Colliders - CodeForces Solution


math number theory *1600

Please click on ads to support us..

Python Code:

from http.client import SWITCHING_PROTOCOLS
from sys import stdin, stdout

def main():
    n = int(1e5)
    lp = [0 for _ in range(n+1)]
    pr= []

    for i in range(2,n+1):
        if lp[i] == 0:
            lp[i] = i
            pr.append(i)

        j = 0
        while j<len(pr) and i*pr[j]<=n and pr[j]<=lp[i]:
            lp[i*pr[j]] = pr[j]
            j+=1

    visited = [0 for _ in range(n+1)]

    n, m = map(int,stdin.readline().split())
    for _ in range(m):
                a,b = stdin.readline().strip().split()
        b=int(b)
        if visited[b]==b:
            if a == '+':
                stdout.write("Already on\n")
                continue
            else:
                stdout.write("Success\n")
                visited[b] = 0
                if lp [b] !=b:
                    B = b
                    facs = []
                    while B!=1:
                        f = lp[B]
                        facs.append(f)
                        while B%f == 0:
                            B//=f
                    for f in facs:
                        visited[f] = 0
                continue
        if lp[b]==b:
            if a == '-':
                stdout.write('Already off\n')
                continue
                        if visited[b]:
                stdout.write('Conflict with '+str(visited[b])+'\n')
            else:
                visited[b]=b
                stdout.write('Success\n')
        else:
            if a == '-':
                stdout.write('Already off\n')
                continue
            B = b
            facs = []
            while B!=1:
                f = lp[B]
                facs.append(f)
                while B%f == 0:
                    B//=f

            flag = True
            for f in facs:
                if visited[f]:
                    stdout.write('Conflict with '+str(visited[f])+'\n')
                    flag = False
                    break
            if not flag:
                continue
            visited[b] = b
            for f in facs:
                visited[f] = b
            stdout.write('Success\n')

if __name__=='__main__':
    main()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n";
//-----------------------------------------------------------------------------------------
   #define db_bhai        ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
   #define test           int T; cin>>T; for(int t=1;t<=T;t++)
   #define ll             long long
   #define lli            long long int
   #define ld             long double
   #define yes            cout<<"YES"<<endl
   #define no             cout<<"NO"<<endl
   #define f(i,x,n)       for(int i = x; i < n; i++)
   #define fo(i,x,n)      for(int i=x;i>=n;i--)
   #define pb             push_back
   #define pp             pop_back()
   #define all(v)         (v).begin(), (v).end()
   #define array_le_le    lli a[n]; f(i,0,n){cin>>a[i];}
   #define vector_le_le   vector<lli>v; f(i,0,n){int x; cin>>x; v.pb(x);}
//-----------------------------------------------------------------------------------------
   const long long        INF=1e18;
   const int32_t          MM=998244353;
   const int              N=1e7+5;
   const int32_t          M=1e9+7;
//-----------------------------------------------------------------------------------------
ll __lcm(ll a,ll b){return (a*b)/__gcd(a,b);}
const ll MOD = 1e9 + 7;
const double eps=1e-6;

int32_t main(){
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      cout.tie(NULL);

      ll n,m;
      cin>>n>>m;
      map<ll,ll>mp;
      map<ll,ll>mpp;
      for(int i=0;i<m;i++)
      {
         char c;
         ll tmp;
         cin>>c>>tmp;
         if(c=='+')
         {
            if(mp[tmp]==1)
            {
               cout<<"Already on";
            }
            else
            {
               bool flag=0;
               ll ans;
               
               for(int j=2;j<=sqrt(tmp);j++)
               {
                  if(tmp%j==0)
                  {
                     if(mpp[j]>0)
                     {
                        flag=1;
                        ans=mpp[j];
                     }
                     else if(mpp[tmp/j]>0)
                     {
                        flag=1;
                        ans=mpp[tmp/j];
                     }
                  }
               }
               if(mpp[tmp]>0)
               {
                  ans=mpp[tmp];
                  flag=1;
               }
               



               if(flag)
               {
                  cout<<"Conflict with "<<ans;
               }
               else
               {
                  cout<<"Success";
                  mp[tmp]=1;
                  for(int j=2;j<=sqrt(tmp);j++)
                  {
                     if(tmp%j==0)
                     {
                        mpp[j]=tmp;
                        mpp[tmp/j]=tmp;
                     }
                  }
                  mpp[tmp]=tmp;
               }
            }
         }
         else
         {
               if(mp[tmp])
               {
                  cout<<"Success";
                  for(int j=2;j<=sqrt(tmp);j++)
                  {
                     if(tmp%j==0)
                     {
                        mpp[j]=0;
                        mpp[tmp/j]=0;
                     }
                  }
                  mpp[tmp]=0;
                  mp[tmp]--;
               }
               else
               {
                  cout<<"Already off";
               }
         }
         cout<<endl;
      }




      return 0;

}


Comments

Submit
0 Comments
More Questions

1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice