前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ACMSGURU 106 - The equation

ACMSGURU 106 - The equation

作者头像
Reck Zhang
发布2021-08-11 10:55:51
4910
发布2021-08-11 10:55:51
举报
文章被收录于专栏:Reck Zhang

The equation

Problem Description

There is an equation ax + by + c = 0. Given a,b,c,x1,x2,y1,y2 you must determine, how many integer roots of this equation are satisfy to the following conditions : x1<=x<=x2, y1<=y<=y2. Integer root of this equation is a pair of integer numbers (x,y).

Input

Input contains integer numbers a,b,c,x1,x2,y1,y2 delimited by spaces and line breaks. All numbers are not greater than 108 by absolute value.

Output

Write answer to the output.

Sample Input

代码语言:javascript
复制
1 1 -3
0 4
0 4

Sample Output

代码语言:javascript
复制
4

Solution

代码语言:javascript
复制
#include <bits/stdc++.h>

int main() {
    std::ios::sync_with_stdio(false);

    std::function<long long(long long, long long, long long&, long long&)> ext_gcd;
    ext_gcd = [&ext_gcd](long long a, long long b, long long& x, long long& y) -> long long {
        long long d = a;
        if(b != 0) {
            d = ext_gcd(b, a % b, y, x);
            y -= (a / b) * x;
        } else {
            x = 1;
            y = 0;
        }
        return d;
    };

    long long a, b, c;
    long long x1, x2;
    long long y1, y2;

    std::cin >> a >> b >> c;
    std::cin >> x1 >> x2;
    std::cin >> y1 >> y2;

    c = -c;
    if (c < 0) {
       c = -c;
       a = -a;
       b = -b;
    }
    if (a < 0) {
        a = -a;
        auto tmp = x1;
        x1 = -x2;
        x2 = -tmp;
    }
    if (b < 0) {
        b = -b;
        auto tmp = y1;
        y1 = -y2;
        y2 = -tmp;
    }

    long long res = 0;
    if (a == 0 && b == 0) {
        res = c == 0 ? (x2 - x1 + 1) * (y2 - y1 + 1) : 0;
    } else if (a == 0 && b != 0) {
        long long tmp_y = c / b;
        res = (c % b == 0 && tmp_y >= y1 && tmp_y <= y2) ? (x2 - x1 + 1) : 0;
    } else if (a != 0 && b == 0) {
        long long tmp_x = c / a;
        res = (c % a == 0 && tmp_x >= x1 && tmp_x <= x2) ? (y2 - y1 + 1) : 0;
    } else {
        long long x, y;
        long long gcd = ext_gcd(a, b, x, y);
        if(c % gcd != 0) {
            res = 0;
        } else {
            long long kx, ky;
            x *= c / gcd;
            y *= c / gcd;
            kx = b / gcd;
            ky = -a / gcd;
            long long left = std::max(std::ceil(1.0 * (x1 - x) / kx) , std::ceil(1.0 * (y2 - y) / ky));
            long long right = std::min(std::floor(1.0 * (x2 - x) / kx), std::floor(1.0 * (y1 - y) / ky));
            res = left > right ? 0 : std::max(0LL, right - left + 1);
        }
    }

    std::cout << res << std::endl;
    return 0;
}
//1 1 -3
//0 4
//0 4
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-06-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • The equation
    • Problem Description
      • Input
        • Output
          • Sample Input
            • Sample Output
              • Solution
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档