HDU-3221
Brute-force Algorithm
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2560 Accepted Submission(s): 657

Any fool but Brute knows that the function “funny” will be called too many times. Brute wants to investigate the number of times the function will be called, but he is too lazy to do it.
Now your task is to calculate how many times the function “funny” will be called, for the given a, b and n. Because the answer may be too large, you should output the answer module by P.
For each test cases, there are four integers a, b, P and n in a single line.
You can assume that 1≤n≤1000000000, 1≤P≤1000000, 0≤a, b<1000000.
3
3 4 10 3
4 5 13 5
3 2 19 100
/**
题意:根据题意可以知道求 f(n) = f(n-1)*f(n-2)的值
f(1) = a
f(2) = b;
f(3) = a*b;
f(4) = a*b^2
f(5) = a^2*b^3
......
可以得知 a,b 的指数是斐波纳锲数列
做法:欧拉 + 矩阵 + 蒙哥马利幂模算法
ps:没有搞清楚一点 在求矩阵的n-3次幂可以过 可是n-2次幂 不能过
**/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define SIZE 2
#define clr( a, b ) memset( a, b, sizeof(a) )
long long MOD;
struct Mat
{
long long mat[ SIZE ][ SIZE ];
int n;
Mat(int _n) {
n = _n;
clr(mat, );
}
void init() {
for(int i = ; i < n; ++i)
for(int j = ; j < n; ++j) {
mat[i][j] = (i == j);
}
}
Mat operator * (const Mat& b) const {
Mat c(b.n);
for(int k = ; k < n; ++k)
for(int i = ; i < n; ++i) {
if(mat[i][k])
for(int j = ; j < n; ++j) {
c.mat[i][j] = (c.mat[i][j] + mat[i][k] * b.mat[k][j]) % MOD;
}
}
return c;
}
}; Mat fast_mod(Mat a, int b)
{
Mat res(a.n);
res.init();
while(b)
{
if(b & ) {
res = res * a;
}
a = a * a;
b >>= ;
}
return res;
} long long eular(long long n)
{
long long ans = n;
for(int i = ; i * i <= n; i++)
{
if(n % i == )
{
ans -= ans / i;
while(n % i == ) {
n /= i;
}
}
}
if(n > ) {
ans -= ans / n;
}
return ans;
}
long long modPow(long long s, long long index, long long mod)
{
long long ans = ;
s %= mod;
while(index >= )
{
if((index & ) == ) { //奇数
ans = (ans * s) % mod;
}
index >>= ;
s = s * s % mod;
}
return ans;
}
int main()
{
int T;
int Case = ;
scanf("%d", &T);
while(T--)
{
long long x, y, n, res, p;
cin >> x >> y >> p >> n;
printf("Case #%d: ", Case++);
if(p == || p == ) {
printf("0\n");
continue;
}
if(n == )
{
cout << x % p << endl;
continue;
}
else if(n == )
{
cout << y % p << endl;
continue;
}
MOD = eular(p);
Mat C();
C.mat[][] = ;
C.mat[][] = ;
C.mat[][] = ;
C.mat[][] = ;
C = fast_mod(C, n - );
long long aa = C.mat[][] + C.mat[][];
long long bb = C.mat[][] + C.mat[][];
res = ((modPow(x , aa, p) * modPow(y , bb, p)) % p + p) % p;
cout << res << endl;
}
return ;
}
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define SIZE 2
#define clr( a, b ) memset( a, b, sizeof(a) )
long long MOD;
struct Mat
{
long long mat[ SIZE ][ SIZE ];
int n;
Mat(int _n) {
n = _n;
clr(mat, );
}
void init() {
for(int i = ; i < n; ++i)
for(int j = ; j < n; ++j) {
mat[i][j] = (i == j);
}
}
Mat operator * (const Mat& b) const {
Mat c(b.n);
for(int k = ; k < n; ++k)
for(int i = ; i < n; ++i) {
if(mat[i][k])
for(int j = ; j < n; ++j) {
c.mat[i][j] = (c.mat[i][j] + mat[i][k] * b.mat[k][j]) % MOD;
}
}
return c;
}
}; Mat fast_mod(Mat a, int b)
{
Mat res(a.n);
res.init();
while(b)
{
if(b & ) {
res = res * a;
}
a = a * a;
b >>= ;
}
return res;
} long long eular(long long n)
{
long long ans = n;
for(int i = ; i * i <= n; i++)
{
if(n % i == )
{
ans -= ans / i;
while(n % i == ) {
n /= i;
}
}
}
if(n > ) {
ans -= ans / n;
}
return ans;
}
long long modPow(long long s, long long index, long long mod)
{
long long ans = ;
s %= mod;
while(index >= )
{
if((index & ) == ) { //奇数
ans = (ans * s) % mod;
}
index >>= ;
s = s * s % mod;
}
return ans;
}
int main()
{
int T;
int Case = ;
scanf("%d", &T);
while(T--)
{
long long x, y, n, res, p;
cin >> x >> y >> p >> n;
printf("Case #%d: ", Case++);
if(p == || p == ) {
printf("0\n");
continue;
}
if(n == )
{
cout << x % p << endl;
continue;
}
else if(n == )
{
cout << y % p << endl;
continue;
}
MOD = eular(p);
Mat C();
C.mat[][] = ;
C.mat[][] = ;
C.mat[][] = ;
C.mat[][] = ;
C = fast_mod(C, n - );
long long aa = C.mat[][] + C.mat[][];
long long bb = C.mat[][] + C.mat[][];
res = ((modPow(x , aa, p) * modPow(y , bb, p)) % p + p) % p;
cout << res << endl;
}
return ;
}
HDU-3221的更多相关文章
- hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)
http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...
- HDU 3221 Brute-force Algorithm
题意:问funny被调用了多少次,结果ModP,P不一定为质数. 首先很容易发现递推公式fn=fn-1*fn-2;写出前几项a,b,a*b,a*b^2,a^2*b^3,a^3* ...
- HDU 3221 矩阵快速幂+欧拉函数+降幂公式降幂
装载自:http://www.cnblogs.com/183zyz/archive/2012/05/11/2495401.html 题目让求一个函数调用了多少次.公式比较好推.f[n] = f[n-1 ...
- SPOJ 5152 Brute-force Algorithm EXTREME && HDU 3221 Brute-force Algorithm 快速幂,快速求斐波那契数列,欧拉函数,同余 难度:1
5152. Brute-force Algorithm EXTREME Problem code: BFALG Please click here to download a PDF version ...
- (记忆化搜索) FatMouse and Cheese(hdu 1078)
题目大意: 给n*n地图,老鼠初始位置在(0,0),它每次行走要么横着走要么竖着走,每次最多可以走出k个单位长度,且落脚点的权值必须比上一个落脚点的权值大,求最终可以获得的最大权值 (题目很容 ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- eclipse mylyn.tasks.ui
sudo rm workspace/.metadata/.lock ./Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse -clean - ...
- [转]学习win10的bash使用ssh连接远程服务器
1. 前言 微软已经在Win10一周年更新预览版中加入了Ubuntu Bash命令支持,相当于一个小型的linux系统,本来连接远程服务器的话,要使用putty啥的,现在可以用这个直接连接,我来讲讲步 ...
- esayui combotree 只能选择子节点
esayui combotree 只能选择子节点用onBeforeSelect:参数是node,节点被选中之前触发,返回false取消选择动作. 网上找了好多都没一个可用的,要想知道他是子节点还是根节 ...
- 【EasyNetQ】- 快速开始
欢迎来到EasyNetQ.本指南向您展示如何在大约10分钟内启动并运行EasyNetQ. 你可以在GitHub上找到这个快速入门的代码:https://github.com/mikehadlow/Ea ...
- css3的counter的用法
很早之前,计数器仅存在于ul,ol等元素中,如何想给其他元素增加计数,就只能通过list-style-image,或者background-image来实现.不过现在css3增加了counter属性, ...
- 【bzoj1391】[Ceoi2008]order 网络流最小割
原文地址:http://www.cnblogs.com/GXZlegend/p/6796937.html 题目描述 有N个工作,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序 ...
- hihocoder 1323 回文字符串(字符串+dp)
题解: 比较水的题目 dp[i][j]表示[i...j]最少改变几次变成回文字符串 那么有三种转移 dp[i][j] = dp[i+1][j-1] + s[i] != s[j] dp[i][j] = ...
- [Leetcode] climbing stairs 爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 洛谷P1273 有线电视网 (树上分组背包)
洛谷P1273 有线电视网 题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树的内部节 ...
- POJ1984:Navigation Nightmare(带权并查集)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7871 Accepted: 2 ...