zoukankan      html  css  js  c++  java
  • 20201030 day50 复习13:逆元、裴蜀定理

    逆元

    纯属复习

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    using namespace std;
    #define f(i,x,y) for(int (i)=x;(i)<=(y);(i)++)
    #define fp(i,x,y) for(int (i)=x;(i)>=(y);(i)--)
    long long read(){
    	long long op=1,a=0;char c=getchar();
    	while(c<'0'||c>'9') {if(c=='-') op=-1;c=getchar();}
    	while(c>='0'&&c<='9') {a*=10,a+=c^48,c=getchar();}
    	return a*op;
    }
    const int maxn=3e6+10;
    #define int long long
    int inv[maxn],mod,n;
    //线性求逆元 
    void init_xianxing(){
    	inv[1]=1;
    	f(i,2,n) inv[i]=(long long)(mod-mod/i)*inv[mod%i]%mod;
    	return ;
    }
    //exgcd 
    void exgcd(int a,int b,int &x,int &y){
    	if(!b) x=1,y=0;
    	else exgcd(b,a%b,y,x),y-=a/b*x;	
    }
    int get_exgcd_way(int a,int mod){
    	int x,y;
    	exgcd(a,mod,x,y);
    	x=(x%mod+mod)%mod;
    	return x;
    }
    //费马小定理
    int fpm_get(int x,int p,int mod){
    	x%=mod;
    	int ans=1;
    	while(p){
    		if(p&1) (ans*=x)%=mod;
    		p>>=1;(x*=x)%=mod;	
    	}
    	return ans;
    }
    #undef int 
    int main(){
    	#define int long long
    	n=read(),mod=read();
    //	init_xianxing();
    //	f(i,1,n) printf("%lld
    ",inv[i]); 
    //	f(i,1,n) printf("%lld
    ",get_exgcd_way(i,mod));
    	f(i,1,n) printf("%lld
    ",fpm_get(i,mod-2,mod));
    	return 0;
    }
    

    裴蜀定理

    关于(x,y)的不定方程(ax + by = c)有整数解的充要条件是(gcd(a, b)mid c)

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    using namespace std;
    #define f(i,x,y) for(int (i)=x;(i)<=(y);(i)++)
    #define fp(i,x,y) for(int (i)=x;(i)>=(y);(i)--)
    long long read(){
    	long long op=1,a=0;char c=getchar();
    	while(c<'0'||c>'9') {if(c=='-') op=-1;c=getchar();}
    	while(c>='0'&&c<='9') {a*=10,a+=c^48,c=getchar();}
    	return a*op;
    }
    const int maxn=25;
    #define int long long
    int n,ans;
    int a[maxn];
    int gcd(int x,int y){return !y?x:gcd(y,x%y);}
    #undef int
    int main(){
    	#define int long long
    	n=read();
    	f(i,1,n) {a[i]=read();if(a[i]<0) a[i]=-a[i];ans=gcd(ans,a[i]);}
    	printf("%lld",ans);
    	return 0;
    }
    
  • 相关阅读:
    5个最好用AngularJS构建应用程序框架
    5款最好的免费在线网站CSS验证器
    10款最优秀的开源移动开发工具
    10个最好的免费PS图象处理软件方案
    10个基本的HTML5动画工具设计
    6款最好的免费在线二维码生成器
    Redis配置文件参数说明
    Redis学习手册(主从复制)
    java.lang.OutOfMemoryError: PermGen space PermGen space & java.lang.OutOfMemoryError: Java heap space Heap siz
    TNSNAMES.ORA 配置
  • 原文地址:https://www.cnblogs.com/liuziwen0224/p/20201030day50-002.html
Copyright © 2011-2022 走看看