[AHOI2007]密码箱

Description
在一次偶然的情况下,小可可得到了一个密码箱,听说里面藏着一份古代流传下来的藏宝图,只要能破解密码就能打开箱子,而箱子背面刻着的古代图标,就是对密码的提示。经过艰苦的破译,小可可发现,这些图标表示一个数以及这个数与密码的关系。假设这个数是n,密码为x,那么可以得到如下表述: 密码x大于等于0,且小于n,而x的平方除以n,得到的余数为1。 小可可知道满足上述条件的x可能不止一个,所以一定要把所有满足条件的x计算出来,密码肯定就在其中。计算的过程是很艰苦的,你能否编写一个程序来帮助小可可呢?(题中x,n均为正整数)

Input
输入文件只有一行,且只有一个数字n(1<=n<=2,000,000,000)。

Output
你的程序需要找到所有满足前面所描述条件的x,如果不存在这样的x,你的程序只需输出一行“None”(引号不输出),否则请按照从小到大的顺序输出这些x,每行一个数。

Sample Input
12

Sample Output
1
5
7
11


首先还是要推下柿子,首先这题要求\(x^2\equiv 1(\%n)\),所以就有\(x^2-yn=1\),因此有\(yn=(x-1)(x+1)\)

我们令\(y=y_1\times y_2,n=n_1\times n_2\),因此\(y_1\times n_1\times y_2\times n_2=(x-1)(x+1)\),所以我们可以得到\(y_1\times n_1=(x-1),y_2\times n_2=(x+1)\)

所以我们可以直接在\(\sqrt{n}\)的时间内枚举\(n_1,n_2\),我们令\(n_1<n_2\),因此我们可以直接暴力枚举\(y_2\),判断\(y_1\)是否存在

由于不能重复,因此我们可以直接用set存储

/*program from Wolfycz*/
#include<set>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
	static char buf[1000000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
	int x=0,f=1; char ch=gc();
	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<3)+(x<<1)+ch-'0';
	return x*f;
}
inline int read(){
	int x=0,f=1; char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<3)+(x<<1)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x<0)	putchar('-'),x=-x;
	if (x>9)	print(x/10);
	putchar(x%10+'0');
}
int n;
set<int>st;
void work(int a,int b){
	for (int i=1;1ll*i*b<=n;i++){
		if ((i*b+2)%a==0)	st.insert((i*b+1)%n);
		if ((i*b-2)%a==0)	st.insert((i*b-1)%n);
	}
}
int main(){
	n=read();
	for (int i=1;i*i<=n;i++){
		if (n%i)	continue;
		work(i,n/i);
	}
	for (set<int>::iterator it=st.begin();it!=st.end();it++)	printf("%d\n",*it);
}
posted @ 2018-11-22 21:20  Wolfycz  阅读(168)  评论(0编辑  收藏  举报