【NOIP 模拟题】[T1]数列(找规律+欧几里得算法)

来源:互联网 发布:js什么是实例化对象 编辑:程序博客网 时间:2024/06/06 14:01

【题解】【找规律+辗转相除】

【通过按照题目给出的产生数的方法可以发现:这是大名鼎鼎的更相减损之术!】

【更相减损之术是古人用来求最大公约数的方法,其实质与我们平时常用的辗转相除其实是一样的。那么,我们用辗转相除代替更相减损即可。】

【这样,我们每进行一次,就会产生一个新的数,边辗转相除边计数即可】

#include<cstdio>#include<cstring>#include<algorithm>#define ll long longusing namespace std;ll a,b,cnt;inline void solve(ll a,ll b){while(1) { if(a<b) swap(a,b); if(!(a%b)) {cnt+=a/b; return;}    cnt+=a/b; a=a%b; }}int main(){freopen("seq.in","r",stdin);freopen("seq.out","w",stdout);scanf("%I64d%I64d",&a,&b);if(a<b) swap(a,b);if(!b) {printf("2\n"); return 0;}solve(a,b);printf("%I64d\n",cnt+1);return 0;}


0 0