Arkady and his friends love playing checkers on an n×nn×n field. The rows and the columns of the field are enumerated from 11 to nn.

The friends have recently won a championship, so Arkady wants to please them with some candies. Remembering an old parable (but not its moral), Arkady wants to give to his friends one set of candies per each cell of the field: the set of candies for cell (i,j)(i,j) will have exactly (i2+j2)(i2+j2) candies of unique type.

There are mm friends who deserve the present. How many of these n×nn×n sets of candies can be split equally into mm parts without cutting a candy into pieces? Note that each set has to be split independently since the types of candies in different sets are different.

Input

The only line contains two integers nn and mm (1≤n≤1091≤n≤109, 1≤m≤10001≤m≤1000) — the size of the field and the number of parts to split the sets into.

Output

Print a single integer — the number of sets that can be split equally.

Examples

Input
3 3
Output
1
Input
6 5
Output
13
Input
1000000000 1
Output
1000000000000000000

Note

In the first example, only the set for cell (3,3)(3,3) can be split equally (32+32=1832+32=18, which is divisible by m=3m=3).

In the second example, the sets for the following cells can be divided equally:

  • (1,2)(1,2) and (2,1)(2,1), since 12+22=512+22=5, which is divisible by 55;
  • (1,3)(1,3) and (3,1)(3,1);
  • (2,4)(2,4) and (4,2)(4,2);
  • (2,6)(2,6) and (6,2)(6,2);
  • (3,4)(3,4) and (4,3)(4,3);
  • (3,6)(3,6) and (6,3)(6,3);
  • (5,5)(5,5).

In the third example, sets in all cells can be divided equally, since m=1m=1.

题意:

给定一个n和一个数m,求有多少对数a,b,满足一下条件:

1<=a<=n

1<=b<=n

(a*a+b*b)%m==0

思路:

这题主要考察了取余的性质。

我们应该知道 (a+b)%M=(a%M+b%M)%M

(a*b)%m=((a%m)*(b%m))%m

知道这个性质的话,我们就可以把(a*a+b*b)%m==0

转为(a%m*a%m+b%m*b%m)%m==0

因为a%m和b%m的范围是0~m-1,

那么我们可以先预处理出1~n中,有多少个数对m取余的结果是i,0<=i<=m-1

