1. 程式人生 > 實用技巧 >【UOJ 114】分火腿

【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; }