BZOJ1406: [AHOI2007]密码箱

1406: [AHOI2007]密码箱

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 687  Solved: 390
[Submit][Status]

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

HINT

Source

题解:

给神题跪烂了。。。

benz的题解:

---------------------------------------------------------------

x^2=1 (mod n) 求所有x。
(x-1)(x+1)=0 (mod n)
得 n|(x-1)(x+1)
则一定存在n的一种分解:n=a*b,满足:
a|(x-1) b|(x+1) 或者 a|(x+1)  b|(x-1)
不妨设a<b,则枚举a,两种情况x都进行枚举。
用set判重。 复杂度:O(sqrt(n)*log(n))

----------------------------------------------------------------

代码:

 1 #include<cstdio>
 2 
 3 #include<cstdlib>
 4 
 5 #include<cmath>
 6 
 7 #include<cstring>
 8 
 9 #include<algorithm>
10 
11 #include<iostream>
12 
13 #include<vector>
14 
15 #include<map>
16 
17 #include<set>
18 
19 #include<queue>
20 
21 #include<string>
22 
23 #define inf 1000000000
24 
25 #define maxn 500+100
26 
27 #define maxm 500+100
28 
29 #define eps 1e-10
30 
31 #define ll long long
32 
33 #define pa pair<int,int>
34 
35 #define for0(i,n) for(int i=0;i<=(n);i++)
36 
37 #define for1(i,n) for(int i=1;i<=(n);i++)
38 
39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
40 
41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
42 
43 #define mod 1000000007
44 
45 using namespace std;
46 
47 inline ll read()
48 
49 {
50 
51     ll x=0,f=1;char ch=getchar();
52 
53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
54 
55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
56 
57     return x*f;
58 
59 }
60 set<ll> s;
61 
62 int main()
63 
64 {
65 
66     freopen("input.txt","r",stdin);
67 
68     freopen("output.txt","w",stdout);
69 
70     ll n=read(),m=(ll)sqrt(n);
71     for(int a=1;a<=m;a++)
72         if(n%a==0)
73         {
74             int b=n/a;
75             for(int x=1;x<=n;x+=b)
76                 if((x+1)%a==0)
77                     s.insert(x);
78             for(int x=b-1;x<=n;x+=b)
79                 if((x-1)%a==0)
80                     s.insert(x);
81         }
82     while(!s.empty())
83     {
84      printf("%lld
",*s.begin());
85      s.erase(s.begin());
86     }
87 
88     return 0;
89 
90 }
View Code

 

原文地址:https://www.cnblogs.com/zyfzyf/p/3979101.html