然后m*m的时间复杂度去枚举 i*i+j*j 是否是m的倍数,如果是,答案加上 i的数量乘以j的数量。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll cnt[maxn];
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout); ll n;
ll k;
ll ans=0ll;
cin>>n>>k;
repd(i,,k-)
{
cnt[i]=n/k;
if((n%k)>=i)
{
cnt[i]++;
}
}
cnt[]--;
repd(i,,k-)
{
repd(j,,k-)
{
if((i*i+j*j)%k==)
{
ans+=cnt[i]*cnt[j];
}
}
}
cout<<ans<<endl; return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Divide Candies CodeForces - 1056B (数学)的更多相关文章

  1. [codeforces Mail.Ru Cup 2018 Round 3][B Divide Candies ][思维+数学]

    https://codeforces.com/contest/1056/problem/B 题意:输入n,m    求((a*a)+(b*b))%m==0的(a,b)种数(1<=a,b<= ...

  2. Codeforces Round #575 (Div. 3) (A. Three Piles of Candies)(数学)

    A. Three Piles of Candies time limit per test1 second memory limit per test256 megabytes inputstanda ...

  3. Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad) D. Divide and Sum (思维,数学,逆元)

    题意:有一个长度为\(2n\)数组,从中选分别选\(n\)个元素出来组成两个序列\(p\)和\(q\),(\(p\)和\(q\)中只要有任意一个元素在\(a\)的原位置不同,就算一个新的情况),选完后 ...

  4. Sonya and Matrix CodeForces - 1004D (数学,构造)

    http://codeforces.com/contest/1004/problem/D 题意:网格图给定到中心点的曼哈顿距离数组, 求该图n,m及中心点位置 首先可以观察到距离最大值mx一定在某个角 ...

  5. CodeForces 621C 数学概率期望计算

    昨天训练赛的题..比划了好久才想出来什么意思 之前想的是暴力for循环求出来然后储存数组 后来又想了想 自己萌的可以.. 思路就是求出来每个人与他的右边的人在一起能拿钱的概率(V(或)的关系)然后*2 ...

  6. 【CF1063D】Candies for Children 数学

    题目大意 有 \(n\) 个人排成一个圈,你有 \(k\) 颗糖,你要从第 \(l\) 个人开始发糖,直到第 \(r\) 个人拿走最后一颗糖.注意这 \(n\) 个人拍成了一个圈,所以第 \(n\) ...

  7. Mail.Ru Cup 2018 Round 3 B. Divide Candies

    题目链接 分析一下题意可以得到题目要求的是满足下面这个 公式的不同的i,ji,ji,j的方案数; 即(i2+j2)mod&ThinSpace;&ThinSpace; m=0 (n ≤  ...

  8. Sasha and a Very Easy Test CodeForces - 1109E (数学,线段树)

    大意: 给定n元素序列, q个操作: (1)区间乘 (2)单点除(保证整除) (3)区间求和对m取模 要求回答所有操作(3)的结果 主要是除法难办, 假设单点除$x$, $x$中与$m$互素的素因子可 ...

  9. Candies CodeForces - 991C(二分水题)

    就是二分暴力就好了 为什么要记下来 呵呵....emm你说为什么... 行吧 好吧 我一直以为我的二分出问题了 原来不是 依旧很帅 统计的时候求的减了多少次  然后用次数乘了mid 这样做会使那个人获 ...

随机推荐

  1. 【Numpy应用】--对于图片处理的机器学习库的应用

    一.思路 二.代码: #coding:utf-8 import numpy as np import PIL.Image as Image import pickle as p import os c ...

  2. Docker 导出&加载镜像

    文章首发自个人网站:https://www.exception.site/docker/docker-save-load-image 本文中,您将学习 Docker 如何导出&加载镜像.当我们 ...

  3. 【转】使用MySQL处理百万级以上数据时,不得不知道的几个常识

    ---------------------------------------------------------------------------------------------------- ...

  4. Java建造者模式

    建造者模式 建造者模式适用场景: 建造一个复杂的对象适用,将构建对象的过程分开,每个类单独构造对象的一部分,最后组装起来,返回我们需要的对象. 下面的例子主要讲解构造一个飞船 Demo: //要获得的 ...

  5. vue学习记录②(hello world!)

    接着上篇vue-cli脚手架构建项目结构建好项目之后,就开始写个“hello world!”吧~~~ vue玩的都是组件,所以开发的也是组件. 1.新建helloworld.vue.(删除Hello. ...

  6. jsp内置对象-pageContext对象

    1.概念:pageContext对象能够获取JSP页面中的request.response.session.application等其他内置对象.pageContext对象的创建和初始化由容器完成,可 ...

  7. web服务器负载均衡与集群基本概念二

    前面已经说过负载均衡的作用是在多个节点之间按照一定的策略(算法)分发网络或计算处理负载.负载均衡可以采用软件和硬件来实现.一般的框架结构可以参考下图.    后台的多个Web节点上面有相同的Web应用 ...

  8. axis根据wsdl生成java客户端代码

    根据wsdl生成java客户端代码有多个方法,其中使用axis生成的代码比较友好,也是经常用的一种方法.首先下载axis jar包:axis-bin-1_4.zip 官方地址:http://ws.Ap ...

  9. Java中的守护线程

    守护线程的概念 在java中有两种线程,守护线程和非守护线程,其两者并没有本质的区别,唯一的区别就是当前的用户线程退出的时候,若只存在唯一的A线程,若A线程为守护线程,那么JVM将会直接退出,否则JV ...

  10. android中使用afinal一行源码显示网络图片

    下面代码是关于android中使用afinal一行显示网络图片的代码. public class DemoActivity extends FinalActivity { @Override publ ...