【UOJ 114】分火腿

【题目描述】:

小月言要过四岁生日了,她的妈妈为她准备了n根火腿,她想将这些火腿均分给m位小朋友,所以她可能需要切火腿。为了省事,小月言想切最少的刀数,使这n根火腿分成均等的m份。请问最少要切几刀?

【输入描述】:

第一行一个整数T,表示有T组数据。

接下来T组数据,每组共一行,有两个数字n,m。

【输出描述】:

每组数据一行,输出最少要切的刀数。

【样例输入】:

2
2 6
6 2

【样例输出】:

4
0

【时间限制、数据范围及描述】:

时间:1s 空间:64M

100%的数据保证 T<=1000;n,m<=2147483647。

题解:最近数学题好多啊555

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int T,a,b,ans,G,xx;
int gcd(int x,int y){
    if(y==0) return x;
    return gcd(y,x/y);
}
int main(){
    freopen("hdogs.in","r",stdin);
    freopen("hdogs.out","w",stdout);
    cin>>T;
    while(T--){
        scanf("%d %d",&a,&b);
        xx=a-b*(a/b);
        G=gcd(xx,b);
        ans=b-2-(xx-1)/G;
        if(a%b==0) cout<<0<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wuhu-JJJ/p/13880123.html