Problem Description

Given a prime number C(1≤C≤2×105), and three integers k1, b1, k2 (1≤k1,k2,b1≤109). Please find all pairs (a, b) which satisfied the equation a^(k1⋅n+b1 )+ b^(k2⋅n−k2+1) = 0 (mod C)(n = 1, 2, 3, ...).

Input

There are multiple test cases (no more than 30). For each test, a single line contains four integers C, k1, b1, k2.

Output

First, please output "Case #k: ", k is the number of test case. See sample output for more detail.
Please output all pairs (a, b) in lexicographical order. (1≤a,b<C). If
there is not a pair (a, b), please output -1.

Sample Input

23 1 1 2

Sample Output

Case #1:

1 22

题目意思一开始理解错了,然后以为只要能找到一个n满足条件,这组(a, b)就算满足条件。

原来是要所有n满足才行,这样题目就是任意型问题,相对会好解决一点。

既然是任意,肯定考虑先取些特殊值试试。

自然考虑取n=1,

发现a^(k1+b1)
+ b = 0(mod c)

这样就把b在模c情况下的值求出来了。

b = - a^(k1+b1)(mod
c)

然后继续带入n = 2,

a^(2k1+b1)
+ b^(k2+1) = 0(mod c)

大胆猜测这个式子成立,n取任何数都会成立。

因为n加1时,a的指数和b的指数增量是一定的,那么n取任何数时,必然和n-1之前相差的式子是一个定值多项式。

首先n
= 2时,

可以得到b^(k2+1)
= -a^(2k1+b1) = -a^(k1+b1)*a^k1 = b*a^k1

这一步可以通过n = 1得到的结论两边消掉一个b,得:

b^k2 =
a^k1。

也就是说由n = 1满足,让n = 2满足的条件是b^k2 = a^k1。

基本是可以YY一下,由n = 2满足,让n = 3满足的条件是b^k2 = a^k1。

即由n
= k满足,让n
= k+1满足的条件是b^k2
= a^k1。

这一步用数学归纳法随便搞一搞就可以验证了。

不知道是什么情况,比赛时是n = 1得出b, n = 2验证b过的。

现在重写了一遍死活T。。。

后来换成n
= 2时验证式子b^k2
= a^k1就能过了。。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; int c, k1, b1, k2; //快速幂m^n
LL quickPow(LL x, LL n)
{
LL a = ;
while (n)
{
a *= n& ? x : ;
a %= c;
n >>= ;
x *= x;
x %= c;
}
return a;
} bool judge(LL a, LL b)
{
if (quickPow(a, k1) != quickPow(b, k2))
return false;
else
return true;
} void work()
{
LL b;
bool flag = false;
for (int a = ; a < c; ++a)
{
b = -quickPow(a, (b1+k1)%(c-));
b = (b+c)%c;
if (judge(a, b))
{
printf("%d %I64d\n", a, b);
flag = true;
}
}
if (!flag)
printf("-1\n");
} int main()
{
//freopen("test.in", "r", stdin);
int times = ;
while (scanf("%d%d%d%d", &c, &k1, &b1, &k2) != EOF)
{
printf("Case #%d:\n", times);
work();
times++;
}
return ;
}

ACM学习历程—HDU5478 Can you find it(数论)(2015上海网赛11题)的更多相关文章

  1. ACM学习历程——HDU5017 Ellipsoid(模拟退火)(2014西安网赛K题)

    ---恢复内容开始--- Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distanc ...

  2. ACM学习历程—HDU5476 Explore Track of Point(平面几何)(2015上海网赛09题)

    Problem Description In Geometry, the problem of track is very interesting. Because in some cases, th ...

  3. ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)

    Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number ...

  4. ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)

    Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...

  5. ACM学习历程—HDU 5459 Jesus Is Here(递推)(2015沈阳网赛1010题)

    Sample Input 9 5 6 7 8 113 1205 199312 199401 201314 Sample Output Case #1: 5 Case #2: 16 Case #3: 8 ...

  6. ACM学习历程—HDU 5451 Best Solver(Fibonacci数列 && 快速幂)(2015沈阳网赛1002题)

    Problem Description The so-called best problem solver can easily solve this problem, with his/her ch ...

  7. ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)

    Problem Description In Land waterless, water is a very limited resource. People always fight for the ...

  8. ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)

    Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...

  9. ACM学习历程—HDU5407 CRB and Candies(数论)

    Problem Description CRB has N different candies. He is going to eat K candies.He wonders how many co ...

随机推荐

  1. 用C#来学习唐诗三百首和全唐诗

    Begin 最近把项目做完了,闲来无事,就想做点好玩的事情,刚好前几天下载了[唐诗三百首]和[全唐诗]这两个txt文件,正好用C#来整理一下. 然后导出QData格式,可以给其他软件读取. 以后弄个开 ...

  2. 星球大战starwar(并查集)

    1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 5253  Solved: 2395[Submit ...

  3. J - 组合

    J - 组合 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu Description 有两 ...

  4. vue router-link子级返回父级页面

    vue-router嵌套路由,从二级路由跳转到一级路由时,间歇性导致一级路由重叠 解决方法: 1.使用this.$router.push跳转

  5. CMD命令获取电脑所有连接过的WiFi密码

    cmd中输入命令:for /f "skip=9 tokens=1,2 delims=:" %i in ('netsh wlan show profiles') do  @echo ...

  6. 九度OJ 1204:农夫、羊、菜和狼的故事 (遍历、BFS)

    时间限制:1 秒 内存限制:32 兆 特殊判题:是 提交:744 解决:502 题目描述: 有一个农夫带一只羊.一筐菜和一只狼过河. 果没有农夫看管,则狼要吃羊,羊要吃菜. 但是船很小,只够农夫带一样 ...

  7. 在普通网页中如何调用html5+的plus对象

    <script> //用法: HTML5+的plus对象,必须由click事件触发后,才能在普通网页中使用.所以在没有click的情况下,调用本文件可以解决问题! //在代码中使用: pl ...

  8. redis持久化AOF详细操作步骤

    1.切换到redis目录下面,创建文件 s3-redis.conf 2.编辑文件s3-redis.conf 3.终止当前redis服务端 4.登录redis客户端失败,说明服务端已停止 5.重启red ...

  9. unix网络编程笔记(二)

    第四章笔记 1. 基本Tcpclient/server程序的套接字函数 2. socket函数: int socket(int family,int type,int protocol); (1)so ...

  10. android启动页延时跳转

    package com.goodness.goodness; import android.content.Context; import android.content.Intent; import ...