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`( ...
随机推荐
- 利用端口转发来访问virtualbox虚拟机centos6的jupyter notebook
1.除了在virtualbox中设置常规的端口转发外,还需要在windows上打开cmd,输入ssh -N -f -L localhost:8888:localhost:8889 -p 22 root ...
- python3.x Day4 模块!!
json and pickle模块 用途是为了持久化信息,这种持久化方式可以和其他程序语言兼容,一般都支持json,json只能持久化数据,pickle是python特有的方式,可以持久化所有信息和数 ...
- Thawte SSL Web Server 多域型SSL证书
Thawte SSL Web Server 多域型SSL证书,最多支持25个域名,需要验证域名所有权和申请单位信息,属于企业验证型SSL证书,提供40位/56位/128位,最高支持256位自适应加密. ...
- 视图 v$sql,v$sqlarea,$sqltext,v$sqltext_with_newlines 的差异
http://blog.csdn.net/leshami/article/details/8658205 视图v$sql,v$sqlarea,v$sqltext,v$sqltext_with_newl ...
- HTML5 & CSS3 & font-family
HTML5 & CSS3 & font-family 中文字体的英文名称 宋体* SimSun 黑体* SimHei 微软雅黑* Microsoft YaHei 微软正黑体 http: ...
- [K/3Cloud] 隐藏菜单后,如何在插件间接的调用隐藏菜单的操作
使用场景: 动态表单里面挂了个单据的序时薄,序时薄有菜单,但是把序时薄的工具栏隐藏了.新增,修改全部动态表单自己写.删除和过滤我想间接调用下隐藏的序时薄的删除和过滤按钮的操作.在插件里如何实现? 答: ...
- 02-js变量学习
<html> <head> <title>js的变量学习</title> <meta charset="UTF-8"/> ...
- 踩坑录-libreoffice fatal error com.sun.start.ucb.Interactive.AugmentedIOException: a folder could not be created
错误概要: 1.LibreOffice可以正常使用: 2.启动tomcat报错如下: Fatal error The application cannot be started. ][context= ...
- day1--大数据概念,hadoop介绍,hdfs整体运行机制
1.什么是大数据 基本概念 在互联网技术发展到现今阶段,大量日常.工作等事务产生的数据都已经信息化,人类产生的数据量相比以前有了爆炸式的增长,以前的传统的数据处理技术已经无法胜任,需求催生技术,一套用 ...
- ISkyShop B2B2C 商城系统V1.0正式版隆重公布
ISkyShop核心开发团队结合7年电商开发经验,历经1年多时间的设计研发,于2014年6月12日隆重推出ISkyShop B2B2C 商城系统V1.0,B2B2C商城系统是ISkyShop独立自主研 ...