dificilcoder's blog

By dificilcoder, history, 3 years ago, In English

Help! How to modify this code to solve Jeff and Periods problem

352B - Jeff and Periods

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define nl "\n"
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vl vector<ll>
#define vll vector<pair<ll, ll>>
#define fi first
#define se second

void solve() {
    int n;
    cin>>n;
    int a;
    map<int, pair<int, int>> res;
    
    for(int i=1; i<=n; i++){
       cin>>a;
       if(res[a].first){
            if(res[a].second ==0){
                res[a].second = i;
            }
       }else{
           res[a].first = i;
       }
    }
    cout<<res.size()<<nl;
    for(auto itr = res.begin(); itr!=res.end(); itr++) {
        if(itr->second.second) {
            cout<<itr->first<<" "<<itr->second.second - itr->second.first<<nl;
        }else{
            cout<<itr->first<<" "<<0<<nl;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t=1;
    // cin>>t;
    while(t--) {
        solve();
    }
}

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By dificilcoder, history, 4 years ago, In English

How to arrive at the correct solution which solves for large input values?

Problem : Hard Compare...

Question

Given 4 numbers A,B,C and D. If AB > CD, print "YES" otherwise, print "NO".

Input
Only one line containing 4 numbers A,B,C and D (1≤A,C≤107) , (1≤B,D≤1012)

Output
Print "YES" or "NO" according to the problem above.

My Program

    #include <iostream>
    #define ll long long
    using namespace std;
     
    int main(){
         
      ll res1, res2, a, b, c, d;
      cin>>a>>b>>c>>d;
      res1 = 1;
      res2 = 1;
      for(int i=1;i<=b; i++){
          res1 = a % ((int)1e9+7) *  res1 % ((int)1e9+7);
      }
      for(int i=1;i<=d; i++){
          res2 = c % ((int)1e9+7) *  res2 % ((int)1e9+7);
      }
      
      cout<<(res1>res2 ? "YES" : "NO");
      return 0;
    }

TestCases

Input : 2887969 614604076030 8478041 209676100616
Answer : YES

Input : 8376260 70 8376259 70
Answer : YES

Input : 2 1 1 1
Answer : YES

Input : 1 7816997 1 1
Answer : NO

Issue with current program : I am not able to solve for large input values

Full text and comments »

  • Vote: I like it
  • -8
  • Vote: I do not like it