暑假训练Day6

1.筱玛爱地理

费马小定理:\(a^{-n}\) \(mod\) \(p=a^{p-n-1}\) \(mod\) \(p\)\(p\)为质数)

由题目可知\(β=V/E\),而结果要求我们对\(β\)排序后取余,即求$α=β $ \(mod\) \(N (N=1e9+7)\)

所以很容易根据上面的公式推导出\(α=V*E^{p-2}\) \(mod\) \(p\)

接下来就可以用快速幂的取余算法算出\(E^{p-2}\) \(mod\) \(p\)算法详解),最后再乘\(V\)就可以得到\(α\)

虽然比赛的时候各种查资料写出了这个算法,但是读题不仔细,没有注意到要对\(α\)进行排序输出。

而且这道题在排序输出上面也放置了陷阱,需要先算\(β\)的值,并进行排序,而且排序算法应当对等号两边移向进行乘法比较,避免误差。

赛后AC代码:

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const LL N = 1e9 + 7;//定义余数
const int MAXN = 2e5 + 5;//定义结构体数组大小

struct Node
{
    LL x, y;
}num[MAXN];

bool cmp(Node a1, Node a2)
{
    return a1.y*a2.x>a2.y*a1.x;//移向乘法
}

LL fun(LL a, LL t);//快速幂取余

int main()
{
    int k;
    LL a, b;
    cin >> k;
    for (int i = 0; i<k; i++)
        scanf("%lld%lld", &num[i].x, &num[i].y);
    sort(num, num + k, cmp);//先排序再取余
    for (int i = 0; i<k; i++)
        cout << num[i].y*fun(num[i].x, N - 2) % N << endl;
    return 0;
}

LL fun(LL a, LL t)
{
    LL base = a, ans = 1;
    while (t > 0)
    {
        if (t & 1)
        {
            ans *= base;
            ans %= N;
        }
        base *= base;
        base %= N;
        t >>= 1;
    }
    ans = (ans + N) % N;
    return ans;
}

转载于:https://www.cnblogs.com/JMWan233/p/11142716.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值