標籤: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;}