poj 2115 Looooops
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 23637 | Accepted: 6528 |
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 < 2k) modulo 2k.
Input
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
Source
#include<iostream>
#include<stdio.h>
using namespace std;
long long pow(long long k)
{
long long ans=;
for(int i=;i<k;i++)
ans*=;
return ans;
}
long long ext_gcd(long long a,long long b,long long *x,long long *y)
{
if(b==)
{
*x=,*y=;
return a;
}
long long r = ext_gcd(b,a%b,x,y);
long long t = *x;
*x = *y;
*y = t - a/b * *y;
return r;
}
int main()
{
long long a,b,c,k;
while(~scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k))
{
if((a+b+c+k)==) break;
long long x,y;
long long _gcd_ = ext_gcd(c,pow(k),&x,&y);
if((b-a)%_gcd_)
{
printf("FOREVER\n");
continue;
}
long long tmp_ans = x*(b-a)/_gcd_;
long long T = pow(k)/_gcd_;/*总结一下: b/gcd是 ax+by = k*gcd中,x*k/gcd的周期*/
long long ans = (tmp_ans%T+T)%T;
printf("%I64d\n",ans);
}
return ;
}
poj 2115 Looooops的更多相关文章
- 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 (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(模线性方程)
http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...
- POJ 2115 C Looooops(Exgcd)
[题目链接] http://poj.org/problem?id=2115 [题目大意] 求for (variable = A; variable != B; variable += C)的循环次数, ...
- poj 2115 C Looooops——exgcd模板
题目:http://poj.org/problem?id=2115 exgcd裸题.注意最后各种%b.注意打出正确的exgcd板子.就是别忘了/=g. #include<iostream> ...
- 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
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19536 Accepted: 5204 Descr ...
- poj 2115 C Looooops 扩展欧几里德
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23616 Accepted: 6517 Descr ...
随机推荐
- Velocity 基本语法
Velocity 基本语法 Velocity 是一个基于 Java 的模板引擎框架,提供的模板语言可以使用在 Java 中定义的对象和变量上.Velocity 是 Apache 基金会的项目,开发的目 ...
- html 中绑定事件 this的指向
var m=function(){ alert(2); } var obj={ A:function(){ }, m:function(){ ...
- How can I fix “Compilation unit name must end with .java, or one of the registered Java-like extensions”?
How can I fix “Compilation unit name must end with .java, or one of the registered Java-like extensi ...
- rsync使用
1)拷贝本地文件.当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式. 如:rsync -a ./test.c /backup 2)使用一个远程 ...
- [Android Pro] AOSP download
Ubuntu14.04系统下载Android源码,直接上步骤: 清华大学 TUNA 镜像源 https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/ https: ...
- [Android] 查看Android中的AlarmManager事件
reference to : https://segmentfault.com/a/1190000000404684 有时候我们需要设置一个alarmmanager事件 但是如果这个事件的时间是凌晨三 ...
- [Android Pro] AES加密
reference to :http://blog.csdn.net/wfung_kwok/article/details/7766427 package com.secufity.aes; impo ...
- chrome进入控制台时自动进入断点模式的解决方法
简单粗暴,不知道为什么,去掉那个√就好了
- java 设置允许ajax XMLHttpRequest 请求跨域访问
怎样才能算跨域?协议,域名,端口都必须相同,才算在同一个域. 方案1: 使用XMLHttpRequest... 异步请求不能跨域访问,除非要访问的网页响应头信息设置为允许跨域访问. 将网页设置为允许 ...
- hdu1141(二进制数位,二分,打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1141 题意:××公司是制造computer的,1960年它造的computer是4bit的,之后每10 ...