Divide Candies CodeForces - 1056B (数学)
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
3 3
1
6 5
13
1000000000 1
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 (数学)的更多相关文章
- [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<= ...
- 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 ...
- Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad) D. Divide and Sum (思维,数学,逆元)
题意:有一个长度为\(2n\)数组,从中选分别选\(n\)个元素出来组成两个序列\(p\)和\(q\),(\(p\)和\(q\)中只要有任意一个元素在\(a\)的原位置不同,就算一个新的情况),选完后 ...
- Sonya and Matrix CodeForces - 1004D (数学,构造)
http://codeforces.com/contest/1004/problem/D 题意:网格图给定到中心点的曼哈顿距离数组, 求该图n,m及中心点位置 首先可以观察到距离最大值mx一定在某个角 ...
- CodeForces 621C 数学概率期望计算
昨天训练赛的题..比划了好久才想出来什么意思 之前想的是暴力for循环求出来然后储存数组 后来又想了想 自己萌的可以.. 思路就是求出来每个人与他的右边的人在一起能拿钱的概率(V(或)的关系)然后*2 ...
- 【CF1063D】Candies for Children 数学
题目大意 有 \(n\) 个人排成一个圈,你有 \(k\) 颗糖,你要从第 \(l\) 个人开始发糖,直到第 \(r\) 个人拿走最后一颗糖.注意这 \(n\) 个人拍成了一个圈,所以第 \(n\) ...
- Mail.Ru Cup 2018 Round 3 B. Divide Candies
题目链接 分析一下题意可以得到题目要求的是满足下面这个 公式的不同的i,ji,ji,j的方案数; 即(i2+j2)mod   m=0 (n ≤ ...
- Sasha and a Very Easy Test CodeForces - 1109E (数学,线段树)
大意: 给定n元素序列, q个操作: (1)区间乘 (2)单点除(保证整除) (3)区间求和对m取模 要求回答所有操作(3)的结果 主要是除法难办, 假设单点除$x$, $x$中与$m$互素的素因子可 ...
- Candies CodeForces - 991C(二分水题)
就是二分暴力就好了 为什么要记下来 呵呵....emm你说为什么... 行吧 好吧 我一直以为我的二分出问题了 原来不是 依旧很帅 统计的时候求的减了多少次 然后用次数乘了mid 这样做会使那个人获 ...
随机推荐
- gcc链接,去掉不用的函数和data
编译代码的时候,没有用到的函数经常会被编译,该怎么才能去掉呢? 解决方法: CFLAGS 加上 -ffunction-sections -fdata-sections LDFLAGS 加上 --gc- ...
- Spring Cloud Eureka 注册中心集群搭建,Greenwich 最新版!
Spring Cloud 的注册中心可以由 Eureka.Consul.Zookeeper.ETCD 等来实现,这里推荐使用 Spring Cloud Eureka 来实现注册中心,它基于 Netfl ...
- Python基础(文件操作)
文件读取: #文件读取方式一 f=open("a.txt","r+",encoding="utf8") data=f.read() prin ...
- Python + PyQt5 实现美剧爬虫可视工具(二)
美剧<权力的游戏>终于开播最后一季了,在上周写了个简单的可视化美剧的爬虫软件来爬取美剧,链接:https://www.cnblogs.com/weijiutao/p/10614694.ht ...
- colly源码学习
colly源码学习 colly是一个golang写的网络爬虫.它使用起来非常顺手.看了一下它的源码,质量也是非常好的.本文就阅读一下它的源码. 使用示例 func main() { c := coll ...
- springboot~yml里的自定义配置
主要介绍三种,字符串配置,数组配置和带默认值的配置 字符串配置 //yml setString: hello /** * 字符串. */ @Value("${setString}" ...
- 使用Atlas进行元数据管理之Glossary(术语)
背景:笔者和团队的小伙伴近期在进行数据治理/元数据管理方向的探索, 在接下来的系列文章中, 会陆续与读者们进行分享在此过程中踩过的坑和收获. 元数据管理系列文章: [0] - 使用Atlas进行元数据 ...
- centos7+rsyslog+loganalyzer+mysql 搭建rsyslog日志服务器
一.简介 在centos7系统中,默认的日志系统是rsyslog,它是一类unix系统上使用的开源工具,用于在ip网络中转发日志信息,rsyslog采用模块化设计,是syslog的替代品. 1.rsy ...
- 【资料总结】| Deep Reinforcement Learning 深度强化学习
在机器学习中,我们经常会分类为有监督学习和无监督学习,但是尝尝会忽略一个重要的分支,强化学习.有监督学习和无监督学习非常好去区分,学习的目标,有无标签等都是区分标准.如果说监督学习的目标是预测,那么强 ...
- DSAPI官方QQ群
DSAPI官方QQ群 请加主群,若主群成员已满,请加分群. 群内除常规的.NET技术交流外,也负责DSAPI的使用技术支持和更新通知. 『VB.NET/C#编程』主群 ...