题意

题目链接

求满足\(i^2 + j^2 \% M = 0\)的数对\((i, j)\)的个数,\(1 \leqslant i, j \leqslant 10^9, M \leqslant 1000\)

Sol

发这篇博客的目的就是为了证明一下我到底有多菜。

mdzz小学组水题我想了40min都没想出来。这要是出在NOIP 2019的话估计我又要做不出Day1T1了。。

\(i^2 + j^2 \% M = i \% M * i \% M + j \% M * j \% M\)

枚举剩余系即可

此题完结

  1. /*
  2. */
  3. #include<bits/stdc++.h>
  4. #define Pair pair<int, int>
  5. #define MP(x, y) make_pair(x, y)
  6. #define fi first
  7. #define se second
  8. #define int long long
  9. #define LL long long
  10. #define rg register
  11. #define pt(x) printf("%d ", x);
  12. #define Fin(x) {freopen(#x".in","r",stdin);}
  13. #define Fout(x) {freopen(#x".out","w",stdout);}
  14. using namespace std;
  15. const int MAXN = 1e6 + 10, INF = 1e9 + 10, mod = 1e9 + 7;
  16. const double eps = 1e-9;
  17. inline int read() {
  18. char c = getchar(); int x = 0, f = 1;
  19. while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
  20. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  21. return x * f;
  22. }
  23. int N, M, vis[3001][3001];
  24. int get(int l, int r) {
  25. return (r - l) / M + (l > 0);
  26. }
  27. main() {
  28. N = read(); M = read();
  29. int ans = 0;
  30. for(int i = 1; i <= M; i++) {
  31. for(int j = 1; j <= M; j++) {
  32. if((i * i + j * j) % M == 0) {
  33. vis[i % M][j % M] = 1;
  34. }
  35. }
  36. }
  37. for(int i = 0; i <= min(N, M); i++)
  38. for(int j = 0; j <= min(N, M); j++)
  39. if(vis[i][j] && ((i * i + j * j) % M == 0))
  40. (ans += get(i, N) * get(j, N));
  41. cout << ans;
  42. return 0;
  43. }
  44. /*
  45. */

cf1056B. Divide Candies(数论 剩余系)的更多相关文章

  1. Divide Candies CodeForces - 1056B (数学)

    Arkady and his friends love playing checkers on an n×nn×n field. The rows and the columns of the fie ...

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

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

  3. [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<= ...

  4. 数论剩余系——cf1089F

    关于模和互质,很好的题目 /* n两个质因子 x,y有 ax+by=n-1 ax+by=n-1 ax+1+by=n y|ax+1 gcd(x,y)=1 ax%y,a取[1,y-1],就会有[1,y-1 ...

  5. Mail.Ru Cup 2018 Round 3 Solution

    A. Determine Line Water. #include <bits/stdc++.h> using namespace std; ]; int main() { while ( ...

  6. 【CSP-J 2021】总结

    前言:程不在长,能过则行.码不在多,无虫则灵.斯是信竞,惟吾爆零.线段维护快,树状跳的勤.数论剩余系,图论前向星.无数竞之推理,无物竞之劳形.大佬楼教主,超奆姚期智,神犇云:您太强了. 早上5:00就 ...

  7. poj---(2886)Who Gets the Most Candies?(线段树+数论)

    Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 10373   Acc ...

  8. 数论算法 剩余系相关 学习笔记 (基础回顾,(ex)CRT,(ex)lucas,(ex)BSGS,原根与指标入门,高次剩余,Miller_Rabin+Pollard_Rho)

    注:转载本文须标明出处. 原文链接https://www.cnblogs.com/zhouzhendong/p/Number-theory.html 数论算法 剩余系相关 学习笔记 (基础回顾,(ex ...

  9. ACM学习历程—HDU5407 CRB and Candies(数论)

    Problem Description CRB has N different candies. He is going to eat K candies.He wonders how many co ...

随机推荐

  1. java连接SqlServer2012

    要用java连接数据库  首先是要通过JDBC驱动 要先去下载一个sqljdbc4.jar,我这里放百度云盘了, 下载地址:链接:http://pan.baidu.com/s/1slJl89B 密码: ...

  2. ThreadLocal系列(三)-TransmittableThreadLocal的使用及原理解析

    ThreadLocal系列(三)-TransmittableThreadLocal的使用及原理解析 上一篇:ThreadLocal系列(二)-InheritableThreadLocal的使用及原理解 ...

  3. 【算法笔记】A1063 Set Similarity

    1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be ...

  4. C# 并发队列ConcurrentQueue

    测试函数 static async Task RunProgram() { var taskQueue = new ConcurrentQueue<CustomTask>(); var c ...

  5. javascript的简单查询和插入和修改

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  6. 剑指offer——面试题27:二叉树的镜像

    函数递归 void MirrorIteratively(BinaryTreeNode* pRoot) { if(pRoot == nullptr) return; std::stack<Bina ...

  7. redis 实现消息发布和订阅

    1,打开二个客户端机器 一个用于发布,一个用于接受 2,发布一个channel1 3,用另外一个客户端收听上面的客户端 4,当再次在发布的redis客户端 发布一个消息  其他所有订阅的客户端会自动收 ...

  8. 【数组】Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  9. Jmeter 建立数据库测试计划

    建立数据库测试计划(Building a Database Test Plan) 在本节中,将学习如何创建测试数据库服务器一个简单的测试计划.您将创建五十个用户向数据库服务器发送2个SQL请求.并且, ...

  10. django+mysql+html简单demo之 views+html

    #coding=utf-8 from __future__ import unicode_literals from django.shortcuts import render,render_to_ ...