【poj 2115】 C Looooops 【CTU Open 2004】
来源:互联网 发布:咨询公司面试数据分析 编辑:程序博客网 时间:2024/06/14 07:52
Description
A Compiler Mystery: We are given a C-language style for loop of type
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.
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.
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
这道题相当于求ax=b(mod n)的最小解(对于这题,a=C,b=B-A,n=2k),使用扩展欧几里得算法可以得到解,下面是程序:
#include<stdio.h>#include<iostream>#define ll long longusing namespace std;ll read(){ll s=0;char c=getchar();while(c<'0'||c>'9'){c=getchar();}while(c>='0'&&c<='9'){s*=10;s+=c-'0';c=getchar();}return s;}bool in(ll &a,ll &b,ll &c,ll &k){a=read();b=read();c=read();k=read();return a|b|c|k;}ll gcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll d=gcd(b,a%b,x,y);ll tp=x;x=y;y=tp-a/b*y;return d;}void out(ll x){if(x>9){out(x/10);}putchar(x%10+'0');}int main(){ll a,b,c,k;while(in(a,b,c,k)){ll x,y,tp=b-a,n=1ll<<k;ll d=gcd(c,n,x,y);if(tp%d){printf("FOREVER\n");}else{x=(x*(tp/d))%n;out((x%(n/d)+n/d)%(n/d));putchar('\n');}}return 0;}
阅读全文
0 0
- 【poj 2115】 C Looooops 【CTU Open 2004】
- poj 2115 C Looooops
- POJ 2115 C Looooops
- POJ 2115 C Looooops
- POJ 2115 C Looooops
- POJ 2115 C Looooops
- POJ 2115 C Looooops
- POJ 2115 C Looooops
- poj 2115 C Looooops
- POJ 2115 (C Looooops)
- poj 2115 C Looooops
- POJ 2115 C Looooops
- POJ 2115 C Looooops
- poj 2115 C Looooops
- POJ 2115 C Looooops
- POJ 2115 C Looooops
- POJ 2115 C Looooops
- POJ 2115 C Looooops
- 14.leetCode500:keyboard row
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B. Train Seats Reservation
- ASP.NET Razor
- eclipse创建maven版的web工程
- POJ 1141 Brackets Sequence
- 【poj 2115】 C Looooops 【CTU Open 2004】
- 【SQL Server学习笔记】14:查漏补缺1(三值谓词,范式,主键/唯一/外键/检查/默认约束)
- Java-二分法查找算法
- codevs 2022 复仇 快速幂 解题报告
- Bazinga
- 线段树(简单模板)
- bzoj 1082: [SCOI2005]栅栏(二分+DFS)
- codevs 1225 八数码难题 搜索+Hash 解题报告
- 重温Servlet学习笔记--session对象