POJ2115 C-Loop
这道题是求解不定方程的一道好练习题。
题目描述的很诡异……还说什么k进制,其实就是要求一个数A,每次加C,问到B要加多少次,所有的数对2k取模。
也就是说我们能列出如下方程:A+xC ≡ B (mod 2k)
我们把这个方程两边移项转化,那么就能得到一个不定方程的形式。
老套路,判断有没有解,否则用exgcd求解。
本题有小坑,你在1<<32的时候,即使变量开了longlong也不行,必须写成1ll的形式,否则会WA。
看一下代码。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<set>
#include<vector>
#include<queue>
#define pb push_back
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar('\n') using namespace std;
typedef long long ll;
const int M = ;
const int N = ;
const int INF = ;
const ll mod = ; ll read()
{
ll ans = ,op = ;
char ch = getchar();
while(ch < '' || ch > '')
{
if(ch == '-') op = -;
ch = getchar();
}
while(ch >= '' && ch <= '')
{
ans *= ;
ans += ch - '';
ch = getchar();
}
return ans * op;
} ll a,b,c,k,x,y,p,q,s; ll gcd(ll a,ll b)
{
return (!b) ? a : gcd(b,a%b);
} ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(!b)
{
x = ,y = ;
return a;
}
ll d = exgcd(b,a%b,y,x);
y -= a/b * x;
return d;
} int main()
{
while()
{
a = read(),b = read(),c = read(),k = read();
if(!a && !b && !c && !k) break;
p = c,q = 1LL << k,s = (b-a+q) % q;
ll G = gcd(p,q);
if(s % G)
{
printf("FOREVER\n");
continue;
}
p /= G,q /= G,s /= G;
exgcd(p,q,x,y);
x = (x + q) % q,x *= s,x %= q;
printf("%lld\n",x);
}
return ;
}
POJ2115 C-Loop的更多相关文章
- POJ2115——C Looooops(扩展欧几里德+求解模线性方程)
C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...
- Atitit 解决Unhandled event loop exception错误的办法
Atitit 解决Unhandled event loop exception错误的办法 查看workspace/.metadata/.log org.eclipse.swt.SWTError: No ...
- Looper.prepare()和Looper.loop()
什么时候需要 Looper Looper用于封装了android线程中的消息循环,默认情况下一个线程是不存在消息循环(message loop)的,需要调用Looper.prepare()来给线程创建 ...
- PostgreSQL-PL/pgSQL-cursor,loop
将spam_keyword表word字段的字符全部拆分,只是利用过程语言完成循环的操作而已. create or replace function proc1() returns setof text ...
- archlinux 加载loop模块,且设定loop设备个数
如果loop模块没有编译进内核就要先加载loop模块 modprobe loop 然后更改/etc/modprobe.d/modprobe.conf(有些文章写是在/etc/modprobe.conf ...
- 工作邮件loop的用法
examples come from native speaker Put john in the loop about this. He will have good advice. Why hav ...
- VMWare虚拟机实例拷贝到另一台服务器后出现Error in the RPC receive loop: RpcIn: Unable to send.错误的解决
把一个VMWare虚拟机实例拷贝到另一台服务器后,在事件查看器中的应用程序日志中不断出现Error in the RPC receive loop: RpcIn: Unable to send.错误, ...
- 禁用nested loop join里的spool
禁用nested loop join里的spool 转载自: https://blogs.msdn.microsoft.com/psssql/2015/12/15/spool-operator-and ...
- Android Handler、Loop 的简单使用
1.子线程和子线程之间的通信 package lib.com.myapplication; import android.os.Bundle; import android.os.Handler; i ...
- mysql while,loop,repeat循环,符合条件跳出循环
1.while循环 DELIMITER $$ DROP PROCEDURE IF EXISTS `sp_test_while`$$ CREATE PROCEDURE `sp_test_while`( ...
随机推荐
- [Python3网络爬虫开发实战] 1.7.3-Appium的安装
Appium是移动端的自动化测试工具,类似于前面所说的Selenium,利用它可以驱动Android.iOS等设备完成自动化测试,比如模拟点击.滑动.输入等操作,其官方网站为:http://appiu ...
- js 技巧 (六)JavaScript[对象.属性]集锦
JavaScript[对象.属性]集锦 SCRIPT 标记 用于包含javascript代码. 语法 属性 LANGUAGE 定义脚本语言 SRC 定义一个URL用以指定以.JS结尾的文件 windo ...
- python3.x Day5 socket编程
socket编程: socket 是大多应用层的底层的封装,实际封装的就是 发送,接收,但其实很复杂,在传输层协议之上(TCP/IP,UDP) 既然是网络通讯,一般按照服务端,客户端来处理:服务端: ...
- 关于No Spring WebApplicationInitializer types detected on classpath的提示,tomcat 卡主
No Spring WebApplicationInitializer types detected on classpath 下一句:Initializing Spring root WebAppl ...
- 慕课笔记利用css进行布局【一列布局】
<html> <head> <title>一列布局</title> <style type="text/css"> bo ...
- ZOJ 3905 Cake
Cake Time Limit: 4 Seconds Memory Limit: 65536 KB Alice and Bob like eating cake very much. One ...
- Delphi XE4 inline 的用法
为了提高应用程序的性能,可以用inline声明一个函数过过程,对于声明为inline的过程,编译器在编译时,会取代正常的过程调用方式,取而代之是直接把这个函数的编译代码拿过来生成到调用该过程的地方,有 ...
- TCP/IP学习笔记(3)----IP,ARP,RARP协议
把这三个协议放到一起学习是因为这三个协议处于同一层(网络层协议),ARP协议用来找到目标主机的Ethernet网卡Mac地址,IP则承载要发送的消息.数据链路层可以从ARP得到数据的传送信息,而从IP ...
- (入门SpringBoot)SpringBoot来临(一)
.创建独立的Spring应用程序. .嵌入tomcat,Jetty或者Undertow,无需部署war文件; .允许通过Maven来获取starter; .尽可能的自动配置Spring. .提供生产就 ...
- Eclipse错误:Syntax error on tokens, delete these tokens问题解决
错误:Syntax error on tokens, delete these tokens 出现这样的错误一般是括号.中英文字符.中英文标点.代码前面的空格,尤其是复制粘贴的代码,去掉即可.