CSU 1446 modified LCS extension Euclidean

Source: Internet
Author: User

I am dying. I have been doing this question for two days ...... Various amazing mistakes ......

This is also the question of hnu 12831.

Question:

We will give you two equal-difference series, and calculate the number of common elements in these two series.

Each series is given in the following format: n f d (representing the length, first item, and tolerances of each series respectively ).

Ideas:

First, an intersection of two series is obtained by extending Euclidean, and then the first intersection of the two series is obtained. Then obtain the number of possible intersections from the first point of intersection to the last point.

Assume that F1 + K1 * D1 = F2 + K2 * D2 is an intersection point and moves to F1-F2 = k2 * D2-K1 * D1. Conclusion X * A + y * B = K * gcd (A, B) from the expanded Euclidean theorem ).

So there is a point of intersection only when the F1-F2 is K * gcd (D1, D2.

In addition, a certain point of intersection can be obtained. Then I want to seek the first intersection. I kneel down for two days and don't want to say 0.

Code:

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cstdlib> 5 # include <cmath> 6 # include <algorithm> 7 # include <string> 8 # include <queue> 9 # include <stack> 10 # include <vector> 11 # include <map> 12 # include <set> 13 # include <functional> 14 # include <time. h> 15 16 using namespace STD; 17 18 typedef long ll; 19 20 const int INF = 1 <30; 21 22 ll N1, F1, D1, N2, F2, d2; 23 24 L L exgcd (ll a, LL B, ll & X, ll & Y) {25 if (B = 0) {26 x = 1; y = 0; 27 return; 28} 29 ll res = exgcd (B, A % B, Y, x); 30 Y-= x * (a/B); 31 return res; 32} 33 34 ll myabs (ll x) {35 return x> 0? X:-X; 36} 37 38 void input () {39 scanf ("% LLD", & N1, & F1, & D1, & N2, & F2, & D2); 40} 41 42 void solve () {43 ll X, Y; 44 ll d = exgcd (D1, D2, X, y); 45 if (0! = (Myabs (F1-F2) % d) {46 puts ("0"); 47 return; 48} 49 ll k = (F1-F2)/d; 50 LL k1 =-K * X, K2 = K * Y; 51 52 LL Gg = max (F1, F2 ); // The first intersection must be greater than or equal to the two starting points 53 ll lcm = D1/D * D2; 54 55 Gg = (F1 + K1 * D1-gg) % LCM + LCM) % LCM + GG; // obtain the first point of intersection 56 57 ll F1 = (gg-F1)/d1 + 1; // calculate the first intersection in the two series respectively subscript 58 ll F2 = (gg-F2)/D2 + 1; 59 60 ll n1 = (N1-f1 + (D2/D )) /(D2/d); 61 ll n2 = (N2-f2 + (D1/D)/(D1/d); 62 ll ans = min (N1, N2 ); 63 If (ANS <0) ans = 0; 64 printf ("% LLD \ n", ANS); 65} 66 67 int main () {68 # ifdef phantom0169 freopen ("hnu12831.txt", "r", stdin); 70 # endif // phantom0171 72 int case; 73 scanf ("% d", & case ); 74 while (case --) {75 input (); 76 solve (); 77} 78 79 return 0; 80}
CSU 1446

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.