CF1295D Same GCDs

原题链接

  • 题意:求 \(\sum _{x = 0}^{m-1}[gcd(a, m) = gcd(a + x, m)]\)
  • 题解:\(d = gcd(a, m)\) 然后发现 \(gcd(a, m) = gcd(\frac{a}{d}, \frac{m}{d})\) 并且 \((a+m) \mod m\) 的值域是 \(\left [0, m \right )\)。随演变成求 \(\varphi(\frac{m}{d})\)
  • 代码:
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 5e5 + 10;
vector<ll>V;
void solve() {
    V.clear();
    ll a, m;
    scanf("%lld%lld", &a, &m);
    ll d = __gcd(a, m);
    ll n = m/d;
    ll ans  = n;
    for (ll i = 2; i * i <= n; i ++) {
        if ( n % i == 0) {
            ans = ans/i * (i-1);
            while (n % i == 0)n/=i;
        }
    }
    if (n > 1) {
        ans = ans / n * (n - 1);
    }
    cout << ans  << endl;
}
int main() {
    int t = 1;cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
上一篇:bigemap如何使用高程DEM建立三维地图模型(Arcgis ArcScene)


下一篇:LeetCode 65. 有效数字(模拟,C++)