1406: [AHOI2007]密码箱
https://www.lydsy.com/JudgeOnline/problem.php?id=1406
分析
$x^2 ≡ 1 mod n$
$x^2 = kn +1$
$x^2 - 1 = kn$
$(x + 1) ( x - 1) = kn$
设$n = a imes b$
$(x + 1) ( x - 1) = k imes a imes b$
那么有
$a | (x+1) , b|(x-1)$
$a | (x-1) , b|(x+1)$
所以枚举n个约数a,b,然后分两种情况讨论。
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 set<int>s; 5 6 int main() { 7 8 int n;cin >> n; 9 if (n == 1) { 10 printf("none"); 11 return 0; 12 } 13 for (int i=1,lim=sqrt(n); i<=lim; ++i) { 14 if (n % i == 0) { 15 int a = i, b = n / i; 16 for (int x=1; x<=n; x+=b) { // b | x-1 , kb = x-1, x = kb+1 17 if ((x + 1) % a == 0) s.insert(x); 18 } 19 for (int x=b-1; x<=n; x+=b) { // b | x+1 ,kb = x+1, x = kb - 1 20 if ((x - 1) % a == 0) s.insert(x); 21 } 22 } 23 } 24 if (s.empty()) { 25 printf("none"); 26 return 0; 27 } 28 set<int>:: iterator it; 29 for (it=s.begin(); it!=s.end(); it++) { 30 printf("%d ",*it); 31 } 32 return 0; 33 }