Day5-T1
原题目
小月言要过四岁生日了,她的妈妈为她准备了n根火腿,她想将这些火腿均分给m位小朋友,所以她可能需要切火腿。为了省事,小月言想切最少的刀数,使这n根火腿分成均等的m份。请问最少要切几刀?
第一行一个整数 T,表示有 T 组数据,对于每组测试数据格式如下: 每组共一行,有两个数字 N 和 M;意义如题目描述;
输出共 T 行,每组数据一行,输出最少要切的刀数。
S1:
Input:
2 2 6 6 2
Output:
4 0
Describe:小小的数论题。
code:
#include<bits/stdc++.h>
#define INF 214748364
#define eps 1e-9
#define rep1(a,b) for(register int i=(a);i<=(b);i++)
#define rep2(a,b) for(register int j=(a);j<=(b);j++)
using namespace std;
long long t,a,b,ans;
inline int read(){
int ret=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-f;ch=getchar();}
while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
inline double read2(){
double X=0,Y=1.0;int w=0;char ch=0;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=X*10+(ch^48),ch=getchar();
ch=getchar();
while(isdigit(ch)) X+=(Y/=10)*(ch^48),ch=getchar();
return w?-X:X;
}
inline void write(int x){
if(x<0){putchar('-');write(-x);return;}
if(x/10) write(x/10);
putchar(x%10+'0');
}
int main(){
//freopen("hdogs.in","r",stdin);
//freopen("hdogs.out","w",stdout);
t=read();
while(t--)
{
ans=0,a=0,b=0;a=read(),b=read();
if(a%b==0){puts("0");continue;} //能均分(n>m)
a%=b;long long f=__gcd(a,b); //a>=b转化为a<b[能分的先分]
f=a/f;b--,a--; //已经切了一刀
ans=b-a/f;write(ans);puts(""); //玄学通项
}
return 0;
}

浙公网安备 33010602011771号