Problemk. Last defence
Description
Given Integersa and B. Sequence S is defined as follow:
? S0 = A
? S1 = B
? Si = | Si?1? si?2| For i≥2
Count the number ofdistinct numbers in S.
Input
The first line ofthe input gives the number of test cases, T. t test cases follow. Tis about 100000.
Each test caseconsists of one line-two space-separated integers A, B. (0≤a, b≤1018).
Output
For each test case,output-one line containing ' case #x: Y ', where x is the ' Test Casenumber (starting from 1) and Y-is the Number of distinct numbers inS.
Samples
Sample Input |
Sample Output |
2 7 4 3 5 |
case #1:6 Case #2:5 |
Knowledge Points:
Ad-hoc, the method of dividing.
Main topic:
Given series The first two items of S, requirements after the various meet si= | Si?1? si?2| (The absolute value of the first two differences). Ask the number of different numbers in the whole series S.
Problem Solving Ideas:
first, it is easy to see that when I was big enough, I would eventually seethe repetition of "xx0xx0 ...". So the number of different numbers must be limited.
the reason, for the numberYand theX,Ymust be able to writeKx+bIn the process of generating a sequence of numbers, it appearsKx+b,X,(k-1) X+b,(k-2) X+b,X,...,2x+b,X,X+b,B,X, the number of different numbers that appear is(kx+b)/x, then the problem becomes a numberXand theBproblem, it can be found that this is the process of dividing the method. Every time I make a divideGCD (x, y), the number of different numbers is moreX/Y. At the end, add a0.
There are some special cases to consider, such as the number 0.
Reference code:
#include <iostream>using namespace Std;long long A, B, Ans;int ncase, Ccase;long long calc (Long long A, long long B) { long long ret = 0; while (b) { long long t = B; RET + = A/b; b = a% B; A = t; } return ret + 1;} int main () { ios::sync_with_stdio (false); Cin >> Ncase; while (ncase--) { cin >> a >> b; if (a = = 0 && b = = 0) { ans = 1; } else if (a = = 0 | | b = = 0) { ans = 2; } else { ans = c ALC (A, b); cout << "Case #" << ++ccase << ":" << ans << endl; } return 0;}
CF GYM 100548 Last Defence (2014ACM Xi ' an live game problem K)