Author: Sanskriti Singh
Tester: Prachi Deshwal
Editorial: Amit bhardwaj
Difficulty:
Easy
Prerequisite:
Basic Math
Explanation & Algorithm:
There is two approach to this problem. One is by iterating the loop and then
calculating the maximum possible value of n and the other is by directly using the
mathematical formula and computing the results in constant time O(1).Due to the
large constraints our first approach will give time limit exceed(TLE), so to get
optimized and efficient solution we use the second approach of mathematical
formula to get the output.
Solution
Setter's Code
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main()
{
ll t;
cin>>t;
while(t–)
{
ll a,b;
cin>>a>>b;
ll n,res;
cin>>n;
res=n-n%a;
if(res+ b <= n)
cout<<res+b<<endl;
else
cout<<res+b-a<<endl;
}
return 0;
}
Tester's Code
#include
#include
#include
#include
using namespace std;
int main() {
// your code goes here
int n;
cin >> n;
vector<vector> dict;
for (int i = 0; i < n; i++)
{
int a, b, N;
cin >> a >> b;
cin >> N;
vector arr = {a, b, N};
dict.push_back(arr);
}
for (vector x : dict) {
int a = x[0];
int b = x[1];
int N = x[2];
for (int i = N; i > 0; i–)
{
if (i % a == b)
{
cout << i << endl;
break;
}
}
}
return 0;
}