let's start Code

Showing posts with label LightOJ. Show all posts
Showing posts with label LightOJ. Show all posts

1178 - Trapezium

Problem link: 1178 - Trapezium 

Explanation: math-only-math.com 

Code:

 

Share:

1118 - Incredible Molecules

Problem link: 1118 - Incredible Molecules

Topic: Geometry

Resources:

Robert Eisele

Matrix.Code

Code:


Share:

Pick’s Theorem

Problem link: 1418 - Trees on My Island / (UVA – 10088)

topic: picks theorem

Implementation:


Share:

1112 - Curious Robin Hood

Share:

1049 - One Way Roads

1049 - One Way Roads

Topic: DFS

concept: কস্টকে ডিরেক্টটেড ধরবো তারপর আনডিক্টটেড গ্রাফ এ ডিএফএস চালাবো.

Share:

1263 - Equalizing Money

1263 - Equalizing Money

Topic: BFS

Concept: সব গুলা কম্পোনেন্ট এ চেক করবো সমান পরিমান টাকা আছে কিনা।


Share:

1002 - Country Roads

1002 - Country Roads

Topic: dijkstra

solution 01:


Share:

1005 - Rooks

1005 - Rooks

Topic: combinatorics + Binomial Coefficient



Share:

1067 - Combinations

1067 - Combinations

Topic: Counting.


Share:

1387 - Setu

1387 - Setu

solution :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    // freopen("in.txt", "r", stdin);
    int T;
    cin >> T;
    for(int cs = 1; cs <= T; ++cs){
        long long ans = 0;
        int n;
        cin >> n;
        cout << "Case "<<cs<< ":\n";
        while(n--){
            string s;
            cin >> s;
            if(s == "donate"){
                int x;
                cin >> x;
                ans += x;
            }
            else{
                cout << ans<<endl;
            }
        }
    }
}

Share:

1225 - Palindromic Numbers (II)

1225 - Palindromic Numbers (II)

Solution:

 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll Max = 100005;

void palindrome(ll n, int cs)
{
  ll test = n;
  ll ans = 0;
  while(n)
  {
     ans = ans*10 + n%10;
    n /= 10;
  }
  cout << "Case "<<cs<<": ";
  if(test == ans)cout << "Yes\n";
  else cout << "No\n";
}

int main()
{
    ///*
  #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
  #endif
    //*/
    int t;
    cin >> t;
    for(int cs = 1; cs <= t; cs++){
      ll n;
    cin >> n;
    palindrome(n, cs);
    }

  //double end_time = clock();
  //printf( "Time = %lf ms\n", ( (end_time - start_time) / CLOCKS_PER_SEC)*1000);

    return 0;
}
Share:

1202 - Bishops


 1202 - Bishops

#include<bits/stdc++.h>
using namespace std;
#define MAX 2000100
typedef long long ll;


int main()
{

  #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
  #endif
    //double start_time = clock();
    int T;

    cin>>T;

    for(int i = 1; i <= T; i++)
    {
        ll r1,c1,r2,c2;
        cin >> r1 >> c1 >> r2 >> c2;
        if((r1+c1)%2 != (r2+c2)%2){
           cout<<"Case "<<i<<": impossible\n";
        }
        else{
           cout<<"Case "<<i<<": ";
           if((r1+c1) == (r2+c2) || ((r1 - c1) == (r2 - c2)))cout << "1\n";
           else cout << "2\n";
        }
    }

   //double end_time = clock();
  //printf( "Time = %lf ms\n", ( (end_time - start_time) / CLOCKS_PER_SEC)*1000);

    return 0;
}


Share:

1014 - Ifter Party

1014 - Ifter Party

Topic: Divisor

Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
set<int>V;

int main()
{

   //double start_time = clock();
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
  #endif
    int t;
    cin >> t;
    for(int cs = 1; cs <= t; cs++){
      int p,l;
      cin >> p >> l;
      int ext = p - l;
      int sz = sqrt(ext);
     // cout << ext << " "<<sz<<endl;
      if(l < 1)V.insert(1);
      if(ext > l)V.insert(ext);
      for(int i = 2; i <= sz; i++){
        if(ext%i == 0){
          if(i*i == ext){
            if(i > l)V.insert(i);
          }
          else{
            if(i > l)V.insert(i);
            if(ext/i > l)V.insert(ext/i);
          }
        }
      }
      cout << "Case "<<cs<<":";
      int tt = V.size();
      if(tt == 0)cout << " impossible\n";
      else{
        for(auto it = V.begin(); it != V.end(); it++)cout << " "<<*it;
          cout << endl;
      }
      V.clear();
    }

    return 0;
}
Share:

1136 - Division by 3

                              1136 - Division by 3

                                                Explanation


#include<bits/stdc++.h>

using namespace std;


int cnt(int n)

{

    if(n == 0)return 0;

    int res = (n/3)*2;

    if(n%3 == 2)res += 1;

    return res;

}


int main()

{

    int t;

    cin >> t;

    for(int cs = 1; cs <= t; cs++){

        int a,b;

        cin >> a >> b;

        cout << "Case "<<cs << ": "<< cnt(b) -  cnt(a - 1) <<endl;

    }

    return 0;

}


Share:

1182 - Parity

                                      1182 - Parity



#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin >> t;
    for(int cs = 1; cs <= t; cs++){
        int x;
        cin >> x;
        if(__builtin_parity(x))cout << "Case "<<cs<< ": odd\n";
        else cout << "Case "<<cs<< ": even\n";
    }
    return 0;
}

Share:

1133 - Array Simulation

1133 - Array Simulation

 #include<bits/stdc++.h>

using namespace std;


typedef long long ll;

//map<int , int>M;


int main()

{

     #ifndef ONLINE_JUDGE

    freopen("in.txt", "r", stdin);

    freopen("out.txt", "w", stdout);

  #endif


    int t;

    cin >> t;

    for(int cs = 1; cs <= t; cs++){

        int n,m;

    cin >> n >>m;

    int a[n+2];


    for(int i = 0; i < n; i++){

        cin >> a[i];

    }

    while(m--){

        char x;

        int val;

        cin >> x;

        if(x == 'S'){

            cin >> val;

            for(int i = 0; i < n; i++)

            a[i] += val;

            //sum +=vall;

        }

        else if(x == 'M'){

            cin >> val;

          for(int i = 0; i < n; i++)

            a[i] *= val;

            //mul *= val;

        }

        else if(x == 'D'){

            cin >> val;

           for(int i = 0; i < n; i++)

            a[i] /= val;

            //div *= val;

        }

        else if(x == 'R'){

            reverse(a, a+n);

        }

        else if(x == 'P'){

            int y,z;

            cin >> y >> z;

            swap(a[y], a[z]);

        }

        //for(int i =0; i < n; i++)cout << a[i]<< " ";

          //  cout << endl <<"------------"<<endl;

    }

    cout << "Case "<<cs << ":\n";

    for(int i = 0; i < n; i++){

        cout << a[i];

        if(i < n-1)cout << " ";

    }

    cout << endl;

 }

 return 0;

}

 


Share:

1116 - Ekka Dokka

1116 - Ekka Dokka

  1. #include<bits/stdc++.h>

  2. using namespace std;

  3. int main()

  4. {

  5.    int t;

  6.     cin >> t;

  7.     for(int cs = 1; cs <= t; cs++){

  8.         long long w;

  9.         cin >> w;

  10.         if(w&1){

  11.             cout << "Case "<< cs<<": Impossible\n";

  12.         }

  13.         else{

  14.             for(long long i = 2; i <= w; i+=2){

  15.                 long long x = w/i;

  16.                 if(x&1 && (x*i) == w){

  17.                     cout << "Case "<<cs<< ": "<<x<<" "<<i<<endl;

  18.                     break;

  19.                 }

  20.             }

  21.         }

  22.     }

  23.     return 0;

  24. }


Share:

1113 - Discover the Web

1113 - Discover the Web

Solution:

#include<bits/stdc++.h>

using namespace std;

int main()

