洛谷noip 模拟赛 day1 T1

T7925 剪纸

题目描述

小芳有一张nnn*mmm的长方形纸片。每次小芳将会从这个纸片里面剪去一个最大的正方形纸片,直到全部剪完(剩下一个正方形)为止。

小芳总共能得到多少片正方形纸片?

输入输出格式

输入格式:

一行两个整数nnn和mmm

输出格式:

一行一个整数,总共能得到的纸片数量。

输入输出样例

输入样例#1:
6 4
输出样例#1:
3

说明

对于30%的数据满足n=1n=1n=1

对于60%的数据满足n,m≤103n,mleq 10^{3}n,m103​​

对于100%的数据满足n,m≤1012n,m leq 10^{12}n,m1012

存一下代码...........

#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
LL read(){
    LL ans=0,f=1,c=getchar();
    while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
    while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
    return ans*f;
}
LL n,m,ans;
LL gcd(LL x,LL y){
    while(y){
        LL p=x%y;
        x=y;
        y=p;
    }
    return x;
}
int main()
{
    n=read(); m=read();
    if(n<m) swap(n,m);
    if(m==1){printf("%lld
",n); return 0;}
    LL now=gcd(n,m);
    if(now!=1) n/=now,m/=now;
    while(n%m!=0){
        if(n<m) swap(n,m);
        now=n/m;
        ans+=now;
        n=n-m*now;
    }printf("%lld
",ans+n/m);
    return 0;
}
View Code

​​

原文地址:https://www.cnblogs.com/lyzuikeai/p/7263219.html