HDU 4229 Vive la Difference!
来源:互联网 发布:大唐 数据所 副所长 编辑:程序博客网 时间:2024/06/13 19:20
Take any four positive integers: a, b, c, d. Form four more, like this:
|a-b| |b-c| |c-d| |d-a|
That is, take the absolute value of the differences of a with b, b with c, c with d, and d with a. (Note that a zero could crop up, but they’ll all still be non-negative.) Then, do it again with these four new numbers. And then again. And again. Eventually, all four integers will be the same. For example, start with 1,3,5,9:
1 3 5 9
2 2 4 8 (1)
0 2 4 6 (2)
2 2 2 6 (3)
0 0 4 4 (4)
0 4 0 4 (5)
4 4 4 4 (6)
In this case, the sequence converged in 6 steps. It turns out that in all cases, the sequence converges very quickly. In fact, it can be shown that if all four integers are less than 2^n, then it will take no more than 3*n steps to converge!
Given a, b, c and d, figure out just how quickly the sequence converges.
That is, take the absolute value of the differences of a with b, b with c, c with d, and d with a. (Note that a zero could crop up, but they’ll all still be non-negative.) Then, do it again with these four new numbers. And then again. And again. Eventually, all four integers will be the same. For example, start with 1,3,5,9:
1 3 5 9
2 2 4 8 (1)
0 2 4 6 (2)
2 2 2 6 (3)
0 0 4 4 (4)
0 4 0 4 (5)
4 4 4 4 (6)
In this case, the sequence converged in 6 steps. It turns out that in all cases, the sequence converges very quickly. In fact, it can be shown that if all four integers are less than 2^n, then it will take no more than 3*n steps to converge!
Given a, b, c and d, figure out just how quickly the sequence converges.
Input
There will be several test cases in the input. Each test case consists of four positive integers on a single line (1 ≤ a,b,c,d ≤ 2,000,000,000), with single spaces for separation. The input will end with a line with four 0s.
Output
For each test case, output a single integer on its own line, indicating the number of steps until convergence. Output no extra spaces, and do not separate answers with blank lines.
Sample Input
1 3 5 94 3 2 11 1 1 10 0 0 0
Sample Output
640
#include <stdio.h>int pd(long long a,long long b,long long c,long long d){ if(a==b&&a==c&&a==d) return 1; return 0;}long long f(long long n){ return n>0?n:-n;}int main(){ long long a,b,c,d,e; int i; while(~scanf("%lld %lld %lld %lld",&a,&b,&c,&d),a||b||c||d) { for(i=0;;i++) { if(pd(a,b,c,d)) break; e=a; a=f(a-b),b=f(b-c),c=f(c-d),d=f(d-e); } printf("%d\n",i); } return 0;}
0 0
- HDU 4229 Vive la Difference!
- hdu4229Vive la Difference!
- void difference(node** LA , node* LB)
- hdu 4438 && la 6389
- hdu 4161 Iterated Difference
- hdu 4598 Difference
- HDU 4598 Difference
- HDU 4161 Iterated Difference
- hdu 5936 Difference 二分
- HDU 4161(Iterated Difference )
- hdu 4715(Difference Between Primes)
- hdu 4715 Difference Between Primes
- hdu 4715Difference Between Primes
- hdu 4715 Difference Between Primes
- HDU 4715 Difference Between Primes
- HDU 4715 Difference Between Primes
- hdu 4715 Difference Between Primes
- hdu 5486 Difference of Clustering
- bzoj1188: [HNOI2007]分裂游戏
- html5 canvas画图
- poj 3984 迷宫问题(BFS+回溯)
- 文件的存取
- UML图
- HDU 4229 Vive la Difference!
- Android Http请求框架二:xUtils 框架网络请求
- STL中 map的用法
- SecureCRT上传、下载文件命令sz与rz用法实例
- GraphX 实现K-Core
- CF 567B(Berland National Library-看成折线图)
- vc屏幕截图实现
- Spring下@Resource, @Autowired和@Inject的区别与联系
- Windows(2003、xp)下配置NTP时间服务器