- #include <bits/stdc++.h>
- #define int long long
- #define mp make_pair
- #define pb push_back
- #define all(a) a.begin(), a.end()
- #define rep(i, a, n) for (int i = a; i <= n; i++)
- #define per(i, a, n) for (int i = n - 1; i >= a; i--)
- #define eps 1e-8
- #define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
- using namespace std;
- const int maxn = 1e6 + 10;
- int n, m,d[maxn];
-
- signed main()
- {
- // freopen(".in", "r", stdin);
- // freopen(".out", "w", stdout);
- // ios::sync_with_stdio(false);
- for (int i = 1; i <= 500000; i++)
- {
- for (int j = 2 * i; j <= 500000; j += i)
- {
- d[j] += i;
- }
- }
- int t;
- cin >> t;
- while (t--)
- {
- int n;
- cin >> n;
- cout << d[n] << endl;
- }
- return 0;
- }
由GCD(a,b) | LCM(a,b)
- #include <bits/stdc++.h>
- #define int long long
- #define mp make_pair
- #define pb push_back
- #define all(a) a.begin(), a.end()
- #define rep(i, a, n) for (int i = a; i <= n; i++)
- #define per(i, a, n) for (int i = n - 1; i >= a; i--)
- #define eps 1e-8
- #define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
- using namespace std;
- const int maxn = 1e6 + 10;
- int n, m;
-
- signed main()
- {
- int t;
- cin >> t;
- while (t--)
- {
- int a, b;
- cin >> a >> b;
- if (b % a != 0)
- cout << -1 << endl;
- else
- cout << a << ' ' << b << endl;
- }
- return 0;
- }
求出gcd(a,b)的所有因子,然后二分查找即可
- #include <bits/stdc++.h>
- #define int long long
- #define mp make_pair
- #define pb push_back
- #define all(a) a.begin(), a.end()
- #define rep(i, a, n) for (int i = a; i < n; i++)
- #define per(i, a, n) for (int i = n - 1; i >= a; i--)
- using namespace std;
- const int maxn = 1e6 + 10;
- vector<int> v;
-
- signed main()
- {
- int n, m;
- cin >> n >> m;
- int g = __gcd(n, m);
- for (int i = 1; i * i <= g; i++)
- {
- if (g % i == 0)
- {
- v.pb(i);
- if (i * i != g)
- v.pb(g / i);
- }
- }
- sort(all(v));
- int q;
- cin >> q;
- while (q--)
- {
- int l, r;
- cin >> l >> r;
- auto ans = upper_bound(v.begin(), v.end(), r) - 1;
- if ((*ans) >= l && (*ans) <= r)
- cout << *ans << endl;
- else
- cout << -1 << endl;
- }
- return 0;
- }
枚举c的所有因子
- #include <bits/stdc++.h>
- #define int long long
- #define mp make_pair
- #define pb push_back
- #define all(a) a.begin(), a.end()
- #define rep(i, a, n) for (int i = a; i < n; i++)
- #define per(i, a, n) for (int i = n - 1; i >= a; i--)
- #define eps 1e-8
- #define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
- using namespace std;
- const int maxn = 1e6 + 10;
- signed main()
- {
- // freopen(".in", "r", stdin);
- // freopen(".out", "w", stdout);
- // ios::sync_with_stdio(false);
- int t;
- cin >> t;
- while (t--)
- {
- int n, m, c;
- cin >> n >> m >> c;
- int ans = 0;
- for (int i = 1; i < sqrt(c); i++)
- {
- if (c % i == 0 && i <= n && c / i <= m)
- ans++;
- }
- for (int i = 1; i < sqrt(c); i++)
- {
- if (c % i == 0 && i <= m && c / i <= n)
- {
- ans++;
- }
- }
- if ((int)sqrt(c) * (int)sqrt(c) == c && (int)sqrt(c) <= n && (int)sqrt(c) <= m)
- {
- ans++;
- }
- cout << ans << endl;
- }
- return 0;
- }
- #include <bits/stdc++.h>
- #define int long long
- #define mp make_pair
- #define pb push_back
- #define all(a) a.begin(), a.end()
- #define rep(i, a, n) for (int i = a; i <= n; i++)
- #define per(i, a, n) for (int i = n - 1; i >= a; i--)
- #define eps 1e-8
- #define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
- using namespace std;
- const int maxn = 1e6 + 10;
- int n, m;
-
- signed main()
- {
- while (cin >> n)
- {
- if (n == 0)
- return 0;
- cout << n << " ";
- int ans = 1;
- for (int i = 2; i <= n; i++)
- {
- int cnt = 0;
- while (n % i == 0)
- {
- cnt++;
- n /= i;
- }
- if (cnt)
- {
- ans *= (cnt * 2 + 1);
- }
- }
- if (n > 1)
- ans *= 3;
- cout << (ans + 1) / 2 << endl;
- }
- return 0;
- }
- #include <bits/stdc++.h>
- #define int long long
- #define mp make_pair
- #define pb push_back
- #define all(a) a.begin(), a.end()
- #define rep(i, a, n) for (int i = a; i <= n; i++)
- #define per(i, a, n) for (int i = n - 1; i >= a; i--)
- #define eps 1e-8
- #define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
- using namespace std;
- const int maxn = 1e6 + 10;
- int n, m;
-
- signed main()
- {
- int t;
- cin >> t;
- while (t--)
- {
- int n;
- cin >> n;
- int ans = 0;
- for (int l = 1, r; l <= n; l = r + 1)
- {
- r = n / (n / l);
- ans += n / l * (r - l + 1);
- }
- cout << ans << endl;
- }
- return 0;
- }
首先欧拉函数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博客
- #include <bits/stdc++.h>
- #define int long long
- #define mp make_pair
- #define pb push_back
- #define all(a) a.begin(), a.end()
- #define rep(i, a, n) for (int i = a; i < n; i++)
- #define per(i, a, n) for (int i = n - 1; i >= a; i--)
- #define eps 1e-8
- #define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
- using namespace std;
- const int maxn = 1e6 + 10;
- const int mod = 1e9 + 7;
-
- int st[maxn], pri[maxn], phi[maxn], cnt;
- int a[maxn], b[maxn];
-
- void init()
- {
- for (int i = 2; i < maxn; i++)
- {
- if (!st[i])
- {
- pri[cnt++] = i;
- phi[i] = i - 1;
- }
- for (int j = 0; pri[j] * i < maxn; j++)
- {
- st[pri[j] * i] = 1;
- if (i % pri[j] == 0)
- {
- phi[i * pri[j]] = phi[i] * pri[j];
- break;
- }
- phi[i * pri[j]] = phi[i] * (pri[j] - 1);
- }
- }
- }
-
- int eular(int x)
- {
- int res = x;
- for (int i = 2; i <= x / i; i++)
- if (x % i == 0)
- {
- res = res / i * (i - 1);
- while (x % i == 0)
- x /= i;
- }
- if (x > 1)
- res = res / x * (x - 1);
-
- return res;
- }
-
- signed main()
- {
- int n;
- while (cin >> n)
- {
- if (n == 0)
- break;
- int x = eular(n);
- int ans = (n * (n - 1) / 2) % mod;
- ans = (ans - (n * eular(n) / 2) % mod + mod) % mod;
- cout << ans << endl;
- }
- system("pause");
- }