首页 技术 正文
技术 2022年11月14日
0 收藏 424 点赞 3,804 浏览 2046 个字

Best Solver

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)
Total Submission(s): 1115    Accepted Submission(s): 645

Problem DescriptionThe so-called best problem solver can easily solve this problem, with his/her childhood sweetheart.

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) InputAn integer T (1<T≤1000), indicating there are T test cases.
Following are T lines, each containing two integers x and M, as introduced above. OutputThe output contains exactly T lines.
Each line contains an integer representing [y]%M. Sample Input7
0 46337
1 46337
3 46337
1 46337
21 46337
321 46337
4321 46337 Sample OutputCase #1: 97
Case #2: 969
Case #3: 16537
Case #4: 969
Case #5: 40453
Case #6: 10211
Case #7: 17947 Source2015 ACM/ICPC Asia Regional Shenyang Online Recommendwange2014 

题目描述:

  对于HDU 5451 Best Solver 数论 快速幂 2015沈阳icpc,给出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 ;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,493
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297