• 数论练习题


    1.Divisor SummationSPOJ - DIVSUM

    题意:求n的所有因子和    (O(N*logN))调和级别

    1. #include <bits/stdc++.h>
    2. #define int long long
    3. #define mp make_pair
    4. #define pb push_back
    5. #define all(a) a.begin(), a.end()
    6. #define rep(i, a, n) for (int i = a; i <= n; i++)
    7. #define per(i, a, n) for (int i = n - 1; i >= a; i--)
    8. #define eps 1e-8
    9. #define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
    10. using namespace std;
    11. const int maxn = 1e6 + 10;
    12. int n, m,d[maxn];
    13. signed main()
    14. {
    15. // freopen(".in", "r", stdin);
    16. // freopen(".out", "w", stdout);
    17. // ios::sync_with_stdio(false);
    18. for (int i = 1; i <= 500000; i++)
    19. {
    20. for (int j = 2 * i; j <= 500000; j += i)
    21. {
    22. d[j] += i;
    23. }
    24. }
    25. int t;
    26. cin >> t;
    27. while (t--)
    28. {
    29. int n;
    30. cin >> n;
    31. cout << d[n] << endl;
    32. }
    33. return 0;
    34. }

    2.GCD LCM

    UVA - 11388

    题意:给出LCM与GCD,求最小符合的数a,b

     由GCD(a,b) | LCM(a,b)

    1. #include <bits/stdc++.h>
    2. #define int long long
    3. #define mp make_pair
    4. #define pb push_back
    5. #define all(a) a.begin(), a.end()
    6. #define rep(i, a, n) for (int i = a; i <= n; i++)
    7. #define per(i, a, n) for (int i = n - 1; i >= a; i--)
    8. #define eps 1e-8
    9. #define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
    10. using namespace std;
    11. const int maxn = 1e6 + 10;
    12. int n, m;
    13. signed main()
    14. {
    15. int t;
    16. cin >> t;
    17. while (t--)
    18. {
    19. int a, b;
    20. cin >> a >> b;
    21. if (b % a != 0)
    22. cout << -1 << endl;
    23. else
    24. cout << a << ' ' << b << endl;
    25. }
    26. return 0;
    27. }

    3.Modified GCD

    CodeForces - 75C

    题意:给出a,b,给出区间l,r,求[l,r]中a,b的最大公约数

    求出gcd(a,b)的所有因子,然后二分查找即可

    1. #include <bits/stdc++.h>
    2. #define int long long
    3. #define mp make_pair
    4. #define pb push_back
    5. #define all(a) a.begin(), a.end()
    6. #define rep(i, a, n) for (int i = a; i < n; i++)
    7. #define per(i, a, n) for (int i = n - 1; i >= a; i--)
    8. using namespace std;
    9. const int maxn = 1e6 + 10;
    10. vector<int> v;
    11. signed main()
    12. {
    13. int n, m;
    14. cin >> n >> m;
    15. int g = __gcd(n, m);
    16. for (int i = 1; i * i <= g; i++)
    17. {
    18. if (g % i == 0)
    19. {
    20. v.pb(i);
    21. if (i * i != g)
    22. v.pb(g / i);
    23. }
    24. }
    25. sort(all(v));
    26. int q;
    27. cin >> q;
    28. while (q--)
    29. {
    30. int l, r;
    31. cin >> l >> r;
    32. auto ans = upper_bound(v.begin(), v.end(), r) - 1;
    33. if ((*ans) >= l && (*ans) <= r)
    34. cout << *ans << endl;
    35. else
    36. cout << -1 << endl;
    37. }
    38. return 0;
    39. }

    4.Chef and KeyboardCodeChef - CHEFKEY 

    题意:给出n,m,c。找出满足a<n,b<m,a*b==c的(a,b)对数

    枚举c的所有因子

    1. #include <bits/stdc++.h>
    2. #define int long long
    3. #define mp make_pair
    4. #define pb push_back
    5. #define all(a) a.begin(), a.end()
    6. #define rep(i, a, n) for (int i = a; i < n; i++)
    7. #define per(i, a, n) for (int i = n - 1; i >= a; i--)
    8. #define eps 1e-8
    9. #define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
    10. using namespace std;
    11. const int maxn = 1e6 + 10;
    12. signed main()
    13. {
    14. // freopen(".in", "r", stdin);
    15. // freopen(".out", "w", stdout);
    16. // ios::sync_with_stdio(false);
    17. int t;
    18. cin >> t;
    19. while (t--)
    20. {
    21. int n, m, c;
    22. cin >> n >> m >> c;
    23. int ans = 0;
    24. for (int i = 1; i < sqrt(c); i++)
    25. {
    26. if (c % i == 0 && i <= n && c / i <= m)
    27. ans++;
    28. }
    29. for (int i = 1; i < sqrt(c); i++)
    30. {
    31. if (c % i == 0 && i <= m && c / i <= n)
    32. {
    33. ans++;
    34. }
    35. }
    36. if ((int)sqrt(c) * (int)sqrt(c) == c && (int)sqrt(c) <= n && (int)sqrt(c) <= m)
    37. {
    38. ans++;
    39. }
    40. cout << ans << endl;
    41. }
    42. return 0;
    43. }

    5.LCM CardinalityUVA - 10892 

    1. #include <bits/stdc++.h>
    2. #define int long long
    3. #define mp make_pair
    4. #define pb push_back
    5. #define all(a) a.begin(), a.end()
    6. #define rep(i, a, n) for (int i = a; i <= n; i++)
    7. #define per(i, a, n) for (int i = n - 1; i >= a; i--)
    8. #define eps 1e-8
    9. #define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
    10. using namespace std;
    11. const int maxn = 1e6 + 10;
    12. int n, m;
    13. signed main()
    14. {
    15. while (cin >> n)
    16. {
    17. if (n == 0)
    18. return 0;
    19. cout << n << " ";
    20. int ans = 1;
    21. for (int i = 2; i <= n; i++)
    22. {
    23. int cnt = 0;
    24. while (n % i == 0)
    25. {
    26. cnt++;
    27. n /= i;
    28. }
    29. if (cnt)
    30. {
    31. ans *= (cnt * 2 + 1);
    32. }
    33. }
    34. if (n > 1)
    35. ans *= 3;
    36. cout << (ans + 1) / 2 << endl;
    37. }
    38. return 0;
    39. }

    6.H(n)

     UVA - 11526 

    整除分块

    1. #include <bits/stdc++.h>
    2. #define int long long
    3. #define mp make_pair
    4. #define pb push_back
    5. #define all(a) a.begin(), a.end()
    6. #define rep(i, a, n) for (int i = a; i <= n; i++)
    7. #define per(i, a, n) for (int i = n - 1; i >= a; i--)
    8. #define eps 1e-8
    9. #define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
    10. using namespace std;
    11. const int maxn = 1e6 + 10;
    12. int n, m;
    13. signed main()
    14. {
    15. int t;
    16. cin >> t;
    17. while (t--)
    18. {
    19. int n;
    20. cin >> n;
    21. int ans = 0;
    22. for (int l = 1, r; l <= n; l = r + 1)
    23. {
    24. r = n / (n / l);
    25. ans += n / l * (r - l + 1);
    26. }
    27. cout << ans << endl;
    28. }
    29. return 0;
    30. }

    7.Calculation 2

    HDU - 3501

    题意 :求小于n且与n不互质的数的和

    首先欧拉函数Euler(n)是求小于n且与n互质的数的个数,再有gcd的性质:如果gcd(n,i)=1,则gcd(n,n-i)=1

    那么,可以看做在[1,n-1]中与n互质的数是成对出现的,即如果i与n互质,则(n-i)也与n互质(Euler(n)为偶数)。而且可以发现这对数(i与n-i)的和为n。

    进一步得到结论:小于n且与n互质的数的和为n*Euler(n)/2;

    那么对于此题课先求1~n的和,再减去n*Euler(n)/2

    原创:

    hdu - 3501 - Calculation 2-(欧拉函数求互质数的和)_菜圾的博客-CSDN博客

    1. #include <bits/stdc++.h>
    2. #define int long long
    3. #define mp make_pair
    4. #define pb push_back
    5. #define all(a) a.begin(), a.end()
    6. #define rep(i, a, n) for (int i = a; i < n; i++)
    7. #define per(i, a, n) for (int i = n - 1; i >= a; i--)
    8. #define eps 1e-8
    9. #define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
    10. using namespace std;
    11. const int maxn = 1e6 + 10;
    12. const int mod = 1e9 + 7;
    13. int st[maxn], pri[maxn], phi[maxn], cnt;
    14. int a[maxn], b[maxn];
    15. void init()
    16. {
    17. for (int i = 2; i < maxn; i++)
    18. {
    19. if (!st[i])
    20. {
    21. pri[cnt++] = i;
    22. phi[i] = i - 1;
    23. }
    24. for (int j = 0; pri[j] * i < maxn; j++)
    25. {
    26. st[pri[j] * i] = 1;
    27. if (i % pri[j] == 0)
    28. {
    29. phi[i * pri[j]] = phi[i] * pri[j];
    30. break;
    31. }
    32. phi[i * pri[j]] = phi[i] * (pri[j] - 1);
    33. }
    34. }
    35. }
    36. int eular(int x)
    37. {
    38. int res = x;
    39. for (int i = 2; i <= x / i; i++)
    40. if (x % i == 0)
    41. {
    42. res = res / i * (i - 1);
    43. while (x % i == 0)
    44. x /= i;
    45. }
    46. if (x > 1)
    47. res = res / x * (x - 1);
    48. return res;
    49. }
    50. signed main()
    51. {
    52. int n;
    53. while (cin >> n)
    54. {
    55. if (n == 0)
    56. break;
    57. int x = eular(n);
    58. int ans = (n * (n - 1) / 2) % mod;
    59. ans = (ans - (n * eular(n) / 2) % mod + mod) % mod;
    60. cout << ans << endl;
    61. }
    62. system("pause");
    63. }

  • 相关阅读:
    jquery实现下拉框
    电脑重装系统后DirectX12旗舰版禁用了怎么解决?
    Spring 注解的诠释
    x86平台SIMD编程入门(1):SIMD基础知识
    最难的IB课程为什么含金量最高?
    高可用组件,Keepalived详解
    125道Python面试题总结
    飞书中板栗看板适合做复杂任务管理吗
    DRF JWT认证(二)
    利用MySQL主从复制延迟拯救误删数据
  • 原文地址:https://blog.csdn.net/weixin_51198300/article/details/125583988