1359A - Berland Poker - CodeForces Solution


brute force greedy math *1000

Please click on ads to support us..

Python Code:

for i in range(int(input())):
    n, m, k = map(int, input().split())
    d = n // k
    a1 = min(m, d)
    a2 = (m - a1 + k - 2) // (k - 1)
    print(a1 - a2)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define ba boolalpha
#define F first
using vi = vector<int>;
using vll = vector<ll>;
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
vector<int> dx = {1, 1, -1, -1};
vector<int> dy = {1, -1, 1, -1};
const long long int mod = 1000000007;
#define loop(i,a,b) for(int i=a;i<b;i++)
#define vect_in(v,n) loop(i,0,n){ll temp; cin>>temp; v.pb(temp);}
#define vect_out(v,n) loop(i,0,n){cout<<v[i]<<" ";}cout<<endl;
ll gcd(ll a,ll b){if(b==0){return a;}return gcd(b,a%b);}
long long binpow(long long a, long long b, long long m) {  a %= m;long long res = 1; while (b > 0) { if (b & 1)res = res * a % m;a = a * a % m;  b >>= 1;} return res;}
long long sum_array(vector<ll>&ans){ll n = ans.size();ll x = 0;loop(i,0,n){x+=ans[i];}return x;}
ll phi(ll n){ll res = n;for(int i = 2;i*i<=n;i++){if(n%i==0){while(n%i==0){n=n/i;}res-=res/i;}}if(n>1)res=res-res/n;return res;}
ll extendedgcd(ll a, ll b, ll& x, ll& y){if(b==0){x=1;y=0;return a;}ll x1,y1; ll d = extendedgcd(b,a%b,x1,y1);x=y1;y=x1-y1*(a/b);return d;}
bool lineardiph(ll a,ll b,ll c,ll&x,ll&y,ll&g){g=extendedgcd(abs(a),abs(b),x,y); if(c%g!=0){return false;}; x*=c/g;y*=c/g;if(a<0){x=-x;}if(b<0){y=-y;}return true;}

void solve(){
ll n,m,k;
cin>>n>>m>>k;

bool flag=1,flag2=1;

if(n%k==0)
{
    flag=0;
}

if(((m-((n/k)+flag))%(k-1))==0)
{
    flag2=0;
}

if(((n/k)+flag)>=m)
{
    cout<<m<<endl;
    return;
}

else
{
    cout<<(((n/k)+flag)-(((m-((n/k)+flag))/(k-1))+flag2))<<endl;
}
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
    ll t = 1;
    cin>>t;
    
    while(t--){
        solve();
    }
 
}


Comments

Submit
1 Comments
  • 4/11/2023 23:41 - Asia/Dhaka

#include<stdio.h>
int main()
{
int n,m,k,t;
scanf("%d",&t);
for(int i = 1 ; i <= t ; i++)
{int v;
int x;
  scanf("%d%d%d",&n,&m,&k);
  int p = n/k;
  int left = k-1;

  if(p >= m)
  {
      printf("%d\n",m);
  }
  else if(p < m)
    {  v = m - p;
     x = v;
     int num[left];


      for(int j = 0 ; j < left ; j++)
      {
         num[j] = v/left;
         v  -= num[j];
      }
      if(left > 1)
      {for(int j = 0 ; j < left ; j++)
      {
          num[j]++;
      }}

      int out = p - num[0];
      printf("%d\n",out);

    }

 

}

return 0 ;

 

}


More Questions

1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness