暑假训练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;
}
posted @ 2019-07-06 14:54  夜烛灯花  阅读(144)  评论(0)    收藏  举报