(1) chocolatesbynumbers
n blocks of chocolate, numbered from 0 to N-1, lined up in a circle. Eat from number No. 0, if the last time you ate x, this time eat (x + M)% n, if the number already exists, then stop. How many pieces of chocolate did you eat before the question ended?
data range M, N [1..10^9]
requirement complexity time O (log (M + N)) space O (1)
Analysis: It can be proved that eating chocolate must form a circle starting from number No. 0. Because 0, m% n, m * 2 n .... These numbers, if there are two equal, such as a * m% n and b * m% n, meet 0 < A < b and a * m% n = = b * M% n, then there is (b-a) * M% n = = 0, stating that again appears b * m% n ago, Already appeared in 0. So the problem is equivalent to finding a minimum positive integer that satisfies x * M% N = = 0, so that x = N/GCD (M, N).
You can also with includes, for example://#include <algorithm>int gcd (int x,int y) { return y?gcd (y, x% y): X ;} int solution (int N, int M) { //write your code in c++98 return n/gcd (M, N);}
(2) given two equal-length arrays of integers, how many (a "I", B "I") contain the same type of mass factor?
data range: array length Z [1..6000], range of each number[1..2147483647].
Required Complexity time O (z* (log (max (A "i" + B "I")) ^ 2), Space O (1)
Analysis: Suppose g = gcd (a "I", B "I"), the next thing we do is to see whether A "I" and B "I" and g contain the same mass factor. We constantly ask for g ' = gcd (a "I", g), and then from a "I" in the G ', when the g ' = = 1 (coprime), if A "i" = = 1 will indicate that they have the same quality factor. How does this time complexity count? Time Complexity O (log (A "I" + g)) = O (Loga "i") per gcd, how many times are required? Each time a "I" is made, the index of at least one factor is reduced by 1, so the maximum number of cycles is the exponent of all primes within a "I". So the maximum number of cycles is log (a "I") times, so the time complexity of finding a pair is at O ((log (A "i") ^ 2) + O (log (B "i") ^ 2) and adding Max is exactly the required complexity.
Code:
You can use includes, for example://#include <algorithm>//you can write to stdout for debugging purposes, e.g./ /cout << "This is a debug message" << endl;int gcd (int x,int y) { return y?gcd (y, x% y): x;} BOOL Same (int x,int y) {for (; y > 1;) { y = gcd (x, y); x/= y; } return (x = = 1);} int solution (vector<int> &a, vector<int> &b) { //write your code in c++11 int n = a.size (), A Nswer = 0; for (int i = 0; I < n; ++i) { int g = gcd (A "i", B "I"); if (Same (A "I", g) && Same (B "I", g)) { ++answer; } } return answer; }
Faster algorithms:
In fact, we only need to look at a "I" enough large enough to the square B "i" = = 0 && B "I" sufficient large sub-square% a "i" = = 0 can be. This is because if they contain the same mass factor, a large enough square (when the exponent is large enough) must be a multiple of another. The question is, "big enough," how big is it? In fact, this is the log level. Of course, this is large enough to be two points, can also be recycled. Modulo exponentiation is also the complexity of log, so if the previous two points, we get a single set of Loglog algorithm. But I don't think that's necessary, if the loop, we get the log algorithm. That's not as straightforward as we are to take big enough for each other. That is, a "I" ^ B "I"% b "I" = = 0 && B "I" ^ a "I"% a "i" = = 0, which must be big enough ... Note that modulo exponentiation is the complexity of log ...
Code:
You can use includes, for example://#include <algorithm>//you can write to stdout for debugging purposes, e.g./ /cout << "This is a debug message" << Endl;int mul (Long long X,long long y,int m) { return x * y% M;} int powermod (int x,int y,int m) {int r = 1 m; for (; y; y >>= 1) { if (Y & 1) { R = Mul (r, X, m); } x = Mul (x, X, M); } return r;} int solution (vector<int> &a, vector<int> &b) { //write your code in c++11int n = a.size (), answer = 0; for (int i = 0; i < n; ++i) { if (Powermod (A "i", B "I", B "i") = = 0) && (powermod (B "I", a "I", a "i") = = 0)) { ++answer; } } return answer;}
Exercises on codility (10)