|
发表于 2022-1-10 19:12:54
|
显示全部楼层
- /*
- 答案:1322
- 耗时:0.0162126秒(单核)
- 0.0035869秒(六核)
- */
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <string>
- #include <cmath>
- #include <omp.h>
- using namespace std;
- int gcd(int x, int y)//计算x,y的最大公因数
- {
- if (y == 0)
- return x;
- int z = x % y;
- while (z != 0)
- {
- x = y;
- y = z;
- z = x % y;
- }
- if (y < 0)
- return (-y);
- return y;
- }
- struct stItem //对应分式 (a*sqrt(D)+b)/c,d=sqrt(D)
- {
- int a, b, c;
- stItem(void)
- {
- a = 0;
- b = 0;
- c = 1;
- }
- stItem(int x, int y, int z)
- {
- c = gcd(x, y);
- c = gcd(c, z);
- a = x / c;
- b = y / c;
- c = z / c;
- }
- stItem(const stItem& x)
- {
- memcpy_s(this, sizeof(stItem), &x, sizeof(stItem));
- }
- const stItem& operator=(const stItem& x)
- {
- memcpy_s(this, sizeof(stItem), &x, sizeof(stItem));
- return *this;
- }
- bool operator==(const stItem& x) const
- {
- return (a == x.a && b == x.b && c == x.c);
- }
- };
- /*
- 返回0还没有找到周期,否则就是周期的长度
- a,b,c进入时为本次的三元组,返回时是下次的三元组
- vItems 记录三元组,用于查找周期
- */
- int f(int nD, int rt, int& a, int& b, int& c, vector<stItem>& vItems)
- {
- int q = int((a * rt + b) / c);
- int a1 = a * c;
- int b1 = c * (c * q - b);
- int c1 = a * a * nD - (c * q - b) * (c * q - b);
- stItem it(a1, b1, c1);
- a = it.a;
- b = it.b;
- c = it.c;
- vector<stItem>::iterator itr = find(vItems.begin(), vItems.end(), it);
- if (itr == vItems.end())
- {
- vItems.push_back(it);
- return 0;
- }
- return int(vItems.end() - itr);
- }
- int main(void)
- {
- double t = omp_get_wtime();
- int nCount = 0;
- #pragma omp parallel for reduction(+:nCount)
- for (int i = 2; i <= 10000; ++i)
- {
- int j = int(sqrt(double(i)));
- if (j * j == i)
- continue;
- vector<stItem> vItems;
- int a = 1, b = 0, c = 1;//刚开始只有sqrt(i)
- stItem it(a, b, c);
- vItems.push_back(it);
- while (true)//展开sqrt(i)
- {
- int k = f(i, j, a, b, c, vItems);
- if (k > 0)//直到出现周期
- {
- nCount += (k & 1);//计数奇数周期
- break;
- }
- }
- }
- t = omp_get_wtime() - t;
- cout << nCount << endl << t << endl;
- return 0;
- }
复制代码 |
|