poj_2115C Looooops(模线性方程)
题目链接:http://poj.org/problem?id=2115
C Looooops
Description A Compiler Mystery: We are given a C-language style for loop of type
for (variable = A; variable != B; variable += C) I.e., a loop which starts by setting variable to value A and while variable is not equal to B, repeats statement followed by increasing the variable by C. We want to know how many times does the statement get executed for particular values of A, B and C, assuming that all arithmetics is calculated in a k-bit unsigned integer type (with values 0 <= x < 2k) modulo 2k. Input The input consists of several instances. Each instance is described by a single line with four integers A, B, C, k separated by a single space. The integer k (1 <= k <= 32) is the number of bits of the control variable of the loop and A, B, C (0 <= A, B, C < 2k) are the parameters of the loop.
The input is finished by a line containing four zeros. Output The output consists of several lines corresponding to the instances on the input. The i-th line contains either the number of executions of the statement in the i-th instance (a single integer number) or the word FOREVER if the loop does not terminate.
Sample Input 3 3 2 16 Sample Output 0 Source |
题意:定义一个循环for(int i = A ; i!=B ; i= (i+c)%2^k)
求循环执行的次数,如果死循环输出forever;
题解:上面的循环可以写成(A+C*X)%2^k=B%2^k
上式可以写成 C*X%2^k=B-A%2^k;
这样就转化成了一个模线性方程。
模线性方程有下列定理
数论:
求解模线性方程
- 定理:方程ax=b(mod n)对于未知量x有解,当且仅当gcd(a, n)|b
- 定理:方程ax=b(mod n)或者对模n有d个不同的解,其中d=gcd(a, n)或者无解。
- 定理:设d=gcd(a, n),假定对整数x’和y’,有d=ax’+ny’。如果d|b,则方程ax=b(modn)有一个解的值为x0,满足x0=x’(b/d)mod n。
- 定理:假设方程ax=b(mod n)有解(即有d|b,其中d=gcd(a, n)),x0是该方程的任意一个解,则该方程对模n恰有d个不同的解,分别为:xi=x0+i(n/d)(i = 1, 2, …, d-1)。
- int Modular_Linear(int a,int b,int n)
{
int d,x,y,x0,i;
d=Extend_Euclid(a,n,x,y);
if(b%d==0)
{
x0=(x*(b/d))%n;
if(x0<n)x0+=n;
for(i=0;i<d;i++)cout<<(x0+i*n/d)%n<<endl;
}
return 0;
}
注意:这个题要求如果有解的话输出最小解,一般在处理最小解的时候用(x%mod+mod)%mod
//求模线性方程
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long ll gcd(ll a, ll b, ll &x, ll &y)
{
if(b==) {
x = ;
y = ;
return a;
}
ll ans = gcd(b,a%b,x,y);
ll tx = x;
x = y;
y = tx-a/b*y;
return ans;
}
int main()
{
ll a,b,c,k;
while(~scanf("%lld%lld%lld%lld",&a,&b,&c,&k))
{
if(a==b&&b==c&&c==k&&k==) return ;
ll M = 1LL << k;
b = b-a;
ll m,n;
ll d = gcd(c,M,m,n);
if(b%d!=) {
puts("FOREVER");
continue;
}
ll ans = (m*(b/d))%M;
ans = (ans%(M/d)+M/d)%(M/d);
printf("%lld\n",ans);
}
return ;
}
/*
void gcd(LL a, LL b, LL &d, LL &x, LL &y) {
if(!b) { d = a; x = 1; y = 0; }
else { gcd(b, a%b, d, y, x); y-= x*(a/b); }
}
*/
poj_2115C Looooops(模线性方程)的更多相关文章
- POJ2115 C Looooops ——模线性方程(扩展gcd)
题目链接:http://poj.org/problem?id=2115 C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ2115 C Looooops 模线性方程(扩展欧几里得)
题意:很明显,我就不说了 分析:令n=2^k,因为A,B,C<n,所以取模以后不会变化,所以就是求(A+x*C)%n=B 转化一下就是求 C*x=B-A(%n),最小的x 令a=C,b=B-A ...
- POJ2115——C Looooops(扩展欧几里德+求解模线性方程)
C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...
- C Looooops(扩展欧几里得求模线性方程)
http://poj.org/problem?id=2115 题意:对于C的循环(for i = A; i != B; i+=C)问在k位存储系统内循环多少次结束: 若循环有限次能结束输出次数,否则输 ...
- POJ 2115 C Looooops(模线性方程)
http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...
- POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))
d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...
- C Looooops(扩展欧几里得+模线性方程)
http://poj.org/problem?id=2115 题意:给出A,B,C和k(k表示变量是在k位机下的无符号整数),判断循环次数,不能终止输出"FOREVER". 即转化 ...
- [ACM_其他] Modular Inverse [a关于模m的逆 模线性方程]
Description The modular modular multiplicative inverse of an integer a modulo m is an integer x such ...
- 模线性方程&&中国剩余定理及拓展
一.求解模线性方程 由ax=b(mod n) 可知ax = ny + b 就相当于ax + ny = b 由扩展欧几里得算法可知有解条件为gcd(a, n)整除d 可以直接套用扩展欧几里得算法 最终由 ...
随机推荐
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)
##机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)---#####注:机器学习资料[篇目一](https://github.co ...
- iOS设置拍照retake和use按钮为中文简体
iOS设置拍照retake和use按钮为中文简体,设置有两种方式一个是代码直接控制,第二就是xcode配置本机国际化为“china”(简体中文). 本文重点要说的是第二种,这样配置有两个好处,一是操作 ...
- bzoj 1835: [ZJOI2010]base 基站选址
Description 有N个村庄坐落在一条直线上,第i(i>1)个村庄距离第1个村庄的距离为Di.需要在这些村庄中建立不超过K个通讯基站,在第i个村庄建立基站的费用为Ci.如果在距离第i个村庄 ...
- SpringJDBC的JdbcTemplate在MySQL5.7下不支持子查询的问题
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [ SELECT ...
- Fragment生命周期及实现点击导航图片切换fragment,Demo
PS:Fragment简介 Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会 ...
- Java_Date_02_截断日期到日
oracle 的 trunc 函数能很方便的将日期截断.现在有个需求,需要用java实现与 oracle 的 trunc 函数 相同的功能. 1.需求:将日期截断到日 即 将格式为 2018-01-0 ...
- 微信小程序语音识别开发过程记录 微信小程序silk转mp3 silk转wav 以及ffmpeg使用
说说最近在开发微信小程序语音识别遇到的问题吧 最先使用微信小程序录音控件可以拿到silk格式,后来微信官方又支持mp3格式了 但是我们拿到这些格式以后,都还不能直接使用,做语音识别,因为目前百度的语音 ...
- 微信小程序参数二维码6问6答
微信小程序参数二维码[基础知识篇],从6个常见问题了解小程序参数二维码的入门知识. 1.什么是小程序参数码? 微信小程序参数二维码:针对小程序特定页面,设定相应参数值,用户扫描后进入相应的页面. 2. ...
- jQuery的get()post()getJson()方法
jQuery get() 和 post() 方法用于通过 HTTP GET 或 POST 请求从服务器请求数据. HTTP 请求:GET vs. POST 两种在客户端和服务器端进行请求-响应的常用方 ...
- C3P0配置属性
acquireIncrement:当连接池中的连接用完时,C3P0一次性创建新连接的数目: acquireRetryAttempts:定义在从数据库获取新连接失败后重复尝试获取的次数,默认为30: a ...