POJ 2115 C Looooops
扩展GCD。。。一定要(1L<<k),不然k=31是会出错的 。。。。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15444 | Accepted: 3941 |
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
Sample Output
0
2
32766
FOREVER
Source
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- using namespace std;
- typedef long long int LL;
- inline LL P(LL k){return (1LL<<k);}
- LL GCD(LL a,LL b)
- {
- return b==?a:GCD(b,a%b);
- }
- LL EX_GCD(LL a,LL b,LL& x,LL& y)
- {
- if(b==)
- {
- x=;y=;
- return a;
- }
- else
- {
- int ret=EX_GCD(b,a%b,x,y);
- int t=x;
- x=y; y=t-a/b*y;
- return ret;
- }
- }
- int main()
- {
- LL a,b,c,k;
- while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k)!=EOF)
- {
- if(a==&&b==&&c==&&k==) break;
- LL Aa=P(k),Bb=-c,Cc=a-b;
- int d=GCD(Aa,Bb);
- if(Cc%d!=)
- {
- puts("FOREVER");
- continue;
- }
- LL A=Aa/d,B=Bb/d,C=Cc/d,X,Y;
- EX_GCD(A,B,X,Y);
- X=X*C;Y=Y*C;
- if(A<) A=-A;
- printf("%I64d\n",(Y%A+A)%A);
- }
- return ;
- }
POJ 2115 C 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(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: 23616 Accepted: 6517 Descr ...
- POJ 2115 C Looooops扩展欧几里得
题意不难理解,看了后就能得出下列式子: (A+C*x-B)mod(2^k)=0 即(C*x)mod(2^k)=(B-A)mod(2^k) 利用模线性方程(线性同余方程)即可求解 模板直达车 #incl ...
- poj 2115 C Looooops(扩展gcd)
题目链接 这个题犯了两个小错误,感觉没错,结果怒交了20+遍,各种改看别人题解,感觉思路没有错误,就是wa. 后来看diccuss和自己查错,发现自己的ecgcd里的x*(a/b)写成了x*a/b.还 ...
随机推荐
- Application.DoEvents()的使用
最近做了一个个人数字图书馆管理系统,因为牵扯到电脑文件的扫描,想做一个实时显示当前扫面文件的功能,就类似于360文件扫描时的效果,本来打算用多线程来实现,但是方法太多没有实现,后来在程序中进行控制,由 ...
- Android开发环境搭建中的一些小问题记录
1.由于市场上大多数教程是基于Eclipse,而AndroidStudio显然是大势所趋,所有我在电脑上同时搭建了两个IDE,直接在官网下载AndroidStudio比较好,因为SDK,AVD都集成了 ...
- easyui使用datagrid时列名包含特殊字符导致表头与数据错位的问题
做一个用easyui的datagrid显示数据的功能时发现表格的列头与数据错位了,而且这个现象不总是能重现,一直没搞清楚原因.后来偶然在控制台看出了一点端倪: 推测表头或者单元格的class名应该是用 ...
- C++ essentials 之 static 关键字
extraction from The C++ Programming Language, 4th. edition, Bjarne Stroustrup If no initializer is s ...
- mysql简单操作(实时更新)
从表中删除某条记录: delete from table_name where xx=xxxx; 创建数据库(注意不同系统对大小写的敏感性): create database xxx; 查看数据库列表 ...
- jQuery知识点总结(第二天)
今天继续从我的笔记上面搬运.我们不产生知识,只是知识的搬运工. 内容过滤选择器: ○ 内容选择过滤器 $("div ...
- maven工程直接部署在tomcat上
http://www.cnblogs.com/guodefu909/p/4874549.html (现在用的是第三点.)
- js时间戳转成日期不同格式 【函数】
//第一种 function getLocalTime(nS) { ).toLocaleString().replace(/:\d{,}$/,' '); } alert(getLocalTime()) ...
- easyui numberbox一些常用属性,方法
1.value="1234567.89"//数字框中的值 2.precision:2//精度(小数点后两位) 3.groupSeparator:','//(组分隔符) 4.deci ...
- ajax返回数据类型为XML数据的处理
/*XML:可扩展标记语言 HTML:超文本标记语言 标签:<标签名></标签名> 特点: 1.必须要有一个根 2.标签名自定义 3.对大小写敏感 4.有开始就要有结束 5.同 ...