Codeforces : 495C Treasure

0

This one is one of my favorite. 🙂


/*
User ID: Tanmoy_Datta
Link : http://codeforces.com/contest/495/problem/C
*/

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 10000
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int c,d,a,b;
    c=d=a=b=0;
    string str;
    cin>>str;
    int len=SZ(str);
    for(int i=0; i<len; i++)
    {
        if(str[i]=='(')
        {
            a++;
            c++;
        }
        if(str[i]==')')
        {
            b++;
            if(c!=0)
                c--;
        }
        if(str[i]=='#')
        {
            d++;
            c=0;
        }
        if(b+d>a)
        {
            cout<<-1<<endl;
            return 0;
        }
    }

    if(c>0)
    {
        cout<<-1<<endl;
        return 0;
    }

    for(int i=1; i<d; i++)
    {
        cout<<1<<endl;
        b++;
    }
    cout<<a-b<<endl;
    return 0;
}

Codeforces: 508C Anya and Ghosts

0

/*
User ID: Tanmoy_Datta
Link : http://codeforces.com/problemset/problem/508/C
*/

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 120
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 100000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;
ll ara[350];
int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    ms(ara,-1000000);
    int m,t,r;
    cin>>m>>t>>r;
    vector<int>v;
    int num;
    for(int i=0; i<m; i++)
    {
        cin>>num;
        v.pb(num);
    }
    if(t<r)
        cout<<-1<<endl;
    else
    {
        int ans=0;
        //reverse(all(v));
        for(int i=0; i<m; i++)
        {
            int c=0;
            for(int j=1; j<=r; j++)
            {
                if(ara[j]+t>=v[i])
                    c++;
            }
            if(c==r)
                continue;
            else
            {
                //ans+=c;
                c=0;
                for(int j=1; j<=r; j++)
                {
                    if(ara[j]+t<v[i])
                    {
                        ara[j]=v[i]-(++c);
                        ans++;
                    }
                }

            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

Codeforces 508B Anton and currency you all know

0

/*
User ID: Tanmoy_Datta
Link : http://codeforces.com/problemset/problem/508/B
*/

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 120
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 100000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    string str;
    cin>>str;
    int lenth=SZ(str);
    int last=str[lenth-1]-'0';
    int a=0;
    int pos=-1,val=-1,dif=1000;
    for(int i=0; i<lenth-1; i++)
    {
        int d=(str[i]-'0');
        if(d%2==0)
        {
            if(last>d)
            {
                char c=str[i];
                str[i]=str[lenth-1];
                str[lenth-1]=c;
                a=1;
                break;
            }
            else
                pos=i;
        }
    }
    if(a)
        cout<<str<<endl;
    else
    {
        if(pos!=-1)
        {
            char c=str[pos];
            str[pos]=str[lenth-1];
            str[lenth-1]=c;
            cout<<str<<endl;
        }
        else
            cout<<-1<<endl;
    }
    return 0;
}

Codeforces 508A Pasha and Pixels

0

/*
User ID: Tanmoy_Datta
Link : http://codeforces.com/contest/508/problem/A
*/

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 120
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 100000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;
bool ara[1010][1010];
bool test=0,test2=0;
int ans=0;
int dirx1[]= {0,0,-1,-1};
int diry1[]= {0,-1,0,-1};

int dirx2[]= {0,0,-1,-1};
int diry2[]= {0,1,0,1};

int dirx3[]= {0,0,1,1};
int diry3[]= {0,1,0,1};

int dirx4[]= {0,0,1,1};
int diry4[]= {0,-1,0,-1};

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    ll n,m,k;

    ms(ara,0);
    cin>>n>>m>>k;
    for(int i=1; i<=k; i++)
    {
        int a,b;
        cin>>a>>b;
        ara[a][b]=true;
        if(i>=4 && !test2)
        {
            int cnt=0;
            {
                for(int j=0; j<4; j++)
                {
                    int c=a+dirx1[j];
                    int d=b+diry1[j];
                    if(c>0 && c<=n && d>0 && d<=m)
                    {
                        if(ara[c][d])
                            cnt++;
                    }
                }
                if(cnt!=4)
                {
                    cnt=0;
                    for(int j=0; j<4; j++)
                    {
                        int c=a+dirx2[j];
                        int d=b+diry2[j];
                        if(c>0 && c<=n && d>0 && d<=m)
                        {
                            if(ara[c][d])
                                cnt++;
                        }
                    }
                }

                if(cnt!=4)
                {
                    cnt=0;
                    for(int j=0; j<4; j++)
                    {
                        int c=a+dirx3[j];
                        int d=b+diry3[j];
                        if(c>0 && c<=n && d>0 && d<=m)
                        {
                            if(ara[c][d])
                                cnt++;
                        }
                    }
                }

                if(cnt!=4)
                {
                    cnt=0;
                    for(int j=0; j<4; j++)
                    {
                        int c=a+dirx4[j];
                        int d=b+diry4[j];
                        if(c>0 && c<=n && d>0 && d<=m)
                        {
                            if(ara[c][d])
                                cnt++;
                        }
                    }
                }
            }
            if(cnt>=4 && !test2)
            {
                test=1;
                ans=i;
                test2=1;
            }
        }
    }
    if(test)
    {
        cout<<ans<<endl;
        return 0;
    }
    cout<<0<<endl;
    return 0;
}

Codeforces 495B – Modular Equations

0

This is a very interesting divisor count problem.


/*
User ID: Tanmoy_Datta
Link : http://codeforces.com/problemset/problem/495/B
*/

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 10000
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;

/* Bits operation */
int Set(int n,int pos)  { return n = n | 1<<pos;}
bool check(int n,int pos) { return n & 1<<pos;}
int Reset(int n, int pos) { return n=n & ~(1<<pos);}

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    ll a,b;
    cin>>a>>b;
    if(a==b)
        cout<<"infinity"<<endl;
    else
    {
        a-=b;
        int ans=0;
        for(int i=1;i*i<=a;i++)
        {
            if(a%i==0)
            {
                if(i>b)
                    ans++;
                if(a/i!=i && (a/i)>b)
                    ans++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}