1888B - Raspberries - CodeForces Solution


math number theory

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

 
typedef long double ld;
typedef long long ll;
#define mod 1000000007
#define mod1 998244353
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<pair<ll,ll>, null_type, less<pair<ll,ll>>, rb_tree_tag, tree_order_statistics_node_update>;
//#define ordered_set tree<pair<ll,ll>, null_type,less<pair<ll,ll>>, rb_tree_tag,tree_order_statistics_node_update>
 

const int N = 1e6+1;


 
 
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
 
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
 
bool isPrime(ll n){if(n <= 1)return false;if(n <= 3)return true;if(n%2 == 0 || n%3 == 0)return false;for(ll i=5; i*i<=n; i=i+6)if(n%i == 0 || n%(i+2) == 0)return false;return true;}
ll nextPrime(ll N){if(N<=1)return 2;ll prime = N;bool found = false;while(!found){prime++;if(isPrime(prime))found=true;}return prime;}
//ll fact(ll n){if(n==1) return 1;return  n* fact(n-1);}
ll cl(ll n,ll d){return (n+d-1)/d;} 
ll gcd(ll a, ll b) {if (b == 0)return a; return gcd(b, a % b);}
ll lcm(ll a,ll b){return (a*b)/(gcd(a,b));}
 
 
typedef long double ld;
//typedef long long ll;
 
ll Pow(ll x, ll n) {
  long long ans = 1;
  long long xx=x;
  while (n) {
    if (n & 1)
      ans=(ans*xx);
    xx=(xx*xx);
  
    n >>= 1;
  }
  return ans;
}
 
 
 
 
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
void nl(ll x){
 cout<<x<<"\n";
}
 
void nl(ll x , ll y){
  cout<<x<<" "<<y<<"\n";
}
 
void yes(){
  cout<<"YES\n";
}
 
void no(){
  cout<<"NO\n";
}
 
void nva(vector<ll>v,ll i,ll n){
 for(ll j=i;j<i+n;j++){
  cout<<v[j]<<" ";
 }
 cout<<"\n";
}
 
void nv(vector<ll>v){
  for(ll x : v){
    cout<<x<<" ";
  }
  cout<<"\n";
}
 
void na(ll arr[],ll i,ll n){
   for(ll j=i;j<i+n;j++){
  cout<<arr[j]<<" ";
 }
 cout<<"\n";
}
 
void sp(){
  cout<<" ";
}
//odd , even , brute force ko dekho
//gcd , prime facorization kabhi bhi lag skta h dhyaan do lcm ko bhi dekhlo
//x1+x2+...x2m=n solutions are (n+2m-1)!/(2m)!(n-1)!
//ho skta h dsu lage
//ho skta h constraint chote ho kuch mil sake
//multiple or single multiset priority queue dhyaan rakh
//binary search , apna wala brute o(n) wala
//dp loop wali memozisation 
//ll dp[1005][1005];










void solve(){
   ll n,k,i;
   cin>>n>>k;
   ll a[n];
   for(i=0;i<n;i++){
     cin>>a[i];
   }
   
   if(k==4){
     ll z = 2,y=INT_MAX;
     for(i=0;i<n;i++){
       if(a[i]%k==0){
         nl(0);
         return;
       }
       y=min(y,(k-(a[i]%k)));
       a[i]=(z-(a[i]%z))%z;
     }
     sort(a,a+n);
     nl(min(a[0]+a[1],y));
     return;
   }
   
   
   ll z = INT_MAX;
   for(i=0;i<n;i++){
      z=min(z,(k-(a[i]%k))%k);
   }
   nl(z);
}




 
  
int main()
{
     fastio;

 
     ll t;
     cin>>t;
     //t=1;
     while(t--)
     {
         
         solve();
      
     }
     
    return 0;
}


Comments

Submit
0 Comments
More Questions

125B - Simple XML
567B - Berland National Library
431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum
1555B - Two Tables
1686A - Everything Everywhere All But One
1469B - Red and Blue
1257B - Magic Stick
18C - Stripe
1203B - Equal Rectangles
1536A - Omkar and Bad Story
1509A - Average Height
1506C - Double-ended Strings
340A - The Wall