Looooops(求解同余方程、同余方程用法)【拓展欧几里得】
Looooops(点击)
A Compiler Mystery: We are given a C-language style for loop of type
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.
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 16
3 7 2 16
7 3 2 16
3 4 2 16
0 0 0 0
Sample Output
0
2
32766
FOREVER
思路:
题目搁了两天,开始想了个很复杂的方法,花了好长时间调试结果TLE
后来实在没办法去搜了一下,才知道是用同余方程 解决
根据题目可以列出一个方程式,将加c的次数设成x:
∵ a+cx≡b(%mod);
∴ a+c*x+mod*y=b;
∴ c*x+mod*y=b-a;
根据exgcd可以求解 同时好要求出最小整数x的解就是结果
代码:
#include<stdio.h>
typedef long long LL;
LL GCD;
LL exgcd(LL a,LL b,LL &x,LL &y) // 拓展欧几里得求x、y特解
{
if(!b){
x=1;y=0;return a;
}
GCD=exgcd(b,a%b,y,x);
y-=a/b*x;
return GCD;
}
LL qpow(LL c,LL q) // 快速幂 因为题目涉及求2^k 如果用pow可能会出错
{
LL ans=1; //快速幂里面不需要%mod 和mod没有关系 不需要担心快速幂结果会超过2^k
while(q){
if(q%2){
ans*=c;
}
c*=c;
q/=2;
}
return ans;
}
int main()
{
LL a,b,c,k,x,y,t;
while(scanf("%lld%lld%lld%lld",&a,&b,&c,&k)!=EOF){
if(a==0&&b==0&&c==0&&k==0){
break;
}
else{
GCD=exgcd(c,qpow(2,k),x,y); //将 c、mod=2^k、x、y 依次带入拓展欧几里得方程求解
if((b-a)%GCD){
printf("FOREVER\n"); // 判断方程是否有解
}
else{
x*=((b-a)/GCD); //类似求解ax+by=c最小整数解的方法 得出最小x的值
t=qpow(2,k)/GCD; //因为求x所以t=b/GCD 但这个b并不是输入的b 而是 方程里面对应
if(t<0){ 的b 即 qpow(2,k)
t=-t;
}
x=(x%t+t)%t;
printf("%lld\n",x); //输出结果x
}
}
}
return 0;
}
Looooops(求解同余方程、同余方程用法)【拓展欧几里得】的更多相关文章
- 【lydsy1407】拓展欧几里得求解不定方程+同余方程
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1407 题意: 有n个野人,野人各自住在第c[i]个山洞中(山洞成环状),每年向前走p[i] ...
- [POJ2115]C Looooops 拓展欧几里得
原题入口 这个题要找到本身的模型就行了 a+c*x=b(mod 2k) -> c*x+2k*y=b-a 求这个方程对于x,y有没有整数解. 这个只要学过拓展欧几里得(好像有的叫扩展欧几里德QA ...
- ACM数论-欧几里得与拓展欧几里得
ACM数论——欧几里得与拓展欧几里得 欧几里得算法: 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd ...
- Vulnerable Kerbals CodeForces - 772C【拓展欧几里得建图+DAG上求最长路】
根据拓展欧几里得对于同余方程 $ax+by=c$ ,有解的条件是 $(a,b)|c$. 那么对于构造的序列的数,前一个数 $a$ 和后一个数 $b$ ,应该满足 $a*x=b(mod m)$ 即 $ ...
- HDU-3579-Hello Kiki (利用拓展欧几里得求同余方程组)
设 ans 为满足前 n - 1个同余方程的解,lcm是前n - 1个同余方程模的最小公倍数,求前n个同余方程组的解的过程如下: ①设lcm * x + ans为前n个同余方程组的解,lcm * x ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- BZOJ-1407 Savage 枚举+拓展欧几里得(+中国剩余定理??)
zky学长实力ACM赛制测试,和 大新闻(YveH) 和 华莱士(hjxcpg) 组队...2h 10T,开始 分工我搞A,大新闻B,华莱士C,于是开搞: 然而第一题巨鬼畜,想了40min发现似乎不可 ...
- [zoj 3774]Power of Fibonacci 数论(二次剩余 拓展欧几里得 等比数列求和)
Power of Fibonacci Time Limit: 5 Seconds Memory Limit: 65536 KB In mathematics, Fibonacci numbe ...
- 51 Nod 1256 乘法逆元(数论:拓展欧几里得)
1256 乘法逆元 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K ...
随机推荐
- ThinkPHP6.0 容器和依赖注入
ThinkPHP6.0 容器和依赖注入 分为如下两部分: 依赖注入 容器 依赖注入 依赖注入其实本质上是指对类的依赖通过构造器完成自动注入: 在控制器架构方法和操作和方法中一旦对参数进行对象类型约束则 ...
- poj2449第K小路径问题
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 30017 Accepted: 8159 ...
- 51Nod - 1255
也是第十一届校赛的C题,不过他把1e5改成了1e7. 一开始就想到用贪心做.思路是这样的:开一个字符数组ans保存答案.然后从头到尾遍历题目给出的字符串S,如果ans数组中还没有这个字母,那么就把字母 ...
- CF832C
题目链接:http://codeforces.com/contest/832/problem/C 题目大意: n个人,面向左或者右站在同一条轴上,每个人在轴上的坐标为x,速度为v.请你在某个位置放置一 ...
- .Net基础之1——学前入门
1..Net平台 2.C#编程语言 3..Net都能做什么 Winform桌面应用程序.Internet应用程序——ASP.Net(京东.淘宝.携程网)(主推). WP8手机开发.Unity 3D游戏 ...
- uiautomator2通过wifi操作手机
参考来源:https://www.cnblogs.com/c-x-a/p/11176066.html,有部分不适合当前版本的做了修改 1.手机通过USB连接电脑,先开启远程adb模式,操作如下(可以指 ...
- python之robotframework+pycharm测试框架
一.robotframework简介 Robot Framework是一款python编写的功能自动化测试框架.具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进行分布式 ...
- P2812 校园网络
luogu 传送门 首先考虑问题一 不难想到,如果有一个学校作为终端机,那么跟其处于同一个强联通中的所有学校就可以不用作为终端机了. 那么,问题一也就迎刃而解了:找到所有入度为0的缩点.因为这个学校( ...
- HttpSession之表单的重复提交 & 验证码
如果采用 HttpServletResponse.sendRedirct() 方法将客户端重定向到成功页面,将不会出现重复提交问题 1.表单的重复提交 1). 重复提交的情况: ①. 在表单提交到一个 ...
- IT笑话十则(二)
一.女程序员征婚 女程序员是这么征婚的: SELECT * FROM 男人们 WHERE 未婚=true and 同性恋=false and 有房=true and 有车=true and 条件 in ...