ABC108C - Triangular Relationship(打表)
程序员文章站
2022-05-18 23:00:34
题意 给出$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 */
上一篇: c++编码规范
下一篇: python基础(基本数据类型常用方法)