C Looooops(欧几里德+poj2115)
来源:互联网 发布:c语言实验温度转换 编辑:程序博客网 时间:2024/05/24 05:13
H - C Looooops
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uAppoint description:
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 < 2 k) modulo 2 k.
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.
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(i=A;i!=B;i+=C){i%(2^k)};问你循环执行几次?
思路:先假设等式成立:(A+x*C)%(2^k)=B
变形(2^k)*y+B=A+C*x ==> C*x+(-(2^k)*y)=B-A;
ax+by=c
所以现在你知道怎么做了吧。哈哈!
转载请注明出处:http://www.cnblogs.com/yuyixingkong/
题目链接:http://poj.org/problem?id=2115
#include<stdio.h>#define LL unsigned long longvoid exgcd(LL a,LL b,LL& d,LL& x,LL& y){ if(!b){d=a;x=1;y=0;} else { exgcd(b,a%b,d,y,x); y-=x*(a/b); }}int main(){ LL A,B,C,k; while(scanf("%llu%llu%llu%llu",&A,&B,&C,&k),(A+B+C+k)) { LL a,b,c,d,x,y,dm; c=B-A; if(c==0){printf("0\n");continue;} a=C; b=(LL)1<<k; exgcd(a,b,d,x,y); if(c%d){ printf("FOREVER\n");continue;} dm=b/d; x=(((x*c/d)%dm)+dm)%dm; printf("%llu\n",x); } return 0;}
1 1
- C Looooops(欧几里德+poj2115)
- poj2115 C Looooops (欧几里德)
- 欧几里德 poj2115 C Looooops
- POJ2115 C Looooops(拓展欧几里德)
- poj2115 Looooops 扩展欧几里德
- [POJ2115]C Looooops(扩欧)
- poj2115 Looooops(扩展欧几里德模板)
- POJ2115-C Looooops (错误分析)
- POJ2115 C Looooops(扩展欧几里得算法)
- POJ2115:C Looooops(扩展欧几里得)
- POJ2115 C Looooops(扩展欧几里得)
- POJ2115 C Looooops
- POJ2115-C Looooops
- [POJ2115] C Looooops
- #POJ2115# C Looooops
- POJ2115-C Looooops
- POJ2115 C Looooops
- poj2115 Looooops 扩展欧几里德的应用
- QT 竖向显示文本
- List 总结
- 【算法导论】2-1-4二进制整数加法.cpp
- UVa 12563 - Jin Ge Jin Qu hao(01背包)
- C#中virtual和abstract的区别
- C Looooops(欧几里德+poj2115)
- Struts2从一个action转到另一个action的两种方法
- Xcode 6 正式版如何创建一个Empty Application
- 开发环境各类资源下载地址
- 【VR】Leap Motion 官网文档 Unity资源与插件
- 使用Pull解析Xml
- ExtJS中xtype一览
- sublime上调试php代码
- 怎么从小事中看“思维突破”