问题 M: Triangular Relationship
题目链接
- You are given integers N and K. Find the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K. The order of a,b,c does matter, and some of them can be the same.
- Constraints
·1≤N,K≤2×105
·N and K are integers. - 输入
Input is given from Standard Input in the following format:
N K - 输出
Print the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K. - 样例输入 Copy
3 2 - 样例输出 Copy
9 - 提示
(1,1,1),(1,1,3),(1,3,1),(1,3,3),(2,2,2),(3,1,1),(3,1,3),(3,3,1) and (3,3,3) satisfy the condition. - 题意分析 :
给定N和K。
求三元组(a,b,c)的个数。
三元组满足下面条件:
a、b、c的值都是不大于N的正整数。
a+b、b+c、a+c都是K的整数倍。 - 思路分析
根据数论同余的知识:
a mod K + b mod K = K mod K.
b mod K + c mod K = K mod K.
a mod K + c mod K = K mod K.
解得a=b=c=K/2 mod K(当K是偶数时有解)。或者a=b=c=K mod K。 - 代码
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include <algorithm>
#define
#define"%lld",&a)
const int mod=998244353;
using namespace std;
const int inf=1e9+7;
const int maxn=2e5+10;
ll n,m,sum,r,t,p,l;
ll a[maxn],b[maxn];
ll dp[maxn];
char str[maxn],s[maxn];
int main() {
cin>>n>>m;
t=n/m;
p=n%m;
sum=t*t*t;
if(p>=m/2) t++;
if(m%2==0)
sum+=t*t*t;
cout<<sum<<endl;
return 0;
}