Best Solver

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

Problem Description
The 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)

 
Input
An 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.
 
Output
The output contains exactly T lines.
Each line contains an integer representing [y]%M.
 
Sample Input

7
0 46337
1 46337
3 46337
1 46337
21 46337
321 46337
4321 46337

 
Sample Output

Case #1: 97
Case #2: 969
Case #3: 16537
Case #4: 969
Case #5: 40453
Case #6: 10211
Case #7: 17947

Source

/**
题意:对于方程 给出x和mod,求y向下取整后取余mod的值为多少
做法:矩阵 构造
**/
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm> using namespace std;
typedef long long ll;
ll mod;
typedef vector<ll> vec;
typedef vector<vec> mat; mat mul(mat& A, mat& B)
{
mat C(A.size(), vec(B[].size()));
for(int i = ; i < A.size(); ++i) {
for(int k = ; k < B.size(); ++k) {
for(int j = ; j < B[].size(); ++j) {
C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod;
}
}
}
return C;
}
mat pow(mat A, ll n)
{
mat B(A.size(), vec(A.size()));
for(int i = ; i < A.size(); ++i) {
B[i][i] = ;
}
while(n > ) {
if(n & ) {
B = mul(B, A);
}
A = mul(A, A);
n >>= ;
}
return B;
}
int solve(long long a, long long b, long long c)
{
long long ans = ;
long long k = a % c;
while(b > )
{
if(b % == )
{
ans = (ans * k) % c;
}
b = b / ;
k = (k * k) % c;
}
return ans;
}
int main()
{
int T;
scanf("%d", &T);
ll a, b, n;
int Case = ;
while(T--) {
scanf("%I64d %I64d", &n, &mod);
mat A(, vec(, ));
A[][] = ;
A[][] = ;
A[][] = ;
A[][] = ;
long long tt = mod;
n = solve(, n, (mod - ) * (mod + )) + ;
A = pow(A, n);
ll ans = ( * A[][] - ) % mod;
printf("Case #%d: %I64d\n", Case++, ans);
}
return ;
}

HDU-5451的更多相关文章

  1. Hdu 5451 Best Solver (2015 ACM/ICPC Asia Regional Shenyang Online) 暴力找循环节 + 递推

    题目链接: Hdu  5451  Best Solver 题目描述: 对于,给出x和mod,求y向下取整后取余mod的值为多少? 解题思路: x的取值为[1, 232],看到这个指数,我的心情是异常崩 ...

  2. hdu 5451 Best Solver 矩阵循环群+矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=5451 题意:给定x    求解 思路: 由斐波那契数列的两种表示方法, 之后可以转化为 线性表示 F[n] = ...

  3. HDU 5451 广义斐波那契数列

    这道题目可以先转化: 令f(1) = 5+2√6 f(2) = f(1)*(5+2√6) ... f(n) = f(n-1)*(5+2√6) f(n) = f(n-1)*(10-(5-2√6)) = ...

  4. hdu 5451(矩阵 +Fibonacci )

    题意:求 [(5 + 2*sqrt(6))^(1 + 2^x)]  % M 基于hdu2256可以求(5 + 2*sqrt(6))^ n 但是n特别大,我们可以找矩阵的循环节 两种可能 1.mod-1 ...

  5. 特征根法求通项+广义Fibonacci数列找循环节 - HDU 5451 Best Solver

    Best Solver Problem's Link Mean: 给出x和M,求:(5+2√6)^(1+2x)的值.x<2^32,M<=46337. analyse: 这题需要用到高中的数 ...

  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. HDU - 5451 Best Solver(循环节+矩阵快速幂)

    Best Solver The so-called best problem solver can easily solve this problem, with his/her childhood ...

  8. HDU 5451 Best Solver(fibonacci)

    感谢这道题让我复习了一遍线代,还学习了一些奇奇怪怪的数论. 令 二项展开以后根号部分抵消了 显然有 所以要求的答案是 如果n比较小的话,可以直接对二项式快速幂,但是这题n很大 这个问题和矩阵的特征值以 ...

  9. HDU 5451 Best Solver 数论 快速幂 2015沈阳icpc

    Best Solver Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Tota ...

  10. HDU 5451——递推式&&循环节

    题意 设 $y = (5+2\sqrt 6)^{1+2^x}$,给出 $x, M$($0\leq x \leq 2^{32}, M \leq 46337$),求 $[y]\%M$. 分析 由通项推递推 ...

随机推荐

  1. JavaScript - arguments object

    The arguments object is an Array-like object corresponding to the arguments passed to a function. fu ...

  2. BZOJ 3569 DZY Loves Chinese II 树上差分+线性基

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3569 Description 神校XJ之学霸兮,Dzy皇考曰JC. 摄提贞于孟陬兮,惟庚寅 ...

  3. 平面最近点对(HDU 1007)

    题解:点击 #include <stdio.h> #include <string.h> #include <algorithm> #include <ios ...

  4. 福大软工1816:Alpha(2/10)

    Alpha 冲刺 (2/10) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务: 文字/口头描述: 1.学习qqbot库: 2.实 ...

  5. html5实现web app摇一摇换歌

    微信可以摇歌曲,根据声音识别出歌曲,然后返回歌曲信息,利用html5的deviceOrientation特性和deviceMotion事件也可以在web app上实现类似于微信摇一摇的功能,原生的ap ...

  6. Spring温故而知新 – bean的装配

    Spring装配机制 Spring提供了三种主要的装配机制: 1:通过XML进行显示配置 2:通过Java代码显示配置 3:自动化装配 自动化装配 Spring中IOC容器分两个步骤来完成自动化装配: ...

  7. 在C/C++程序中打印当前函数调用栈

    前几天帮同事跟踪的一个程序莫名退出,没有core dump(当然ulimit是打开的)的问题.我们知道,正常情况下,如果程序因为某种异常条件退出的话,应该会产生core dump,而如果程序正常退出的 ...

  8. Java IO 之 FileFilter与FilenameFilter

    FileFilter与FilenameFilter可以实现对文件的过滤,他们都是接口,具体的过滤规则需要我们自己编写 1.FileFilter package org.zln.io.file; imp ...

  9. Codeforces Round #518 Div. 1没翻车记

    A:设f[i][j][0/1]为前i个数第i位为j且第i位未满足/已满足限制的方案数.大力dp前缀和优化即可. #include<iostream> #include<cstdio& ...

  10. TCP/IP Note4

    TCP/IP邮件 你的电子邮件程序会使用不同的TCP/IP协议: 使用SMTP来发送邮件: 使用POP从邮件服务器下载邮件: 使用IMAP连接到邮件服务器 1. SMTP - 简单邮件传输协议 SMT ...