C Looooops
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14765   Accepted: 3719

Description

A Compiler Mystery: We are given a C-language style for loop of type

for (variable = A; variable != B; variable += C)
statement;

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
3 7 2 16
7 3 2 16
3 4 2 16
0 0 0 0 题目大意:计算循环的次数,有a+ct=b(mod 2^k),变换下得到二元一次不定方程:ct+p*2^k=b-a;
用B=1<<k Wrong answer
正确的 B=pow(2,k+0.0);
AC代码:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std; __int64 a,b,c,A,B,C,x,y,d,t,k; __int64 Extended_Euclid(__int64 a,__int64 b,__int64 &x,__int64 &y)
{
__int64 d,t;
if(b==)
{
x=;y=;
return a;
}
d=Extended_Euclid(b,a%b,x,y);
t=x;
x=y;
y=t-a/b*y;
return d;
} int main()
{
while(scanf("%I64d %I64d %I64d %d",&a,&b,&c,&k),a+b+c+k)
{
if (a==b) {cout<<<<endl; continue;}
else if (c==) {cout<<"FOREVER"<<endl; continue;}
A=c;
C=b-a;
B=pow(,k+0.0);
d=Extended_Euclid(A,B,x,y);
if(C%d)
printf("FOREVER\n");
else
{
t=B/d;
x=x*C/d;
x=(x%t+t)%t;
printf("%I64d\n",x);
}
}
return ;
}

poj 2115 二元一次不定方程的更多相关文章

  1. poj 1061 青蛙的约会(二元一次不定方程)

      Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要 ...

  2. P5656 【模板】二元一次不定方程(exgcd)

    还不会 exgcd 的请移步窝的学习笔记,这里只讲怎么搞出烦人的答案. 在 \(a,b\) 两者互质的情况下,二元一次不定方程的通解:\(a(x+db)+b(y+da)=c\). 所以要先将 \(a, ...

  3. uva 10090 二元一次不定方程

    Marbles Input: standard input Output: standard output I have some (say, n) marbles (small glass ball ...

  4. exgcd、二元一次不定方程学习笔记

    (不会LATEX,只好用Word) ( QwQ数论好难) 再补充一点,单次询问a,b求逆元的题可以直接化简然后套用exgcd求解. 例题:https://www.luogu.org/problemne ...

  5. POJ 2115 C Looooops(扩展欧几里得应用)

    题目地址:POJ 2115 水题. . 公式非常好推.最直接的公式就是a+n*c==b+m*2^k.然后能够变形为模线性方程的样子,就是 n*c+m*2^k==b-a.即求n*c==(b-a)mod( ...

  6. 【题解】POJ 2115 C Looooops (Exgcd)

    POJ 2115:http://poj.org/problem?id=2115 思路 设循环T次 则要满足A≡(B+CT)(mod 2k) 可得 A=B+CT+m*2k 移项得C*T+2k*m=B-A ...

  7. POJ 2115 C Looooops(Exgcd)

    [题目链接] http://poj.org/problem?id=2115 [题目大意] 求for (variable = A; variable != B; variable += C)的循环次数, ...

  8. POJ 2115 C Looooops(模线性方程)

    http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...

  9. poj 2115 C Looooops——exgcd模板

    题目:http://poj.org/problem?id=2115 exgcd裸题.注意最后各种%b.注意打出正确的exgcd板子.就是别忘了/=g. #include<iostream> ...

随机推荐

  1. 我用ABAP做过的那些无聊的事情

    国庆大假马上就要来临了,我们聊点轻松的话题,关于假期. Jerry的成都同事李贝宁(Li Ben), <SAP成都研究院李三郎:SCP Application Router简介>的作者,有 ...

  2. MVC视图特性

    在主界面的视图中可以使用viewdata,引用主界面的分布视图界面也可以调用主界面的分部视图,但是分部视图不可以定义viewdata并使用 例子如下: // // GET: /Home/ public ...

  3. C#中加锁问题

    今天在工作中遇到了一个问题 当我使用多线程访问同一个方法资源时,为了不对结果进行冲突于是加了个死锁,还遇到了一些坑,特此来进行一些记录 static object obj=new object(); ...

  4. 给我说说你能想到几种分布式session实现

    附录: https://mp.weixin.qq.com/s/8Hh4j0CjfF5S8zM29JZl2w # 面试官心理分析 面试官问了你一堆 dubbo 是怎么玩儿的,你会玩儿 dubbo 就可以 ...

  5. Asp.Net Core 入门(九)—— 环境变量 TagHelper

    我们在之前讲Program.cs文件做了什么的时候,提到启动CreaeDefaultBuilder会获取环境变量来做一些判断之类的操作.那么我们的Taghelper也可以使用“ASPNETCORE_E ...

  6. SSIS 通过 WINscp 从SFTP下载文件

    1.通过SSIS的process task调用 winscp :C:\Program Files (x86)\WinSCP\WinSCP.exe /script="C:\SFTPFile\T ...

  7. bootstrap下拉菜单(Dropdowns)

    本章将重点讲解bootstrap下拉菜单(Dropdowns),下拉菜单是可切换的,是以列表格式显示链接的上下文菜单. <!DOCTYPE html><html><hea ...

  8. java在线聊天项目 swt可视化窗口Design 登录框注册按钮点击改变窗口大小——出现注册面板 实现打开登录框时屏幕居中

    登录框注册按钮点击改变窗口大小——出现注册面板  首先用swt可视化设计登录窗口如下图: 此时窗口高度为578 没点击注册时高度为301(可自己定) 注意:注册用户的Jpanel 的border选择T ...

  9. 高度自适应的bug

    今天在整理之前IFEde作业,发现有个简历的效果好像没实现.于是想把样式改成作业要求的那样. 作业要求是这样的: 右边栏昨晚高度是839px,我想把左边栏做成高度自适应的.但是没成功.现在我把这个问题 ...

  10. 【mysql】 load local data infield 报错 ERROR 1148 (42000): The used command is not allowed with this MySQL version

    mysql> load data local infile '/Users/flint/learn/mysql/pet' into table bx_pet; 执行报错 ERROR 1148 ( ...