ABC108C - Triangular Relationship(打表)

题意

给出$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
*/
posted @ 2018-09-02 06:42  自为风月马前卒  阅读(456)  评论(0编辑  收藏  举报

Contact with me