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. powershell 版本问题

    Login-AzureRmAccount : 无法将“Login-AzureRmAccount”项识别为 cmdlet.函数.脚本文件或可运行程序的名称.请检查名称的拼写,如果包括路径,请确保路径正确 ...

  2. (转)SpringMVC学习(五)——SpringMVC的参数绑定

    http://blog.csdn.net/yerenyuan_pku/article/details/72511611 SpringMVC中的参数绑定还是蛮重要的,所以单独开一篇文章来讲解.本文所有案 ...

  3. mysql利用binlog恢复数据详细例子

    模拟数据恢复的案例 有些时候脑瓜就会短路,难免会出错 场景:在生产环境中,我们搭建了mysql主从,备份操作都是在从备份数据库上 前提:有最近一天或者最近的全备 或者最近一天相关数据库的备份 最重要的 ...

  4. 根据HTML语义化编码

    语义化标签——http://www.html5jscss.com/html5-semantics-section.html 写HTML代码时应注意什么? 尽可能少的使用无语义的标签div和span: ...

  5. Codeforces Round #272 (Div. 2)-A. Dreamoon and Stairs

    http://codeforces.com/contest/476/problem/A A. Dreamoon and Stairs time limit per test 1 second memo ...

  6. Lazy Instantiator

    lazy instantiator (懒加载.延迟实例化.延迟初始化) 最开始看斯坦福的视频,对 延迟初始化 这个概念,不太理解 只见到,有些属性的初始化是在init做的,有些是在viewDidLoa ...

  7. H5新人福音~零配置搭建现代化的前端工程

    X-BUILD一套基于Webpack(v4.21.0)快速搭建H5场景开发环境的脚手架,只需要几分钟的时间就可以运行起来.X-BUILD是针对H5开发的一套自动化构建工具,致力于提升开发效率,减小开发 ...

  8. clipboard 在 vue 中的使用

    简介 页面中用 clipboard 可以进行复制粘贴,clipboard能将内容直接写入剪切板 安装 npm install --save clipboard 使用方法一 <template&g ...

  9. perl之更多的控制结构

    1.unless/if结构 unless 条件为假的时候 才执行语句块. eg: unless($fred =~ /^[A-Z_]\w*$/i){ print "The value of \ ...

  10. python--线程的其他方法

    一 . current_thread的用法 import threading import time from threading import Thread, current_thread def ...