2021杭电多校2 1010/HDU 6970 I love permutation

题目链接:https://acm.hdu.edu.cn/showproblem.php?pid=6970

题目大意:有一个长度为\(P-1\)的序列满足\(b_{i} = ai(mod \ P)\),其中\(P\)为奇素数,求这个序列逆序对的奇偶

题目思路:
\(k\)为置换\(g\)的逆序数,则有

\[sgn\left ( g \right )=\prod_{1\leq i< j\leqslant n}^{}\frac{g\left (j \right )-g\left ( i \right )}{j-i}=\left ( -1 \right )^{k} \]

其中\(g(i)\)的意思是置换的结果
证明参考这里ACM要什么证明
本题的置换为

\[\begin{pmatrix} 1&2&3&\dots &P-1\\ a&2a&3a&\dots &(P-1)a \end{pmatrix} \]

代入计算公式

\[\prod_{1\leq i< j < P}^{}\frac{g\left (j \right )-g\left ( i \right )}{j-i}\\ =\prod_{1\leq i< j < P}^{}\frac{aj \ mod \ P-ai \ mod \ P}{j-i}\\ =\prod_{1\leq i< j < P}^{}\frac{a(j-i) \ mod \ P}{j-i}\\ =\prod_{1\leq i< j < P}^{}\frac{a(j-i) }{j-i} \ mod \ P\\ =a^{\frac{P(P-1)}{2}} \ mod \ P\\ =a^{\frac{P(P-1)}{2} \ mod \ \varphi (P) } \ mod \ P\\ =a^{\frac{(P-1)}{2} } \ mod \ P\\ \]

AC代码:

#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <stack>
#include <deque>
#include <queue>
#include <cmath>
#include <map>
#include <set>
using namespace std;
typedef pair<int, int> PII;
typedef pair<double, int> PDI;
typedef __int128 int128;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 10, M = 4e7 + 10;
const int base = 1e9;
const int P = 131;
const int mod = 998244353;
const double eps = 1e-8;
const double PI = acos(-1.0);
ll ksm(ll a, ll b, ll mod)
{
	ll res = 1 % mod;
	while (b)
	{
		if (b & 1)
			res = (int128)res * a % mod;
		a = (int128)a * a % mod;
		b >>= 1;
	}
	return res;
}
int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		ll a, P;
		scanf("%lld%lld", &a, &P);
		ll ans = ksm(a, (P - 1) / 2, P);
		if (ans == 1)
			printf("0\n");
		else
			printf("1\n");
	}
	return 0;
}

posted @ 2021-07-26 10:12  xiaopangpang7  阅读(125)  评论(0编辑  收藏  举报