題意 給出$n, k$,求出滿足$a+b, b + c, c + a$都是$k$的倍數的三元組$a, b, c$的個數,$1 \leqslant a, b, c \leqslant N$ $n \leqslant 10^5$ Sol 昨晚Atcoder的第三題 我用$O(1)$的演算法過了一個$n \ ...
題意
給出$n, k$,求出滿足$a+b, b + c, c + a$都是$k$的倍數的三元組$a, b, c$的個數,$1 \leqslant a, b, c \leqslant N$
$n \leqslant 10^5$
Sol
昨晚Atcoder的第三題
我用$O(1)$的演算法過了一個$n \leqslant 10^5$的題qwq。
首先當$a, b, c$是$k$的倍數的話肯定是滿足條件的,答案為$(\frac{N}{K})^3$
關鍵是$a, b, c$中存在不是$k$的倍數的數,顯然,此時$a, b, c$都不能是$k$的倍數
打表找規律得,此時$a, b, c$可以為$\frac{K}{2} + x*K$中的任意數
然後就做完了。。。
/* */ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<vector> #include<set> #include<queue> #include<cmath> //#include<ext/pb_ds/assoc_container.hpp> //#include<ext/pb_ds/hash_policy.hpp> #define Pair pair<int, int> #define MP(x, y) make_pair(x, y) #define fi first #define se second #define int long long #define LL long long #define ull long long #define rg register #define pt(x) printf("%d ", x); //#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++) //char buf[(1 << 22)], *p1 = buf, *p2 = buf; //char obuf[1<<24], *O = obuf; //void print(int x) {if(x > 9) print(x / 10); *O++ = x % 10 + '0';} //#define OS *O++ = ' '; using namespace std; //using namespace __gnu_pbds; const int MAXN = 1e6 + 10, INF = 1e9 + 10, mod = 1e9 + 7; const double eps = 1e-9; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } ull N, K; main() { //freopen("a.in", "r", stdin); //freopen("c.out", "w", stdout); N = read(); K = read(); ull base = N / K; ull ans = base * base * base, ans2 = 0; if(K % 2 == 0) { base = (N - K / 2) / K + (N >= K / 2); ans2 += base * base * base; } cout << ans + ans2; return 0; } /* 50 12 */