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. 洛谷 P2984 [USACO10FEB]给巧克力Chocolate Giving

    题目描述 Farmer John is distributing chocolates at the barn for Valentine's day, and B (1 <= B <= ...

  2. EL1008E: Property or field 'timestamp' cannot be found on object of type 'java.util.HashMap

    2018-06-22 09:50:19.488  INFO 20096 --- [nio-8081-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : ...

  3. python基础一 day11 装饰器复习

    # 复习# 讲作业# 装饰器的进阶 # functools.wraps # 带参数的装饰器 # 多个装饰器装饰同一个函数# 周末的作业 # 文件操作 # 字符串处理 # 输入输出 # 流程控制 # 装 ...

  4. Repbase library|divergence rate|self-sequence alignment|genomic rearrangement|cutoffs|breakpoint

    (Panda, dog and human repeat comparison):与其他动物比较重复序列 我们使用Repbase 库(重复序列库)+已知的转录原件序列+识别软件,评估出转录原件占比,并 ...

  5. Ubuntu下Hyperledger Fabric v0.6安装部署

    系统环境:虚拟机VMware Workstation中的Ubuntu 16.04LTS 1.环境准备 1.1安装Docker Docker安装命令: curl –fsSL https://get.do ...

  6. shell脚本,计算输入给定的数,判断最大值,最小值,总和?

    [root@localhost ~]# cat five.sh #!/bin/bash #任意输入5个数,判断最大值,最小值,总和 s= read -p "please input:&quo ...

  7. 20171201Jsp Jstl详细配置

    Jsp Jstl详细配置 1. 下载包 http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/jakarta-taglibs ...

  8. break、continue、exit、return的区别和对比

    break.continue.exit.return的区别和对比 一:说明 break.continue在条件循环语句及循环语句(for.while.if等)中用于控制程序的走向:而exit则用于种植 ...

  9. Linux 特殊权限位简介

    suid.guid suid 针对二进制命令或者程序,让其拥有root或者属主权限执行 一.setsuid : chmod 4755 二进制命令或者程序 chmod u+s   二进制命令或者程序 示 ...

  10. Bin Paking Problem:简单的构造性算法

    *本文主要记录和分享学习到的知识,算不上原创 *参考文献见链接 目录 BL和BLF算法 BF算法 HR算法 PH算法