zoukankan      html  css  js  c++  java
  • NOIP2012同余方程

    描述

    求关于 x的同余方程  ax ≡ 1(mod b) 的最小正整数解。

    输入格式

    输入文件 mod.in
    输入只有一行,包含两个正整数a,b,用一个空格隔开。

    输出格式

    输出文件 为 modmod .out 。
    输出只有一行,包含一个正整数,包含一个正整数 ,包含一个正整数 x0,即最小正整数解。 输入据保证一定有解。

    测试样例1

    输入

    3 10

    输出

    7

    备注

    对于 40% 的数据    2 ≤b≤1,000
    对于 60% 的数据    2 ≤b≤50,000,000
    对于 100%的数据    2 ≤a, b≤2,000,000,000NOIP2012-TG
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #define ll long long
    using namespace std;
    ll euler_phi(ll x){
        ll m = (ll)sqrt(x+0.5);
        ll ans = x;
        for(int i = 2;i <= m;i++){
            if(x % i == 0){
                ans = ans / i * (i-1);
                while(x % i == 0) x /=  i;
            }
        }
        if(x > 1) ans = ans / x * (x-1);
        return ans;
    }
    ll q_mul(ll a,ll b,ll p){
        ll ans = 0;
        while(b){
            if(b&1){
                ans = (ans + a) % p;
                b--;
            }
            b >>= 1;
            a = (a + a) % p;
        }
        return ans;
    }
    ll q_pow(ll a,ll b,ll p){
        ll ans = 1;
        while(b){
            if(b&1){
                ans = q_mul(ans,a,p);
            }
            b >>= 1;
            a = q_mul(a,a,p);
        }
        return ans;
    }
    void exgcd(ll a,ll b,ll& d,ll& x,ll& y){
        if(b == 0){
            x = 1;
            y = 0;
            d = a;
            return;
        }
        exgcd(b,a%b,d,y,x);
        y -= (a/b)*x;
    }
    int phi[1000];
    void phi_table(int n){
        for(int i = 2;i <= n;i++) phi[i] = 0;
        phi[1] = 1;
        for(int i = 2;i <= n;i++) if(!phi[i])
        for(int j = i;j <= n;j+=i){
            if(!phi[j]) phi[j] = j;
            phi[j] = phi[j] / i * (i-1);
        }
    }
    int main(){
        ll a,b,d,x,y;
        cin>>a>>b;
        cout<<q_pow(a,euler_phi(b)-1,b);
        return 0;
    }
  • 相关阅读:
    【Python学习】URL编码解码&if __name__ == '__main__'
    【Python学习】邮件发送
    【Python学习】网络编程
    【Python学习】接口开发
    【Python学习】操作Sqllite
    【Python学习】操作Mongodb
    【Python学习】操作Redis
    【Python学习】操作Mysql
    jzoj6003
    jzoj5995
  • 原文地址:https://www.cnblogs.com/hyfer/p/5794047.html
Copyright © 2011-2022 走看看