POJ2115 C Looooops(数论)
题目链接。
分析:
数论了解的还不算太多,解的时候,碰到了不小的麻烦。
设答案为x,n = (1<<k), 则 (A+C*x) % n == B
即 (A+C*x) ≡ B (mod n)
化简得 C*x ≡ (B-A) (mod n)
设 a = C, b = (B-A)
则原式变为 ax=b.解 x。
到这里,以为求出来 a 的逆, 然后 x = b*a-1。
a 的 逆好求,用《训练指南》上的模板(P122) inv函数.
例如,求 2 模 66536 下的逆, inv(2, 66536) 便可, 但 inv(LL a, LL n) 这个函数的要求就是如果 a 和 n 的最大公约数不为1,则逆不存在。 但样例是有解的。
在网上查了一下,说是《算法导论》(第三版)P555页有解法, 便根据书上的解法解 x 了。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <set> using namespace std; typedef long long LL; void gcd(LL a, LL b, LL &d, LL &x, LL &y) {
if(!b) { d = a; x = ; y = ; }
else { gcd(b, a%b, d, y, x); y-= x*(a/b); }
} int main() {
LL A, B, C, k, n, x, y, d; while(cin >> A >> B >> C >> k) {
if(A == && B == && C == && k == ) break; n = (1LL << k); LL a = C;
LL b = (B-A+n)%n; if(b == ) { printf("0\n"); continue; } gcd(a, n, d, x, y); if(b % d == ) {
LL x0 = (x*(b/d)) % n;
LL minn = (x0%(n/d)+n/d)%(n/d);
cout << minn << endl;
}
else printf("FOREVER\n");
} return ;
}
POJ2115 C Looooops(数论)的更多相关文章
- poj2115 C Looooops(exgcd)
poj2115 C Looooops 题意: 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束. 若在有限次内结束,则输出循环次数. 否则输出死循环. ...
- [暑假集训--数论]poj2115 C Looooops
A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; variable != ...
- POJ2115——C Looooops(扩展欧几里德+求解模线性方程)
C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...
- POJ2115 C Looooops[扩展欧几里得]
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24355 Accepted: 6788 Descr ...
- POJ2115 C Looooops 扩展欧几里德
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2115 题意 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次 ...
- POJ2115:C Looooops(一元线性同余方程)
题目: http://poj.org/problem?id=2115 要求: 会求最优解,会求这d个解,即(x+(i-1)*b/d)modm;(看最后那个博客的链接地址) 前两天用二元一次线性方程解过 ...
- poj2115 C Looooops
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29262 Accepted: 8441 Descr ...
- POJ2115 C Looooops ——模线性方程(扩展gcd)
题目链接:http://poj.org/problem?id=2115 C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ2115 C Looooops(线性同余方程)
无符号k位数溢出就相当于mod 2k,然后设循环x次A等于B,就可以列出方程: $$ Cx+A \equiv B \pmod {2^k} $$ $$ Cx \equiv B-A \pmod {2^k} ...
随机推荐
- 工作于内存和文件之间的页缓存, Page Cache, the Affair Between Memory and Files
原文作者:Gustavo Duarte 原文地址:http://duartes.org/gustavo/blog/post/what-your-computer-does-while-you-wait ...
- Android Camera开发:使用GLSurfaceView预览Camera 基础拍照
GLSurfaceView是OpenGL中的一个类,也是可以预览Camera的,而且在预览Camera上有其独到之处.独到之处在哪?当使用Surfaceview无能为力.痛不欲生时就只有使用GLSur ...
- noip 2012 疫情控制
/* 考试的时候没想出正解 也没打暴力 时间不够了 随便yy了几种情况按出现的先后顺序处理而没有贪心 的了20分 不粘了 正解是围绕首都的儿子来搞的 显然先二分答案 对于每个限定的最大时间 我们尝试着 ...
- ACCSESS数据库导入到SQL SEVERES2005
首先打开Access数据库然后选择一张表,右击选择要导入数据库的类型(此处已sql2005为例) 然后选择新建 点击下一步 选择导入数据库类型(sql) 输入一个名称,在前面能用到(此处建的是250) ...
- 分析器错误消息: 未能加载文件或程序集“System.WEB.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。系统找不到指定的文件。
分析器错误消息: 未能加载文件或程序集“System.WEB.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=3 ...
- Jquery小东西收集
1. $(document).ready(),$(function(){}),$(window).load(),window.onload的关系与区别 $(document).ready(functi ...
- 2015-09-21CSS:引入方式、选择器、注释、文字样式
1.HTML中引入CSS的方式 HTML中引入CSS的样式有4种:行内式.内嵌式.导入式和链接式. ⑴行内式 行内式是在标记的style属性中设定CSS样式.这种方式没有体现出CSS的优势,不推荐使用 ...
- BaseBean构造
package cn.jsonlu.passguard.model; import cn.jsonlu.passguard.utils.MD5Util; import com.fasterxml.ja ...
- php创建读取 word.doc文档
创建文档; <?php $html = "this is question"; for($i=1;$i<=3;$i++){ $word = new word(); $w ...
- Mavne + Spring整合CXF
http://blog.csdn.net/xiongyu777888/article/details/23787615(没毛病) http://blog.csdn.net/hbsong75/artic ...