Days on the Island
Autumn Tournament (2024)
Submission name: days.cpp
Problem summary
Two castaways stay on an island for n days, counting their arrival as day one. The first day has weekday p, where Monday is 1 and Sunday is 7. Find the number of full seven-day periods from their arrival, the weekday of rescue, and the total Wednesdays and Saturdays (both have Bulgarian initial С).
Input
Two integers p and n.
Output
Three lines: full weeks; final weekday number; count of Wednesdays and Saturdays.
Constraints
1 ≤ p ≤ 7; 1 ≤ n < 2000000000.
Worked example
The example is reproduced from the original tournament statement.
Input
3 9
Output
1 4 3
Solution analysis
- Every block of seven consecutive days contains each weekday exactly once, regardless of p. Thus n/7 full blocks contribute two occurrences of the target initial each.
- Inspect only the remaining n mod 7 days, starting again at p. Their weekday numbers are (p−1+offset) mod 7+1; count numbers 3 and 6. Rescue is on weekday (p−1+(n−1)) mod 7+1.
- For p=3,n=9, the first week contributes Wednesday and Saturday. The remaining Wednesday and Thursday add one more target day. There is one full week, rescue is Thursday (4), and the count is 3.
Why it works
Partitioning the stay into full weeks and a final remainder covers every day exactly once. Each full week contributes exactly two target days; explicit inspection counts the remaining ones. Advancing n−1 days from the first weekday gives the rescue weekday.
Watch out. Full weeks are counted from arrival, not from Monday. Arrival and rescue are both included.
Commented C++17 solution
An independently written complete program with standard headers.
// Key idea: Partitioning the stay into full weeks and a final remainder covers every day exactly once.
// Each full week contributes exactly two target days; explicit inspection counts the remaining ones.
#include<iostream>
using namespace std;
int main(){
long long p,n;
cin>>p>>n;
long long weeks=n/7;
long long count=2*weeks;
for(int offset=0;offset<n%7;offset++){
long long weekday=(p-1+offset)%7+1;
if(weekday==3||weekday==6){
count++;
}
}
long long last=(p-1+(n-1))%7+1;
cout<<weeks<<'\n'<<last<<'\n'<<count<<'\n';
}