传送门

这道题是求解不定方程的一道好练习题。

题目描述的很诡异……还说什么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的更多相关文章

  1. POJ2115——C Looooops(扩展欧几里德+求解模线性方程)

    C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...

  2. Atitit 解决Unhandled event loop exception错误的办法

    Atitit 解决Unhandled event loop exception错误的办法 查看workspace/.metadata/.log org.eclipse.swt.SWTError: No ...

  3. Looper.prepare()和Looper.loop()

    什么时候需要 Looper Looper用于封装了android线程中的消息循环,默认情况下一个线程是不存在消息循环(message loop)的,需要调用Looper.prepare()来给线程创建 ...

  4. PostgreSQL-PL/pgSQL-cursor,loop

    将spam_keyword表word字段的字符全部拆分,只是利用过程语言完成循环的操作而已. create or replace function proc1() returns setof text ...

  5. archlinux 加载loop模块,且设定loop设备个数

    如果loop模块没有编译进内核就要先加载loop模块 modprobe loop 然后更改/etc/modprobe.d/modprobe.conf(有些文章写是在/etc/modprobe.conf ...

  6. 工作邮件loop的用法

    examples come from native speaker Put john in the loop about this. He will have good advice. Why hav ...

  7. VMWare虚拟机实例拷贝到另一台服务器后出现Error in the RPC receive loop: RpcIn: Unable to send.错误的解决

    把一个VMWare虚拟机实例拷贝到另一台服务器后,在事件查看器中的应用程序日志中不断出现Error in the RPC receive loop: RpcIn: Unable to send.错误, ...

  8. 禁用nested loop join里的spool

    禁用nested loop join里的spool 转载自: https://blogs.msdn.microsoft.com/psssql/2015/12/15/spool-operator-and ...

  9. Android Handler、Loop 的简单使用

    1.子线程和子线程之间的通信 package lib.com.myapplication; import android.os.Bundle; import android.os.Handler; i ...

  10. mysql while,loop,repeat循环,符合条件跳出循环

    1.while循环 DELIMITER $$ DROP PROCEDURE IF EXISTS `sp_test_while`$$ CREATE PROCEDURE `sp_test_while`( ...

随机推荐

  1. phpstorm破解激活码

    一.将“0.0.0.0 account.jetbrains.com”添加到hosts文件中 二.浏览器打开 http://idea.lanyus.com,点击页面中的“获得注册码”,然后在注册时切换至 ...

  2. assert.notDeepStrictEqual()详解

    assert.notDeepStrictEqual(actual, expected[, message]) 深度地严格不相等比较测试,与 assert.deepStrictEqual() 相反. c ...

  3. linux 负载各项查看命令

    free -h top -c 查看使用情况 sar -r/s/b 查看IO状态 iostat -x 1 10 查看服务器的状态 vmstat 查看内存使用率最后的前10个进程 ps -aux |sor ...

  4. (转载)O(N)的素数筛选法和欧拉函数

    转自:http://blog.csdn.net/dream_you_to_life/article/details/43883367 作者:Sky丶Memory 1.一个数是否为质数的判定. 质数,只 ...

  5. 洛谷 P2335 SDOI 2005 毒瘤 位图(也补上注释了)

    #include<iostream> #include<cstdio> #include<queue> #include<cstring> using ...

  6. Mybatis传递多个参数的4种方式(干货)

    Mybatis传递多个参数的4种方式(干货)-----https://blog.csdn.net/youanyyou/article/details/79406486

  7. UVALive 6507 Passwords

    Passwords Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ...

  8. 数据结构-B+树

    B+ 树是一种树数据结构,是一个n叉排序树,每个节点通常有多个孩子,一棵B+树包含根节点.内部节点和叶子节点.根节点可能是一个叶子节点,也可能是一个包含两个或两个以上孩子节点的节点. B+ 树通常用于 ...

  9. Automation 的 Wait 工具

    public static WebDriverWait createWait(WebDriver driver) { return new WebDriverWait(driver, Environm ...

  10. nyoj_308_Substring_201405091611

    Substring 时间限制:1000 ms  |           内存限制:65535 KB 难度:1   描述 You are given a string input. You are to ...