zoukankan      html  css  js  c++  java
  • 乘法逆元

     逆元详解

    定义:满足a*x≡1 (mod p)的x值就是a关于p的乘法逆元。

    实际就是a*x=k*p+1. x就是a模p的逆元

    逆元一般情况下在求 (a/b)%p 且a很大 无法求a/b,推论(a*k)%p=(a/b)%p k为b模p的逆元

    还有很多用法

    POJ 1845

    详解见大神博客

      1 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~~~hdu 用c++交
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cstdlib>
      5 #include <iostream>
      6 #include <queue>
      7 #include <stack>
      8 #include <cmath>
      9 #include <set>
     10 #include <algorithm>
     11 #include <vector>
     12 #include <map>
     13 // #include<malloc.h>
     14 using namespace std;
     15 #define clc(a,b) memset(a,b,sizeof(a))
     16 #define LL long long
     17 const int inf = 0x3f3f3f3f;
     18 const double eps = 1e-5;
     19 // const double pi = acos(-1);
     20 const LL MOD = 9901;
     21 const int N = 10005;
     22 
     23 // inline int r(){
     24 //     int x=0,f=1;char ch=getchar();
     25 //     while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();}
     26 //     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
     27 //     return x*f;
     28 // }
     29 
     30 bool prime[N];
     31 int p[N];
     32 int cnt;
     33 
     34 void isprime(){
     35     cnt = 0;
     36     clc(prime,true);
     37     for(int i=2;i<N;i++){
     38         if(prime[i]){
     39             p[cnt++]=i;
     40             for(int j=i+i;j<N;j+=i){
     41                 prime[j]=false;
     42             }
     43         }
     44     }
     45 }
     46 
     47 LL pow_m(LL a,LL b)  
     48 {  
     49     LL ans = 1;  
     50     a %= MOD;  
     51     while(b)  
     52     {  
     53         if(b & 1)  
     54         {  
     55             ans = ans * a % MOD;  
     56             b--;  
     57         }  
     58         b >>= 1;  
     59         a = a * a % MOD;  
     60     }  
     61     return ans;  
     62 }  
     63 
     64 //计算1+p+p^2+````+p^n
     65 LL sum(LL p,LL n)
     66 {
     67     if(p==0) return false;
     68     if(n==0) return true;
     69     if(n&1){//奇数
     70         return ((1+pow_m(p,n/2+1))%MOD*sum(p,n/2)%MOD)%MOD;
     71     }
     72     else 
     73         return ((1+pow_m(p,n/2+1))%MOD*sum(p,n/2-1)+pow_m(p,n/2)%MOD)%MOD;
     74 }
     75 
     76 void solve(LL A,LL B){
     77     LL ans=1;
     78     for(int i=0;p[i]*p[i]<=A;i++){
     79         if(A%p[i]==0){
     80             int num=0;
     81             while(A%p[i]==0){
     82                 num++;
     83                 A/=p[i];
     84             }
     85             ans*=sum(p[i],num*B)%MOD;
     86             ans%=MOD;
     87         }
     88     }
     89     if(A>1){//A本身就是素数
     90         ans*=sum(A,B)%MOD;
     91         ans%=MOD;
     92     }
     93     cout<<ans<<endl;
     94 }
     95 int main(){
     96     LL A,B;
     97     isprime();
     98     while(cin>>A>>B){
     99         solve(A,B);
    100     } 
    101     return 0;
    102 }
  • 相关阅读:
    C# 遍历Hashtable
    asp.net 处理超链接中文参数编码问题
    electronvue开发问题总结
    vue全局使用样式文件vueclipluginstyleresourcesloader
    win10使用VMware安装macOS11.1镜像系统教程
    LVM从CentOS7默认安装的/home中转移空间到根目录/(转载)
    Nginx引用多配置文件
    ssh_exchange_identification: read: Connection reset by peer
    linux修改时区
    修改键盘Tab键为四个空格
  • 原文地址:https://www.cnblogs.com/ITUPC/p/5496369.html
Copyright © 2011-2022 走看看