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. 性能测试工具 wrk 使用教程

    文章首发自个人微信公众号:小哈学Java 个人网站地址:https://www.exception.site/wrk 被面试官经常问到之前开发的系统接口 QPS 能达到多少,经常给不出一个数值,支支吾 ...

  2. Python爬虫入门教程 44-100 Charles的安装与使用-手机APP爬虫部分

    1. 第二款抓包工具Charles安装与使用 Charles和Fiddler一样,也是一款抓包工具,比Fiddler界面更加清晰,支持多平台 1.1 官方网址 https://www.charlesp ...

  3. @vue-cli3安装element组件过程

    在创建好的项目目录下 执行: vue add element 如图选择对应的选项 安装成功!KO!

  4. 在Xunit中使用FsCheck

    目录 编写基于Property-based的单元测试 使用FsCheck编写Property-based测试 在Xunit中使用FsCheck 使用FsCheck编写Model-based测试-待续 ...

  5. 取之有道——巧用Root权限 启动其他APP中的Activity

    这次博主来分享一个很巧妙的办法来启动其他APP中Activity的方法. 首先说一下这样做的目的:最近博主在攻克一个技术难点,就是搞定某些三方系统中,对于应用权限的限制.为此给出用户指导,引导用户启动 ...

  6. WPF ResourceDictionary 主题资源替换(一)

    当我们需要在程序中替换主题,更换另一套背景.颜色.样式时,如何在不修改资源Key值,直接替换呢? 问题&疑问 1. Key值冲突 同一ResourceDictionary中,不可以使用相同Ke ...

  7. IntelliJ IDEA下如何设置JSP模板

    今天在学习Spring MVC知识时,发现自己所用的IntelliJ IDEA中自动生成的JSP文件不支持EL表达式的使用,所以就想导入新的JSP模板,方便以后使用.根据旧模板的提示,如下图 找到Se ...

  8. Eclipse设置全局用户名

    -Duser.name=你的名字

  9. vue学习之vuex

    1  首先还是安装 npm install vuex --save. 2 在src这种创建目录为store 创建 index.js  (getters.js ,actions.js ,mutation ...

  10. Windows 2019 docker 速记

    教程参考: http://www.runoob.com/docker/docker-tutorial.html 开发调试参考: https://www.cnblogs.com/xuebuwan/arc ...