【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));
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章