【2020.5.23Code+】同余方程

题目

Description
在这里插入图片描述

Input
在这里插入图片描述

Output
输出到标准输出。
输出包含 n 行,第 i 行包含一个正整数,表示第 i 个方程解的组数。

Sample Input
Sample Input1
5
59 0
31 15
29 0
47 0
43 38

Sample Input2
1
5 0

Sample Output
Sample Output1
1
32
57
1
44

Sample Output2
9

【样例 2 解释】
9 组解分别为 (a, b) = (0, 0),(1, 2),(1, 3),(2, 1),(2, 4),(3, 1),(3, 4),(4, 2),(4, 3)。

Data Constraint

在这里插入图片描述

思路

二次剩余+中国剩余乱搞就行

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll p,x,nx[10000077],cnt;
ll work(ll p,ll x)
{
	if(p%4==1&&x==0)return 2*p-1;
	if(p%4==1&&x!=0)return p-1;
	if(x==0)return 1;
	return p+1;
}
ll solve(ll p,ll x)
{
	if(nx[p]==0)return work(p,x%p);
	return solve(p/nx[p],x)*work(nx[p],x%nx[p]);
}
int main()
{
	scanf("%lld",&cnt);
	for(int i=2; i<=10000000; i++) if(nx[i]==0)
		for(int j=2; j<=10000000/i; j++) nx[i*j]=i;
	while(cnt--)
	{
		scanf("%lld%lld",&p,&x);
		printf("%lld\n",solve(p,x));
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章