{

    int t;

    cin >> t;

    for(int cs = 1; cs <= t; cs++)

    {

        stack<string>f,b,st;

        string s;

        cout << "Case "<<cs<< ":\n";

        st.push("http://www.lightoj.com/");

        while(cin >> s)

        {

            if(s == "VISIT")

            {

                string str;

                cin >> str;

                cout <<str<<endl;

                st.push(str);

                while(!f.empty())f.pop();

            }

            else if(s == "BACK")

            {

               // cout << st.size()<<endl;

                if(st.size() <= 1)

                {

                    cout << "Ignored\n";

                }

                else

                {

                    f.push(st.top());

                    st.pop();

                     cout << st.top()<<endl;

                }

            }

            else if(s == "FORWARD")

            {

                if(f.size() == 0) cout << "Ignored\n";

                else

                {

                    cout << f.top()<<endl;

                    st.push(f.top() );

                    f.pop();

                }

            }

            else break;

        }

    }

    return 0;

}


Share:

1109 - False Ordering

1109 - False Ordering


Solution 01:

#include<bits/stdc++.h>

using namespace std;

void Sieve(int n, bool prime[], bool primesqure[], int a[])

{

    for(int i = 2; i <= n; i++)

        prime[i] = true;

    for(int i = 0; i <= (n*n + 1); i++)

        primesqure[i] = false;

    prime[1] = false;

    for(int p = 2; p*p <= n; p++){

        if(prime[p] == true){

            for(int i = p*2; i <= n; i += p)

                prime[i] = false;

        }

    }

    int j = 0;

    for(int p = 2; p <= n; p++){

        if(prime[p])

        {

            a[j] = p;

            primesqure[p*p] = true;

            j++;

        }

    }

}

int countDivisor(int n)

{

    if(n == 1)return 1;

    bool prime[n+1], primesqure[n*n +1];

    int a[n];

    Sieve(n,prime, primesqure, a);

    int ans = 1;

    for(int i = 0; ; i++){

        if(a[i]*a[i]*a[i] > n)break;

        int cnt = 1;

        while(n % a[i] == 0){

            n = n/a[i];

            cnt = cnt + 1;

        }

        ans = ans * cnt;

    }

    if(prime[n])ans = ans * 2;

    else if(primesqure[n])ans = ans*3;

    else if(n != 1)ans = ans*4;

    return ans;

}

bool cmp (const pair<int, int> &aa, const pair<int, int> &bb)

{

  if(aa.first != bb.first )

    {

        return aa.first < bb.first;

        }

            else     {

        return(aa.second > bb.second);

    }

}

int main()

{

    vector<pair<int, int> >Vp;

    for(int i = 1; i <= 1000; i++){

        int x = countDivisor(i);

        //cout << i << " -------> "<<x<<endl;

        Vp.push_back({x,i});

    }

    sort(Vp.begin(), Vp.end(),cmp);

   // for(auto x: Vp){

       // cout << x.first << " "<< x.second<<endl;

   // }

   int t;

   cin >> t;

   for(int cs = 1; cs <= t; cs++){

        int x;

   cin >> x;

   cout << "Case "<<cs<< ": "<<Vp[x-1].second<<endl;

   }

   return 0;

}


Solution 02:

#include<bits/stdc++.h>

using namespace std;


int countDivisors (int n)

{

    int cnt = 0;

    for(int i = 1; i <= sqrt(n); i++){

        if(n % i == 0){

            if(n/i == i)cnt++;

            else cnt += 2;

        }

    }

    return cnt;

}


bool cmp (const pair<int, int> &aa, const pair<int, int> &bb)

{

  if(aa.first != bb.first )

    {

        return aa.first < bb.first;

        }

            else     {

        return(aa.second > bb.second);

    }

}



int main()

{

    vector<pair<int, int> >Vp;

    for(int i = 1; i <= 1000; i++){

        int x = countDivisors(i);

        //cout << i << " -------> "<<x<<endl;

        Vp.push_back({x,i});

    }

    sort(Vp.begin(), Vp.end(),cmp);

   // for(auto x: Vp){

       // cout << x.first << " "<< x.second<<endl;

   // }

   int t;

   cin >> t;

   for(int cs = 1; cs <= t; cs++){

        int x;

   cin >> x;

   cout << "Case "<<cs<< ": "<<Vp[x-1].second<<endl;

   }

   return 0;

}



Share:

About

let's start CODE

Popular Posts