Codeforces #305 C. Mike and Frog 数论
2016-03-01 14:09
330 查看
题目
题目链接:http://codeforces.com/problemset/problem/548/C题目来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105944#problem/L
简要题意:求俩同余方程是否会同时成立,如果可以,求出最小的那个成立的值。
题解
首先去判断两个方程是否能够成立,次数的上限是mm。然后去算回到自己的循环节,就是达到aa后多久再回到aa。
然后最后根据已有的信息去暴力加上循环节然后去判断,上限还是mm。
题目相当坑,做起来要考虑许多边界的情况。
代码
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <stack> #include <queue> #include <string> #include <vector> #include <set> #include <map> #define fi first #define se second using namespace std; typedef long long LL; typedef pair<int,int> PII; // head int mul(int h, int x, int y, int m) { return (LL(x) * h + y) % m; } int findPeriod(int h, int a, int x, int y, int m) { for (int i = 1; i <= m; i++) { h = mul(h, x, y, m); if (h == a) return i; } return 0; } LL solve(LL rem1, int rem2, int per1, int per2, int m) { for (int i = 0; i <= m; i++) { if (rem1 >= rem2 && (rem1-rem2) % per2 == 0) return rem1; rem1 += per1; } return -1; } int main() { int m, x1, y1, h1, a1, x2, y2, h2, a2; scanf("%d", &m); scanf("%d%d%d%d", &h1, &a1, &x1, &y1); scanf("%d%d%d%d", &h2, &a2, &x2, &y2); int rem1 = findPeriod(h1, a1, x1, y1, m); int rem2 = findPeriod(h2, a2, x2, y2, m); if (!rem1 || !rem2) { puts("-1"); return 0; } int per1 = findPeriod(a1, a1, x1, y1, m); int per2 = findPeriod(a2, a2, x2, y2, m); if (rem1 > rem2) { swap(rem1, rem2); swap(per1, per2); } if (rem1 == rem2 || (per1 && (rem2-rem1)% per1 == 0)) { printf("%d\n", rem2); } else if (!per1 || !per2) { puts("-1"); } else { printf("%I64d\n", solve(rem1, rem2, per1, per2, m)); } return 0; }
相关文章推荐
- 60条Android日常开发总结的技术经验
- 5.5 单例模式实例
- CALayer3-层的属性
- 【maven】maven查找jar的步骤
- 简单理解Java中的类、对象、继承、多态等概念
- CSS兼容性
- 自我介绍
- Android提供的支持不同屏幕大小的全部方法
- android之JNI开发步骤总结
- JSON对象和字符串互转
- jQuery.data()方法
- get提交中文乱码问题
- MySQL常用时间函数
- 2016-03-01 地图定位 以及失败
- Android APP 开发项目框架搭建
- NGUI点透问题
- ZOJ1041
- 阻止ie、chrome中的事件冒泡和监听函数的覆盖问题
- 个人简介
- beanstalkd----安装启动