博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2115:C Looooops
阅读量:6260 次
发布时间:2019-06-22

本文共 2026 字,大约阅读时间需要 6 分钟。

C Looooops
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19536   Accepted: 5204

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 < 2
k) modulo 2
k

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 < 2
k) 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 163 7 2 167 3 2 163 4 2 160 0 0 0

Sample Output

0232766FOREVER

题意是问在

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

这种情况下,循环多少次。

其中所有的数要mod 2的k次方。所以方程就是(A+C*x)%(2^k)=B,变换一下就是-C*x+(2^k)*y=A-B。解这个方程的最小正数x即可。

又是扩展欧几里德。

代码:

#include 
#include
#include
#include
#include
using namespace std;long long yue;void ex_gcd(long long a,long long b, long long &xx,long long &yy){ if(b==0) { xx=1; yy=0; yue=a; } else { ex_gcd(b,a%b,xx,yy); long long t=xx; xx=yy; yy=t-(a/b)*yy; }}int main(){ long long A,B,C,k,k2,xx,yy; while(scanf_s("%lld%lld%lld%lld",&A,&B,&C,&k)) { if(!A&&!B&&!C&&!k) break; k2=(1LL<

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/lightspeedsmallson/p/4785867.html

你可能感兴趣的文章
PCA人脸识别学习笔记---代码篇
查看>>
grep
查看>>
归档备份被删,GoldenGate无法抽取数据
查看>>
Could not create the view: An unexpected exception was thrown.
查看>>
codeforces 445A
查看>>
基础语法
查看>>
多线程和CPU的关系
查看>>
005 -- DuLinkList_add nodes, delete node, Caser print..
查看>>
HashMap、TreeMap、LinkedHashMap、hashtable的区别
查看>>
ajax之百度 应用实例
查看>>
单行文本溢出、多文本溢出
查看>>
yarn的学习-2-从 npm 迁移到 yarn-包管理工具
查看>>
vagrant特性——基于docker开发环境(docker和vagrant的结合)-1-基本使用
查看>>
SQL 学习——简序以及学习路线
查看>>
Quoit Design
查看>>
iOS---后台运行机制详解
查看>>
python-装饰器的最终形态和固定格式 语法糖
查看>>
iphone配置实用工具iPhone Configuration Utility
查看>>
Centos搭建开发环境,PHP7+ Nginx1.12+ Mysql5.7
查看>>
RSA的密钥把JAVA格式转换成C#的格式
查看>>