密碼箱 BZOJ - 1406

題目傳送門

思路:x^2 mod n = 1
(x^2 - 1) mod n = 0
(x + 1)(x - 1) mod n = 0
我們假設n = k * n
x - 1 = k1 * n1, x + 1 = k2 * n2
k1 * k2 = k, n1 * n2 = n
我們就可以枚舉所有的大於sqrt(n)的所有因子就可以

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MAXN 100010
#define MAXE 5
#define INF 1000000000
#define MOD 10007
#define LL long long
#define ULL unsigned long long
#define pi 3.14159

using namespace std;

set<LL> Set;

int main() {
    std::ios::sync_with_stdio(false);
    LL n;
    cin >> n;
    for (LL i = 1; i * i <= n; ++i) {
        if (n % i == 0) {
            LL a = i, b = n / i;
            for (LL j = 0; j <= n; j += b) {
                if ((j - 2) % a == 0 && j - 2 >= 0) {
                    Set.insert(j - 1);
                }
                if ((j + 2) % a == 0 && j + 2 < n) {
                    Set.insert(j + 1);
                }
            }
        }
    }
    if (Set.empty()) {
        cout << "None\n";
    } else {
        for (set<LL>::iterator it = Set.begin(); it != Set.end(); ++it) {
            cout << *it << endl;
        }
    }
    return 0;
}
發佈了130 篇原創文章 · 獲贊 25 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章