东华大学2020年程序设计竞赛(同步赛)B

东华大学2020年程序设计竞赛(同步赛)B
Given a positive integer y and a prime p, you are asked to find an integer x such that (x \times y)\ mod\ p= 1(x×y) mod p=1. If such x exists, output any x mod p. Otherwise, output -1.

Hint: For any integer a and positive integer b, a mod b is the remainder of a dividing b. Remind that a mod b is non-negative!

输入描述:
The first line of input contains a positive integer T, which is the number of test cases. (1 \le T \le 200,000)(1≤T≤200,000)
For each test case, there will be one line containing 2 positive integers y, p. ( 1 \le y, p \le 10^9 )(1≤y,p≤10
9
), p is guaranteed to be a prime)
输出描述:
Output one integer for each test case. If such x exists, output any x mod p. Otherwise, output -1.
示例1
输入
复制
3
1 97
2 97
3 97
输出
复制
1
49
65
链接:https://ac.nowcoder.com/acm/contest/5891/B
费马小定理 a^(p-1) (mod p)=1 p为素数

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
ll f(ll a,ll b,ll  p)
{
    ll s=1;
    while(b)
    {
        if(b%2==1)s=s*a%p;
        a=a*a%p;
        b=b/2;
    }
    return s;
}
int main()
{int t;
    cin>>t;
    while(t--)
    {
        int y,p;
        cin>>y>>p;
        if(y%p==0)printf("-1\n");
        else
        {
            ll c=f(y,p-2,p);
            cout<<c<<endl;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值