HDU 5451 Best Solver 数论 快速幂 2015沈阳icpc
Best Solver
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)
Total Submission(s): 1115 Accepted Submission(s): 645
It is known that y=(5+26√)1+2x.
For a given integer x (0≤x<232) and a given prime number M (M≤46337), print [y]%M. ([y] means the integer part of y)
Following are T lines, each containing two integers x and M, as introduced above.
Each line contains an integer representing [y]%M.
0 46337
1 46337
3 46337
1 46337
21 46337
321 46337
4321 46337
Case #2: 969
Case #3: 16537
Case #4: 969
Case #5: 40453
Case #6: 10211
Case #7: 17947
题目描述:
对于,给出x和mod,求y向下取整后取余mod的值为多少?
分析:
这种(a+sqrt(b))^n%mod,向下取整取余的式子有个递推式 S(n) = 2*a*S(n-1)+(b-a*a)*S(n-2) 这个式子得到的结果是向上取整,我们这里是向下取整,所以最后得到的结果还要减一
我这篇博客https://www.cnblogs.com/l609929321/p/9398349.html有这个递推式的推导过程
由上诉递推式不难得到:S(n) = (10*S(n-1)-S(n-2)+mod)%mod
但是这个题目的指数1+2^x(x<2^32)非常大,就算用矩阵快速幂也不行,所以我们得对式子进行一次优化
看到指数很大的类似斐波拉数列问题(都是线性递推式)很容易的想到找循环节
因为mod<=46337,所以我们直接遍历求前面46337项一定会出现循环节,然后记录下循环节的位置就好
(以后找模一个数的周期最大取到他的三倍,证明博客:https://www.xuebuyuan.com/1198038.html)
再对指数按照循环节的位置进行取模就可以了
AC代码
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e5 + 10;
const ll mod = 1e9 + 7;
ll r[maxn], ans[maxn], x, m;
ll qow( ll b, ll n, ll mod ) {
ll ans = 1;
while( n ) {
if( n&1 ) {
ans = ans*b%mod;
}
b = b*b%mod;
n /= 2;
}
return ans;
}
int main() {
ll T, t = 0;
cin >> T;
while( T -- ) {
ll x, m;
t ++;
cin >> x >> m;
ans[0] = 2, ans[1] = 10;
for( ll i = 2; i < maxn; i ++ ) {
ans[i] = (10*ans[i-1]-ans[i-2]+m)%m; //根据类似斐波拉数得到的一个递推式
if( ans[i-1] == ans[0] && ans[i] == ans[1] ) { //因为x很大,这里计算了循环周期
r[m] = i-1;
break;
}
}
ll k = (1+qow(2,x,r[m]))%r[m];
cout << "Case #" << t << ": " << (ans[k]-1+m)%m << endl;
}
return 0 ;
}
HDU 5451 Best Solver 数论 快速幂 2015沈阳icpc的更多相关文章
- Hdu 5451 Best Solver (2015 ACM/ICPC Asia Regional Shenyang Online) 暴力找循环节 + 递推
题目链接: Hdu 5451 Best Solver 题目描述: 对于,给出x和mod,求y向下取整后取余mod的值为多少? 解题思路: x的取值为[1, 232],看到这个指数,我的心情是异常崩 ...
- ACM数论-快速幂
ACM数论——快速幂 快速幂定义: 顾名思义,快速幂就是快速算底数的n次幂.其时间复杂度为 O(log₂N), 与朴素的O(N)相比效率有了极大的提高. 原理: 以下以求a的b次方来介绍: 把b转换成 ...
- HDU.1575 Tr A ( 矩阵快速幂)
HDU.1575 Tr A ( 矩阵快速幂) 点我挑战题目 题意分析 直接求矩阵A^K的结果,然后计算正对角线,即左上到右下对角线的和,结果模9973后输出即可. 由于此题矩阵直接给出的,题目比较裸. ...
- hdu 3117 Fibonacci Numbers 矩阵快速幂+公式
斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...
- 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 ...
- hdu 5451 Best Solver 矩阵循环群+矩阵快速幂
http://acm.hdu.edu.cn/showproblem.php?pid=5451 题意:给定x 求解 思路: 由斐波那契数列的两种表示方法, 之后可以转化为 线性表示 F[n] = ...
- HDU 2842 (递推+矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...
- HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdu 2604 Queuing(矩阵快速幂乘法)
Problem Description Queues and Priority Queues are data structures which are known to most computer ...
随机推荐
- Log4Net 配置日志按日期和日志级别分类写入
配置效果图: 配置代码: <?xml version="1.0" encoding="utf-8" ?> <log4net> <! ...
- LVS + Keepalived + Nginx基于DR模式构建高可用方案
在大型网站中一般服务端会做集群,同时利用负载均衡器做负载均衡.这样有利于将大量的请求分散到各个服务器上,提升网站的响应速度.当然为了解决单点故障的问题,还会做热备份方案.这里演示利用LVS做负载均衡器 ...
- Ubuntu 系统如何用pycharm开发python—OpenCV
- 重入锁的学习 (ReentrantLock)
重入锁 :(ReentrantLock) 上锁 用reentrantLock.lock 方法 解锁 用reentrantLock.unlock 方法 上锁和解锁 必须配对 可以多重上锁 Reentr ...
- 【原创】HashMap复习精讲
引言 由于近期忙着搬家,又偷懒了几个礼拜! 其实我很早以前就想写一篇关于HashMap的面试专题.对于JAVA求职者来说,HashMap可谓是集合类的重中之重,甚至你在复习的时候,其他集合类都不用看, ...
- 转载 | CSS图片下面产生间隙的 6种解决方案
在进行页面的DIV+CSS排版时,遇到IE6(当然有时Firefox下也会偶遇)浏览器中的图片元素img下出现多余空白的问题绝对是常见的对於 该问题的解决方法也是「见机行事」,根据原因的不同要用不同的 ...
- android——SQLite数据库存储(操作)
public class MyDatabaseHelper extends SQLiteOpenHelper { //把定义SQL建表语句成字符串常量 //图书的详细信息 //ID.作者.价格.页数. ...
- getpost请求案例
public class MainActivity extends AppCompatActivity { private ListView lv; @Override protected void ...
- Python.append()与Python.extend()的区别
lst=[1,2] >>>[1,2] lst.append([3,4]) >>>[1, 2, [3, 4]] lst.extend([3,4]) >>& ...
- c# http Post Get 方法
/// <summary> /// get方式访问webapi /// </summary> /// <param name="url">< ...