· Taught by the Chinese PE Instructor.
· The age limit for this article is 16 +
· All the above two items are successful.
Things start with yesterday's exam.
Pro 2: Given N, how many full shards can n be divided into at least.
That's not a nonsense, all kinds of special judgments.
The determination of prime numbers of Core algorithms and the decomposition of large numbers are totally overwhelmed by many special statements.
Recommended:
1. "64-bit Rabin-Miller + strong Pseudo Prime Number test and implementation of Pollard + rock + factorization algorithm" (the pseudo code inside is amazing .... the legendary PC language ?!)
2. csdn fisher_jiang miller_rabin prime number test-http://blog.csdn.net/fisher_jiang/article/details/986654
3. Go to introduction to algorithms.
Prime Number determination (miller_rabin)
I will not speak out.
In short, it is a relatively high correctness to determine whether P is a prime number by using the Fermat's small Theorem A ^ (p-1) ≡ 1 (mod p) (where a and P are mutually qualitative and P is the prime number.
Prove that no matter (specifically, there is no energy to look at), only the attention algorithm =. =
As long as you select a few more items, the algorithm accuracy can be very high. Currently, there are two methods:
1. Random fetch.
2. Select the first K prime numbers.
The first error rate is 1/4 ^ s (S is the number of randomly selected A), and the second is described in detail in [1, the first 10 prime numbers can satisfy the 100% positive solution in the int64 range.
So how can we find a ^ (p-1 )? Quick power... (it's impossible for anyone to see such a thing. It's not a quick power ......)
Big data decomposition (pollard_pho)
I was so frustrated that I had a bad afternoon.
In short, we can randomly select two numbers a and B (B <A <n) to determine whether gcd (a-B, n) is greater than 1.
Maybe it will be said that how can this random problem be quickly solved! Or something.
However, if we construct a circular section (A1, A2, A3, A4, A5... AK) (in the sense of modulo n) and make B have a certain pattern.
According to various papers, we can find a factor p of N in O (√ p) (note that it is a factor rather than a qualitative factor, so we need to recursively process it after finding P)
Generally, f (x) = x² + C is used to construct the loop section. For the rest of the proofs, see [1] (I think it should be the best outside of the computing guide)
The following is the pseudocode:
Function Pho (N)
IfN is a primeThen // n is a prime number and exits directly.
Return Error;
X = y = x0; C = random [1, n); // Initialization
K = 0; I = 1; D = 1;
While (true) Do
++ K;
X = f (x), D = gcd (x-y, n); // construct x
IfDε (1, N)Then return D; // locate a factor
IfD = NThen c = random [1, n); // x-y = 0, which indicates that the constant factor of C is a bit weak... for another one (tragedy afternoon, orz screen brother)
IfK = IThen y = X, I <= 1; // update y
End Function
The above is the Brent optimization code of Pollard. For details, see [1].
The following is the code for completely decomposing the number of workers (which includes prime number determination and prime factor decomposition for large numbers)
#include <cmath>#include <cstdio>#include <cstdlib>#include <iostream>#include <algorithm>#define error -1#define ll long longconst int po[11] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29};using namespace std;int test;ll n, p[10010];ll gcd(ll a, ll b){ ll x; while (x = a, b) a = b, b = x % b; return a; }ll mul(ll a, ll b, ll c){ ll plas = 0; for (ll i = 1; i <= b; i <<= 1, a = (a + a) % c) if (b & i) plas = (plas + a) % c; return plas; }ll Rollard_Brent(ll n){ ll x = 1, y = 1, d = 1; y = x; int k = 0, i = 1, z = rand() + 1; while (true) { ++k; x = (mul(x, x, n) + z) % n; d = gcd((x - y + n) % n, n); if (d > 1 && d < n) return d; if (k == i) y = x, i <<= 1; if (d == n) z = rand() + 1; }} bool Miller_Rabin(ll n){ ll a, w, sec; for (int i = 1; i <= 10; ++i) { sec = 1; if (n == po[i]) continue; for (a = po[i], w = 1; w < n; w <<= 1, a = a * a % n) if ((n - 1) & w) sec = sec * a % n; if (sec != 1) return false; } return true;}int tap(ll n){ ll plas = n; int head = 1, tail = 1, top = 0; p[tail] = n; while (head <= tail && n != 1) if (Miller_Rabin(p[head])) { bool wis = 0; while (n % p[head] == 0) wis = !wis, n /= p[head]; if (wis && (p[head] & 3) == 3) goto Compare; ++head; } else { ll vec = Rollard_Brent(p[head]); p[++tail] = p[head] / vec; p[++tail] = vec; ++head; } return 2; Compare: while (!(plas & 3)) plas >>= 2; if (((plas - 7) & 7) == 0) return 4; else return 3;}int main(){ freopen("p2.in", "r", stdin); freopen("p2.out", "w", stdout); srand((unsigned)time(NULL)); scanf("%d", &test); for (; test--; ) { scanf("%I64d", &n); int w = (int) sqrt((double) n); if ((ll) w * w == n) { printf("1\n"); continue; } printf("%d\n", tap(n)); } }