POJ C Looooops
Description
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
< 2 k) are the parameters of the loop.
The input is finished by a line containing four zeros.
Output
Sample Input
3 3 2 16
3 7 2 16
7 3 2 16
3 4 2 16
0 0 0 0
Sample Output
0
2
32766
FOREVER
注意基础知识:http://blog.csdn.net/u014665013/article/details/50858292
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
LL exgcd(LL a,LL b,LL &x,LL &y) ///返回最大公约数
{
if(b==0)
{
x=1;
y=0;
return a;
}
LL r=exgcd(b,a%b,x,y);
// cout<<"x="<<x<<" y="<<y<<endl;
LL t=x;
x=y;
y=t-a/b*y;
return r;
}
int main (){ LL A,B,C,k;
while(~scanf("%I64I64d%I64d%I64d%I64d",&A,&B,&C,&k)&&!(A==0&&B==0&&C==0&&k==0)){
LL mmax=1LL<<k;
LL a=C,b=mmax,c=(B-A+mmax)%mmax; ///注意这里的b能通过是-mmax,因为这样到后面再求大于0的最小值就不好求了 但是也可以,下面的就要改了如下注释
LL x,y;
LL gcd = exgcd(a,b,x,y);
if(c==0) {
printf("%d\n",0);
continue;
}
if(c%gcd==0){
x=x*(c/gcd);
LL r = b/gcd;
x=(x%r+r)%r; ///最小的正值 <span style="font-family: Arial, Helvetica, sans-serif;">x=-(x%r-r)%r</span>
printf("%I64d\n",x);
}
else{
printf("FOREVER\n");
}
}
return 0;
}
POJ C Looooops的更多相关文章
- poj 2115 Looooops
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23637 Accepted: 6528 Descr ...
- POJ - 2115C Looooops 扩展欧几里得(做的少了无法一眼看出)
题目大意&&分析: for (variable = A; variable != B; variable += C) statement;这个循环式子表示a+c*n(n为整数)==b是 ...
- POJ 2115 C Looooops
扩展GCD...一定要(1L<<k),不然k=31是会出错的 .... C Looooops Time Limit: 1000MS Mem ...
- Poj 2115 C Looooops(exgcd变式)
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22704 Accepted: 6251 Descripti ...
- 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( ...
- POJ 2115:C Looooops
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19536 Accepted: 5204 Descr ...
- POJ 2115 C Looooops(模线性方程)
http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...
- 【题解】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 ...
- POJ 2115 C Looooops(Exgcd)
[题目链接] http://poj.org/problem?id=2115 [题目大意] 求for (variable = A; variable != B; variable += C)的循环次数, ...
随机推荐
- 上下文菜单项(contextMenu)----长按按钮弹出菜单项
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- 安装64位mysql5.626
计算机--右击属性--左上高级系统变量---环境变量 path 添加 mysql 的bin目录 ;D:\mysqlwinx64\bin1 //mysql 5.6.26安装前先解压到d盘根目录 cd D ...
- C语言基础--函数
函数概念: 1. C语言程序是由函数组成 2. 什么是函数? 函数就是一段具备特定功能的程序段 定义函数的目的: 定义函数的目的: 将一个功能封装以来方便复用 不使用函数的弊端: 1.重复代码太多, ...
- Sql 注意点
1. Set.Select赋值 使用SELECT语句来替代SET命令的主要优点是:可以在一个操作内同时给多个变量赋值.执行下面的SELECT语句,通过SELECT语句赋值的变量就可以用于任何操作了. ...
- 字符串中带有emoji表情处理
1:先删除字符然后解析当前字符再显示 edit.addTextChangedListener(new TextWatcher() { @Override public void beforeTextC ...
- SPFA算法学习笔记
一.理论准备 为了学习网络流,先水一道spfa. SPFA算法是1994年西南交通大学段凡丁提出,只要最短路径存在,SPFA算法必定能求出最小值,SPFA对Bellman-Ford算法优化的关键之处在 ...
- Gradle for Android
1.project vs project.rootProject 2.System.console() != null ? System.console().readPassword("\n ...
- C++虚函数示例
和Java不同,CDerive derive语句可以直接生成对象,不需要new关键字 重载虚函数才可以用父类引用调用子类对象,重载普通函数没有效果 #include<iostream> # ...
- Linux的yum源的配置
yum实际上是管理的rpm软件包 只要连接网络就可以使用在线的yum源,不用配置本地yum源 网络yum源/etc/yum.repos.d/fedora.repo(Fedora)[fedora]nam ...
- iOS运行时工具-cycript
cycript是大神saurik开发的一个非常强大的工具,可以让开发者在命令行下和应用交互,在运行时查看和修改应用.它确实可以帮助你破解一些应用,但我觉得这个工具主要还是用来学习其他应用的设计(主要是 ...