zoukankan      html  css  js  c++  java
  • BZOJ-5027: 数学题 (扩展欧几里得)

    5027: 数学题

    Time Limit: 10 Sec  Memory Limit: 256 MB
    Submit: 86  Solved: 28
    [Submit][Status][Discuss]

    Description

    给出a,b,c,x1,x2,y1,y2,求满足ax+by+c=0,且x∈[x1,x2],y∈[y1,y2]的整数解有多少对?

    Input

    第一行包含7个整数,a,b,c,x1,x2,y1,y2,整数间用空格隔开。
    a,b,c,x1,x2,y1,y2的绝对值不超过10^8。

    Output

    输出整数解有多少对?

    Sample Input

    1 1 -3 0 4 0 4

    Sample Output

    4

    HINT

     

    Source

    laj现在要沦为抄标程选手了 _(:зゝ∠)_ 明明这题如此简单的 _(:зゝ∠)_

     1 #include "bits/stdc++.h"
     2 using namespace std;
     3 typedef long long LL;
     4 int exgcd(int a,int b,LL &x,LL &y){
     5     if (b==0) return x=1,y=0,a;
     6     int d=exgcd(b,a%b,x,y);
     7     LL t=x;x=y,y=t-a/b*y;
     8     return d;
     9 }
    10 int main(){
    11     freopen ("math.in","r",stdin);freopen ("math.out","w",stdout);
    12     int i,j;
    13     int a,b,c,d,e,f,g;
    14     scanf("%d%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f,&g);c=-c;
    15     if (a==0 && b==0){
    16         if (c==0) printf("%lld",(LL)(e-d+1)*(g-f+1));
    17         else puts("0"); return 0;
    18     }
    19     if (a==0){
    20         if (c%b) return puts("0"),0;
    21         if (c/b>=f && c/b<=g) return printf("%d",e-d+1),0;
    22         return puts("0"),0;
    23     }
    24     if (b==0){
    25         if (c%a) return puts("0"),0;
    26         if (c/a>=d && c/a<=e) return printf("%d",g-f+1),0;
    27         return puts("0"),0;
    28     }
    29     LL x0,y0;int gcd=exgcd(a,b,x0,y0);if(c%gcd) return puts("0"),0;
    30     x0=x0*c/gcd;y0=y0*c/gcd;int dx=b/gcd,dy=-a/gcd;int xt1,xt2,yt1,yt2;
    31     if(dx>0) xt1=ceil((d-x0)*1.0/dx*1.0),xt2=floor((e-x0)*1.0/dx*1.0);
    32     if(dx<0) xt1=ceil((e-x0)*1.0/dx*1.0),xt2=floor((d-x0)*1.0/dx*1.0);
    33     if(dy>0) yt1=ceil((f-y0)*1.0/dy*1.0),yt2=floor((g-y0)*1.0/dy*1.0);
    34     if(dy<0) yt1=ceil((g-y0)*1.0/dy*1.0),yt2=floor((f-y0)*1.0/dy*1.0);
    35     printf("%d",max(0,min(xt2,yt2)-max(xt1,yt1)+1));
    36     return 0;
    37 }
    未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
  • 相关阅读:
    简单工厂、工厂方法和抽象工厂的学习笔记
    单一职责原则,开-闭原则,依赖倒置原则
    我对面向对象设计的理解——Java接口和Java抽象类
    高可用与负载均衡的区别
    应用交付、负载均衡(Load balancing)、高可用、F5
    四种常见的 POST 提交数据方式
    Git fetch和git pull的区别
    苹果Mac OS X显示隐藏文件的方法
    Appium教程---Client/Server Architecture
    UNIX网络编程——使用线程的TCP回射服务器程序
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/7779216.html
Copyright © 2011-2022 走看看