Q: If you have x,y,n. and you have to find a number k between 0<=n so that,
k%x=y
Approach1(Bruteforce):
1.Check if n%x==y print n
2.if n%x!=y then loop until n%x==y then print n.
Code:
Time Complexity: O(n)
let assume, k=p'*x+y
now, p'=(k-y)/x ; 0<=k<=n;
so p'=floor((k-y)/x)
ans=k=p'*x+y
- #include <bits/stdc++.h>
- using namespace std;
- int main(){
- int t;cin>>t;
- while(t--)
- {
- int x,y,n;cin>>x>>y>>n;
- if(n%x==y)cout<<n<<endl;
- else
- {
- while(n%x!=y)
- {
- n--;
- }
- cout<<n<<endl;
- }
- }
- }
Time Complexity: O(n)
let assume, k=p'*x+y
now, p'=(k-y)/x ; 0<=k<=n;
so p'=floor((k-y)/x)
ans=k=p'*x+y
Theory Review:
Code:
- #include <bits/stdc++.h>
- using namespace std;
- int main(){
- int t;cin>>t;
- while(t--)
- {
- int x,y,n;cin>>x>>y>>n;
- int p=floor(n-y)/x;
- cout<<p*x+y<<endl;
- }
- }
Related Problem: https://codeforces.com/contest/1374/problem/A
Comments
Post a Comment