[CSU 1803(湖南省赛16)] 2016 (数论+模运算)
2016-09-05 20:05
453 查看
CSU - 1803 (湖南省赛16)
给定 N 和 M ,问有多少对 a 和 b其中 1≤a≤N,1≤b≤M,且 a×b%2016=0
刚开始想错了,导致我花了很多时间去思考如何去重
后来看到一种特别机智的做法
a×b=(k×2016+t)×b=t×b=0 (mod2016)
所以只要枚举 amod2016的余数 t
对于每个这样的余数,找出有多少个 b 符合条件
然后对于 a 和 a+2016 的答案是一样的
这样统计就方便多了
别忘了最后减去 a=0 的情况
#pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #include <iostream> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <cctype> #include <map> #include <set> #include <queue> #include <bitset> #include <string> #include <complex> using namespace std; typedef pair<int,int> Pii; typedef long long LL; typedef unsigned long long ULL; typedef double DBL; typedef long double LDBL; #define MST(a,b) memset(a,b,sizeof(a)) #define CLR(a) MST(a,0) #define SQR(a) ((a)*(a)) #define PCUT puts("\n----------") const int maxn=2016+10; LL gcd(LL a, LL b){return b?gcd(b,a%b):a;} int N,M; LL res[maxn]; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif while(~scanf("%d%d", &N, &M)) { LL ans=0; for(int i=0; i<2016; i++) res[i] = M/(2016/gcd(2016,i)); for(int i=0; i<2016; i++) ans += N/2016*res[i]; N%=2016; for(int i=0; i<=N; i++) ans += res[i]; ans -= res[0]; cout << ans << "\n"; } return 0; }
相关文章推荐
- JZOJ3053. 【NOIP2012模拟10.25】旅行
- C++构造函数以及引用的小理解
- AtCoder Grand Contest 004 C - AND Grid(思路题)
- BZOJ 4198 荷马史诗
- 持续集成简介(转)
- node.js中获取当前时间
- 根据线程安全的相关知识,分析以下代码,当调用test方法时i>10时是否会引起死锁?并简要说明理由。
- Java NIO详解
- Java基础
- SVN操作心得
- hadoop yarn
- JavaScript阻止事件冒泡和默认事件
- Android Studio:正确导入SO相关文件
- CSU 1811 Tree Intersection(启发式合并)
- 正则表达式
- 一道留学作业题//C语言
- Unity 优化
- 图算法(1) - 基础知识
- PAT(乙级)1013 组个最小数 (20)
- Lightoj1202——Bishops(找规律)