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 ...
随机推荐
- sql server 2008 去除html标签
由于商品详情数据库的字段是text,存放的是html,但是要求导出的商品详情中只是商品的描述,不要标签,原来打算先把数据导入excel中,然后利用java的正则去替换,结果由于商品详情太大,一个单元格 ...
- 股神小L 2016Vijos省选集训 day1
股神小L (stock.c/pas/cpp)============================ 小L厌倦了算法竞赛,希望到股市里一展身手.他凭借自己还行的计算机功底和可以的智商,成功建立一个模型 ...
- [原创]关于absolute、relative和float的一些思考
absolute: 元素完全脱离文档流,不占文档流的位置,不使用top.left等属性时,仍然在原文档流位置上(但是不在文档流中,也不占用位置),设置了top.left等之后,向上寻找到第一个非sta ...
- 配置过滤器filter对跨站脚本攻击XSS实现拦截
1.web.xml中配置filter [html] view plain copy <filter> <filter-name></filter-name> & ...
- 九度OJ 1346:会员积分排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:967 解决:413 题目描述: 元旦佳节快到了,超市A想要给会员一些奖品.但是奖品有限,所以它需要给这些会员做一个排序,然后将名单输出来.排 ...
- 目标检测--之RCNN
目标检测--之RCNN 前言,最近接触到的一个项目要用到目标检测,还有我的科研方向caption,都用到这个,最近电脑在windows下下载数据集,估计要一两天,也不能切换到ubuntu下撸代码~.所 ...
- 3.25课·········JavaScript的DOM操作
1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 2.Window对象操作 一.属性和方法: 属性(值或者子对象): op ...
- 【leetcode刷题笔记】Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Python3 logging 模块
Python3 logging模块 日志模块: 用于便捷记录日志且线程安全的模块 CRITICAL = 50 FATAL = CRITICAL ERROR = 40 WARNING = 30 WARN ...
- 查询速度优化用not EXISTS 代替 not in
1,not in 速度奇慢,要用 not EXISTS ,速度奇快! 大表效果尤其明显 sql中exists,not exists的用法 exists()后面的子查询被称做相关子查询,他是不返回列表的 ...