标签:des style blog io ar color os sp for
(1)ChocolatesByNumbers分析: 可以证明吃巧克力必然形成一个从0号开始的圈。因为0, M % N, M * 2 % N .... 这些编号,如果有两个相等,比如a * M % N 和 b * M % N,满足0 < a < b 且a * M % N == b * M % N, 则有(b - a) * M % N == 0 ,说明再出现b * M % N 之前,已经出现了0。 于是问题等价于 求一个最小的正整数满足 x * M % N == 0 ,这样的x = N / gcd(M , N)。
// you can also use 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); }
// 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(), answer = 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; }
// 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++11 int 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; }
标签:des style blog io ar color os sp for
原文地址:http://blog.csdn.net/caopengcs/article/details/41819659