You are on page 1of 3

#include <iostream>

#include <vector>
#include <fstream>
#include <cmath>
#include <string>
#include <set>
#include <map>
#include <utility>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
#include <chrono>
#include <unordered_set>
#include <unordered_map>
#include <random>
#include <bitset>
#include <sstream>

using namespace std;

typedef int64_t ll;


typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<int64_t> vll;
typedef vector<vector<int64_t>> vvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef pair<int,int> pii;
typedef pair<int64_t,int64_t> pll;
typedef set<int> si;
typedef set<int64_t> sll;

#define PI 3.14159265359
#define M1 ll(998244353)
#define M2 ll(1000000007)
#define INF 1500000000000000000
#define NINF -1500000000000000000

#define pb push_back
#define pob pop_back()
#define all(x) x.begin(), x.end()

#define o0(a) cout<<a<<" "


#define o1(a) cout<<a<<"\n"
#define o2(a, b) cout<<a<<" "<<b<<"\n"
#define o3(a, b, c) cout<<a<<" "<<b<<" "<<c<<"\n"
#define o4(a, b, c, d) cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n"
#define reach cout<<"Reached!"<<endl;

// Container Output Functions


template <class T>
ostream& operator<<(ostream &os, vector<T> a){
//os<<"[ ";
for(auto x:a){
os<<x<<" ";
}
//return os<<"]"<<"\n";
return os;
}

template <class T>


ostream& operator<<(ostream &os, set<T> a){
//os<<"{ ";
for(auto x:a){
os<<x<<" ";
}
//return os<<"}"<<"\n";
return os;
}

template <class T>


ostream& operator<<(ostream &os, multiset<T> a){
//os<<"{ ";
for(auto x:a){
os<<x<<" ";
}
//return os<<"}"<<"\n";
return os;
}

template <class T,class Q>


ostream& operator<<(ostream &os, pair<T,Q> a){
os<<"| ";
os<<a.ff<<", "<<a.ss<<" ";
return os<<"|";
}

template<class P,class Q, class T>


ostream& operator<<(ostream &os, tuple<P,Q,T> a){
os<<"| "<<(get<0>(a))<<", "<<(get<1>(a))<<", "<<(get<2>(a))<<"|";
return os;
}

// Returns vector containing prefix sum of input vector


template <class T>
vector<int64_t> prefix_sum(vector<T> v){
int n = v.size();
vector<int64_t> prefix_sums(n, 0);
prefix_sums[0] = v[0];
for (int i = 1; i < n; i++){
prefix_sums[i] = prefix_sums[i-1] + v[i];
}
return prefix_sums;
}

//------Start------//

ll mceil(ll p, ll c){
if(p%c == 0) return p/c;
else return p/c + 1;
}

void solve(){
ll n; cin >> n;
ll P; cin >> P;
ll l; cin >> l;
ll t; cin >> t;

ll tasks = (n-1)/7 + 1;
ll k = tasks/2;
ll c = 2*t + l;
ll j = P-k*c;
if(k*c >= P){
o1(n-mceil(P,c));
}
else{
if(tasks%2 == 1){
if(k*c + t + l >= P){
o1(n-k-1);
return;
}
j -= (t+l);
o1(n-mceil(j,l)-1-k);
return;
}
else{
o1(n-mceil(j,l)-k);
return;
}
}
}

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

int tc; cin >> tc;

while(tc--){
solve();
}
}

You might also like