zoukankan      html  css  js  c++  java
  • POJ 1061

    胡乱写一下,竟然是一次同余方程的内容。

    a=n-m; b=L; d=x-y; 得

    ax+by=d

    然后,根定理,方程有解必须gcd(a,b)|d。

    确定有解后,两边除以gcd(a,b); 此时gcd(a',b')=1;使用EXGCD求出为1的解后再乘上d/gcd(a,b)。

    但要求最小解,就尽可能的把ax的值附到by上去,所以可以有ax=b*k+a*v(因为附到by上后必须仍上a*x的形式)。两边同除a就可得到结果。其实,只有一个可能,就是a=1。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    __int64 gcd(__int64 a,__int64 b){
    	if(b==0) return a;
    	return gcd(b,a%b);
    }
    
    void exgcd(__int64 a,__int64 b,__int64 &x,__int64 &y){
    	if(b==0){
    		x=1; y=0;
    		return ;
    	}
    	exgcd(b,a%b,x,y);
    	__int64 t=x;
    	x=y;
    	y=t-a/b*y;
    }
    
    int main(){
    	__int64 x,y,m,n,L,a,b,c,d;
    	while(scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&m,&n,&L)!=EOF){
    		a=n-m; b=L; d=x-y;
    		c=gcd(a,b);
    		if(d%c!=0){
    			printf("Impossible
    ");
    			continue;
    		}
    		a/=c; b/=c; d/=c;
    		exgcd(a,b,x,y);
    		x*=d; y*=d;
    		__int64 t=(x%(b)+b)%b;
    		printf("%I64d
    ",t);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    Hystrix解析(三)
    Hystrix解析(二)
    在阿里云开发平台编写第一个 HelloWorld 程序
    Jenkins与Docker的自动化CI/CD实战
    网页计数器例子
    ServletContext 对象
    Session
    Cookie
    Request 对象作用域
    转发,重定向(包括请求中文乱码解决)
  • 原文地址:https://www.cnblogs.com/jie-dcai/p/3939440.html
Copyright © 2011-2022 走看看