思路
本题最佳时间复杂度为 ,但一开始只想出了 ,这种做法虽能通过样例,但在极限数据下会超时,因此本题考虑数学方法,代码难度红,思维难道橙。
分类讨论,当 时,就是翻转时刚好漏完,答案为 ;
当 时,就是漏完后过 分翻转,但因为 可能 ,就会出现负数,需判断一下,答案为 ;
当 时,第一次翻转时沙漏上部分有 分,下部分有 分,由于翻转导致沙漏往上流,第二次翻转时下部分的 又刚好流回上部分。所以分情况讨论,翻转次数即为 。当翻转次数为偶数时,答案为 ;当翻转次数为奇数时,答案为 。
Code
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fast_read ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
int t,T,s,k,m;
int main(){
fast_read;
cin>>t;
for(;T<t;T++){
cin>>s>>k>>m;
if(k==s)
cout<<s-(m%s)<<endl;
else if(k>s)
cout<<max(0,s-(m%k))<<endl;
else{
if((m/k)%2==0)
cout<<s-(m%k)<<endl;
else
cout<<k-(m%k)<<endl;
}
}
return 0;
}