400C - Inna and Huge Candy Matrix - CodeForces Solution


implementation math *1500

Please click on ads to support us..

Python Code:

import sys
input = sys.stdin.readline


def f(a, b, x, c):

    n1, m1 = n, m
    if c == 1:
        x = 4 - x
        if xx % 2:
            n1, m1 = m, n

    if x == 1:
        a, b = b, n1-a+1
    elif x == 2:
        a, b = n1 - a + 1, m1 - b + 1
    elif x == 3:
        a, b = m1-b+1, a

    return (a, b)


n, m, xx, y, z, p = map(int, input().split())
xx %= 4
y %= 2
z %= 4
for _ in range(p):
    a, b = map(int, input().split())
    a, b = f(a, b, xx, 0)
    if y == 1:
        if xx % 2:
            b = n-b+1
        else:
            b = m-b+1
    a, b = f(a, b, z, 1)
    print(a, b)

C++ Code:

#define aavade(a,n) for(ll i=0;i<n;i++) cin>>a[i]
#define f(i,x,n) for(int i=x;i<n;i++)
#define bf(i,n,x) for(int i=n-1;i>=x;i--)
#define ll long long int
#define Max 1000000000+7
#define ull unsigned long long int
#include<bits/stdc++.h>
using namespace std;
bool prime[1000000];
void SieveOfEratosthenes(ll n)
{
    memset(prime, true, sizeof(prime));
 
    for (ll p = 2; p * p <= n; p++) {
        if (prime[p] == true) {
            for (ll i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
}
vector<int> Divisors(int n)
{
    vector<int> v;
    for (int i=1; i<=sqrt(n); i++)
    {
        if (n%i == 0)
        {
            // If divisors are equal, print only one
            if (n/i == i)
                v.push_back(i);
  
            else // Otherwise print both
            {
                v.push_back(i);
                v.push_back(n/i);
            }
        }
    }
    return v;
}
bool isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;
 
    // Check from 2 to square root of n
    for (int i = 2; i <= sqrt(n); i++)
        if (n % i == 0)
            return false;
 
    return true;
}
 
int vis[1000001];
vector<int> g[1000010];
vector<vector<int>> conn;
vector<int> curr_c;
vector<int> vere[1000000];
int cnt=0,cc[1000000];
void dfs_graph(int vertex)
{
    vis[vertex]=1;
    curr_c.push_back(vertex);
    cc[vertex]=cnt;
    vere[cnt].push_back(vertex);
    for(int child:g[vertex])
    {
        if(vis[child]) continue;
        dfs_graph(child);
    }
}
ll height[500001];
ll depth[500001];
int c[100001];
vector<int> ans;
void dfs_tree(ll vertex,ll par=0)
{
    //  cout<<"-->"<<vertex<<'\n';
    int k=c[vertex];
    for(ll child:g[vertex])
    {
        if(child==par) continue;
        depth[child]=depth[vertex]+1;
        // cout<<c[child]<<" "<<c[vertex]<<"\n";
        k*=c[child];
        dfs_tree(child,vertex);
        height[vertex]=max(height[vertex],height[child]+1);
    }
        if(k==1) ans.push_back(vertex);
}
string getbinary(int x)
{
    string s;
    bf(i,10,0)
    {
         s+=to_string((x>>i)&1);
    }
    return s;
}
void subsetsUtil(vector<int>& A, vector<vector<int> >& res, vector<int>& subset, int index)
{
    res.push_back(subset);
    for (int i = index; i < A.size(); i++) {
  
        subset.push_back(A[i]);
          subsetsUtil(A, res, subset, i + 1);
        subset.pop_back();
    }
  
    return;
}
vector<vector<int> > subsets(vector<int>& A)
{
    vector<int> subset;
    vector<vector<int> > res;
    int index = 0;
    subsetsUtil(A, res, subset, index);
  
    return res;
}
int visa[300010];
ll n,r1,c1,r2,c2;
int matrix[1000][1000];
int matrix1[1000][1000];
vector<pair<int,int>> vp;
// ll cnt=0;
map<pair<ll,ll>,ll> mp;
// void dfs_matrix(ll i,ll j)
// {
//     if(i<0 or j<0) return;
//     if(i>=n or j>=n) return;
//     if(matrix[i][j]=='1') return;
//     // cout<<i<<" "<<j<<'\n';
//     matrix[i][j]='1';
//     vp[cnt].push_back({i,j});
//     mp[{i,j}]=cnt;
//     dfs_matrix(i-1,j);
//     dfs_matrix(i+1,j);
//     dfs_matrix(i,j-1);
//     dfs_matrix(i,j+1);
// }
ll power(ll x, ll y,ll p)
{
 
    ll res = 1;
 
    while (y > 0) {
 
        if (y % 2 == 1)
            res = ((res%p) * (x%p))%p;
 
        y = y >> 1;
        x = ((x%p * x%p))%p;
    }
    return res % p;
}
bool check(ll x,ll a[],ll k,ll n)
{
    ll moves=0;
    for (int i=n/2; i<n; i++)
    {
        if (x-a[i]>0) moves+=x-a[i];
        if (moves>k) return false;
    }
    if (moves<=k) return true;
    else return false;
}
void solve()
{
    ll n,m,x,y,z,p,nn,mm;
    cin>>n>>m>>x>>y>>z>>p;
    pair<ll,ll> v[p];
    for(int i =0;i<p;++i)
    {
        cin>>v[i].first>>v[i].second;
        nn = n; mm = m;
        f(j,0,x%4){
        swap(v[i].first,v[i].second);
        v[i].second = nn-v[i].second+1;
        swap(nn,mm);}
        f(j,0,y%2)
        v[i].second = mm-v[i].second+1;
        f(j,0,z%4)
        {
            swap(v[i].first,v[i].second);
            v[i].first=mm-v[i].first+1;
            swap(nn,mm);
        }
        cout<<v[i].first<<" "<<v[i].second<<'\n';}
}
int main()
{
    int t = 1; 
    while(t--)
    {
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
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