ACM学习历程—HDU5478 Can you find it(数论)(2015上海网赛11题)
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题)的更多相关文章
- ACM学习历程——HDU5017 Ellipsoid(模拟退火)(2014西安网赛K题)
---恢复内容开始--- Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distanc ...
- ACM学习历程—HDU5476 Explore Track of Point(平面几何)(2015上海网赛09题)
Problem Description In Geometry, the problem of track is very interesting. Because in some cases, th ...
- ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)
Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number ...
- ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)
Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...
- 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 ...
- 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 ...
- 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 ...
- ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)
Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...
- ACM学习历程—HDU5407 CRB and Candies(数论)
Problem Description CRB has N different candies. He is going to eat K candies.He wonders how many co ...
随机推荐
- Pycharm 中错误ImportError: No module named appium
Q: Pycharm 中错误ImportError: No module named appium A: Pycharm IDE Preferences -> Project Interpret ...
- Linux下,部署多个Tomcat
1.复制一个已有的tomcat,并重命名: 2.打开tomcat--conf--server.xml:[共修改3处配置] <Server port="8005" shutdo ...
- 软件测试人员需要精通的开发语言(1)--- VBScript
软件测试不局限于点点点的纯黑盒测试,提升自身的代码能力也是事关重要的.软件测试的发展,越来越多的公司对于测试人员的要求也日益提高,测试人员必备开发能力的优势也凸显出来.简单的介绍下部分开发语言的学习及 ...
- Office 365系列(-)
昨天参加上海微软TechED技术大会,看见很多传说中的大牛,听了涂曙光老师等人的讲座,激情澎湃啊,看见他们对技术以及程序员社区的投入及激情,十分敬佩.自己搞IT行业也已经10多年了,平常都很少写博客和 ...
- GCD(st表+二分)
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- Arrays类--Arrays.asList()方法使用
java.util类 Arrays java.lang.Object——java.util.Arrays public class Arrays extends Object 此类包含用来操作数组(比 ...
- hibernate 查询方式
1.对象导航查询 2.OID查询 3.hql查询 4.QBC查询 5.本地sql查询 一.对象导航查询 示例: 查询id=6的user对象的所有角色: 二.OID查询 实例查询id=6的user对象 ...
- Android JSON And Object Cast
Ref:JSON字符串转换成Java实体类(POJO) Ref:Java.Json转换方式之二:Jackson Ref:Jackson 框架,轻易转换JSON Ref:几种序列化协议(protobuf ...
- python实例3-天气小模块
调用中国天气的一小段代码,抓取 #! /usr/bin/python # coding = utf-8 # ToDo: get weather info from weather.com.cn # A ...
- webdriver与JS操作浏览器元素
1.JQuery的选择器实例 语法 描述 $(this) 当前 HTML 元素 $("p") 所有 <p> 元素 $("p.intro") 所有 c ...