CSU - 1446 Modified LCS

來源:互聯網
上載者:User

標籤:des   style   http   os   io   2014   div   line   

Description

Input

Output

Sample Input

35 3 4 15 3 110 2 2 7 3 3100 1 1 100 1 2

Sample Output

4350

題意:求兩個等差序列相同的元素個數

思路: 首先我們可以假設得到解是當 F1 + D1 * K1 = F2 + D2 * K2,那麼我們可以通過擴充歐幾裡德演算法來求出最小的正數解,再來是當我們知道一組解是(x0, y0)的時候,任意解都可以是(x0 + kb‘, y0-ka‘) {b‘ = b/gcd, a‘ = a/gcd},在各自的範圍內求解,注意處理範圍裡有多少解的時候最好類比一下,容易理解

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>typedef long long ll;using namespace std;ll x, y, g;void exgcd(ll a, ll b) {if (b == 0) {x = 1;y = 0;g = a;return;}exgcd(b, a%b);ll t = x;x = y;y = t - a / b * y;return;}int main() {int t;ll n1, n2, f1, f2, d1, d2;scanf("%d", &t);while (t--) {cin >> n1 >> f1 >> d1 >> n2 >> f2 >> d2;ll f = f2 - f1;exgcd(d1, -d2);if (f % g) {printf("0\n");continue;}else {ll r = abs((-d2)/g);x = ((x * f / g) % r + r) % r;y = (f - x * d1) / (-d2);ll dx = abs(d1 / g);ll dy = abs(d2 / g);ll ans = min((n1 - 1 - x) / dy, (n2 - 1 - y) / dx) + 1;cout << ans << endl;}}return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.