• zjuoj 3609 Modular Inverse


    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3609

    Modular Inverse

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1x (mod m). This is equivalent to ax≡1 (mod m).

    Input

    There are multiple test cases. The first line of input is an integer T ≈ 2000 indicating the number of test cases.

    Each test case contains two integers 0 < a ≤ 1000 and 0 < m ≤ 1000.

    Output

    For each test case, output the smallest positive x. If such x doesn't exist, output "Not Exist".

    Sample Input

    3
    3 11
    4 12
    5 13
    

    Sample Output

    4
    Not Exist
    8
    

    References


    Author: WU, Zejun
    Contest: The 9th Zhejiang Provincial Collegiate Programming Contest

    分析:
    题目要求给出a和m的值 , 求出 ax % m == 1 % m成立时的x 的最小值 , 直接x枚举到m即可。

    一开始写的时候没有想到是枚举到m, 后来队友推出m。

    AC代码:

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<queue>
     5 #include<iostream>
     6 #include<stack>
     7 #include<map>
     8 #include<string>
     9 using namespace std;
    10 int main(){
    11     int n, x, a, m;
    12     scanf("%d", &n);
    13     while(n--){
    14         bool flag = true;
    15         scanf("%d%d", &a, &m);
    16         for(x = 1; x <= m; x++){
    17             if((a*x)%m == 1%m){
    18                 flag = false;
    19                 printf("%d
    ", x);
    20                 break;
    21             }
    22         }
    23         if(flag){
    24             printf("Not Exist
    ");
    25         }
    26     }
    27     return 0;
    28 }
    View Code
  • 相关阅读:
    洛谷 P2867 [USACO06NOV]大广场Big Square
    考前冲刺 1
    洛谷 P1939 【模板】矩阵加速(数列)
    洛谷 P3390 【模板】矩阵快速幂
    洛谷 P3376 【模板】网络最大流
    cogs P1578【模板】 次小生成树初级练习题
    洛谷 P3379 【模板】最近公共祖先(LCA)
    10-1 集合之Map
    【Web Components】
    【Animations】
  • 原文地址:https://www.cnblogs.com/jeff-wgc/p/4472233.html
Copyright © 2020-2023  润新知