M: Triangular Relationship (数论)

该博客介绍了如何利用数论中的同余性质解决一个数学问题,即找出所有不超过给定数值N的正整数三元组(a, b, c),使得a+b, b+c, a+c都是另一个整数K的倍数。通过分析,得出了解决问题的关键在于考虑K为偶数和奇数的情况,并给出了简洁的C++代码实现。
摘要由CSDN通过智能技术生成

问题 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 ll long long int
#define sc(a) scanf("%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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值