模数(2021年新生赛试题)
Background
出题人:刘坤(赛时通过率:36/45)
Description
给定三个整数 �,�,�x,y,n,你的任务是求出最大的 �(0≤�≤�)k(0≤k≤n),使得 �k % �=�x=y。
换句话来说,你需要从 0....�0....n 之间找出一个最大的数对 �x 取模为 �y。
Format
Input
第一行输入一个 �(1≤�≤50000)t(1≤t≤50000),表示测试数量。
接下来 �t 行,每行三个整数 �,�,�(2≤�≤109;0≤�<�;�≤�≤109)x,y,n(2≤x≤109;0≤y<x;y≤n≤109)。
Output
输出 �t 行,每行输出一个最大的 �k。
Samples
输入数据 1
7
7 5 12345
5 0 4
10 5 15
17 8 54321
499999993 9 1000000000
10 5 187
2 0 999999999
Copy
输出数据 1
12339
0
15
54306
999999995
185
999999998
Copy
样例解释
在示例的第一个测试用例中,答案是 12339=7⋅1762+512339=7⋅1762+5(因此,12339���7=512339mod7=5)。很明显,没有不超过 1234512345 的更大整数,模 77 余 55 。
#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int x,y,n;
cin>>x>>y>>n;
for(int i = n;i >= 0;i--){
if(i%x==y){
printf("%d\n",i);
break;
}
}
}
return 0;
}
20个数据AC17,TLE3
答案:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
int t;
cin>>t;
while(t--){
ll x,y,n,p;
cin>>x>>y>>n;
p=n/x*x;
if(n-p<y)p-=x;
cout<<p+y<<"\n";
}
return 0;
}
我的代码是嵌套循环会超时。