Official

B - First Contest of the Year Editorial by en_translator


For beginners

If the first contest of a year takes place on day \(F\), the contests will be held on days \(F, F + 7, F + 14, \ldots\) in that year.

Notice that day \(x\) of the next year is virtually day \((D + x)\) of this year. Thus, after adding \(7\) to \(F\) repeatedly while \(F\) is less than or equal to \(D\), the resulting \((F - D)\) is the answer. This procedure can be implemented as a while statement.

Sample code

#include <bits/stdc++.h>
using namespace std;

int main() {
	int d, f;
	cin >> d >> f;
	while (f <= d) f += 7;
	cout << f - d << '\n';
}

Alternatively, we can avoid using a loop and still find the answer as follows:

Sample code

#include <bits/stdc++.h>
using namespace std;

int main() {
	int d, f;
	cin >> d >> f;
	cout << 7 - ((d - f) % 7) << '\n';
}

posted:
last update: