POJ-2115 E - C Looooops
来源:互联网 发布:韩火火淘宝店的模特 编辑:程序博客网 时间:2024/06/14 03:52
原题链接
C Looooops
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 22250 Accepted: 6123
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
C * X + 2^K * Y = B - A,只要求出x的最小正整数解即可
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define INF 1e9using namespace std;typedef long long ll;void gcd(ll a, ll b, ll &x, ll &y, ll &d){if(b == 0){x = 1;y = 0;d = a;return ;}ll x1, y1;gcd(b, a % b, x1, y1, d);x = y1;y = x1 - a / b * y1;}int main(){//freopen("in.txt", "r", stdin);ll a, b, c, k;while(scanf("%I64d%I64d%I64d%I64d", &a, &b, &c, &k)){if(a == 0 && b == 0 && c == 0 && k == 0) break;ll s = (ll)1 << k, x, y, d;gcd(c, s, x, y, d);ll h = b - a;if(h % d){puts("FOREVER");continue;} x = x * h / d;ll v = s / d;x = (x % v + v) % v;printf("%I64d\n", x);}return 0;}
0 0
- POJ-2115 E - 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
- POJ 2115 C Looooops
- leetcode: Pascal's Triangle II
- 平时积累2
- Spring afterPropertiesSet方法
- MapReduce Hive Hbase 项目 优化
- 前后端分离的思考与实践(五)
- POJ-2115 E - C Looooops
- 平时积累3
- 自定义View(一)
- Boost asio
- Visual Studio 2012 编译错误【error C4996: 'scanf': This function or variable may be unsafe. 】的解决方案
- arcgis api for js共享干货系列之二自定义Navigation控件样式风格
- 一位被清华录取的学霸,给中国父母的8点忠告,惊醒了无数家长!
- JAVA泛型
- maven pom.xml报错解决