qoj2625 Junk or Joy

题意

给出 \(k\),找出不同的 \((n,p,m)\) 组数使得 \(n^2-kp^m=1\),其中 \(n,p,m\) 为正整数,且 \(p\) 为质数。

\(T=100,1\le k\le 10^9\)

思路

\[n^2-kp^m=1 \]

\[n^2-1=kp^m \]

\[(n+1)(n-1)=(k_1p^{m_1})(k_2p^{m_2}) \pod{k_1k_2=k,m_1+m_2=m} \]

枚举所有可能的 \(k_1,k_2\)

\(k_1p^{m_1}=n+1,k_2p^{m_2}=n-1\)

则有:

\[k_1p^{m_1}-k_2p^{m_2}=2 \]

\(m_1\le m_2\) 时:

\[p^{m_1}(k_1-k_2p^{m_2-m_1})=2 \]

\(p^{m_1}=2\)

显然 \(p=2,m_1=1\),且 \(k_1-k_2p^{m_2-m_1}=1,p^{m_2-m_1}=\frac{k_1-1}{k_2}\)

则当 \(\frac{k_1-1}{k_2}=2^x \pod{x\in \mathbb{N}}\) 时有解。

\(p^{m_1}=1\)

显然 \(m_1=0\),且 \(k_1-k_2p^{m_2-m_1}=2,p^{m_2-m_1}=\frac{k_1-2}{k_2}\)

则当 \(\frac{k_1-2}{k_2}=p^x \pod{x\in\mathbb{N}}\) 时有解。

\(m_1>m_2\) 时同理,得到 \(\frac{k_2+1}{k_1}=2^x \pod{x\in \mathbb{N}}\)\(\frac{k_2+2}{k_1}=p^x\pod{x\in\mathbb{N}}\) 时有解。

于是就写完了,注意判一下 \(m1+m2=0\)

未优化过的时间复杂度为 \(O(V^{\frac{3}{4}})\),理论上 \(T=100\) 不太可过,但是程序十分甚至九分的跑不满,所以就过了。

代码

#include <bits/stdc++.h>
using namespace std;
int T,n;
set<int> ans;
int check(int x){ 
	if(x<0) return 0;
	for(int i=2;i<=sqrt(x);i++){
		if(x%i==0){
			while(x%i==0) x/=i;
			if(x!=1) return 0;
			else return i;
		}
	}
	if(x==1) return 0;
	return x;
}
void add(int k1,int k2){
	if((k1-1)%k2==0&&check((k1-1)/k2)==2)
		ans.insert(2*k1);
	if((k1-2)%k2==0&&check((k1-2)/k2))
		ans.insert(k1);
	if((k2+1)%k1==0&&check((k2+1)/k1)==2)
		ans.insert(2*k2+2);
	if((k2+2)%k1==0&&check((k2+2)/k1))
		ans.insert(k2+2);
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr),cout.tie(nullptr);
	cin>>T;
	while(T--){
		cin>>n;
		ans.clear();
		for(int i=1;i<=sqrt(n);i++){
			if(n%i==0){
				add(i,n/i);
				add(n/i,i);
			}
		}
		cout<<ans.size()<<endl;
	}
	return 0; 
}
posted @ 2025-09-03 19:13  WuMin4  阅读(21)  评论(0)    收藏  举报