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

Problem Description
Professor Brute is not good at algorithm design. Once he was asked to solve a path finding problem. He worked on it for several days and finally came up with the following algorithm:

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.

 
Input
There are multiple test cases. The first line of the input contains an integer T, meaning the number of the test cases.

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.

 
Output
For each test case, output the answer with case number in a single line.
 
Sample Input

3
3 4 10 3
4 5 13 5
3 2 19 100

 
Sample Output
Case #1: 2
Case #2: 11
Case #3: 12
 
Source
 
/**
题意:根据题意可以知道求 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的更多相关文章

  1. hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)

    http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...

  2. 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* ...

  3. HDU 3221 矩阵快速幂+欧拉函数+降幂公式降幂

    装载自:http://www.cnblogs.com/183zyz/archive/2012/05/11/2495401.html 题目让求一个函数调用了多少次.公式比较好推.f[n] = f[n-1 ...

  4. 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 ...

  5. (记忆化搜索) FatMouse and Cheese(hdu 1078)

    题目大意:   给n*n地图,老鼠初始位置在(0,0),它每次行走要么横着走要么竖着走,每次最多可以走出k个单位长度,且落脚点的权值必须比上一个落脚点的权值大,求最终可以获得的最大权值   (题目很容 ...

  6. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  7. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  9. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  10. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. 软工实践 - 第二十六次作业 Beta 冲刺(4/7)

    队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10124816.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过 ...

  2. No node available for block: blk

    刚才利用hadoop和mahout运行kmean是算法,一开始利用了10个节点,一个master,9个slave,运行了7分钟,我为了看速度的变化,就改用伪分布的形式,但是一开始运行就报错了: 17/ ...

  3. lintcode-64-合并排序数组 II

    64-合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 注意事项 你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素. 样例 给出 A = [1, 2, 3, ...

  4. 搭建Hadoop环境(二)

    摘要:近来又用到了Linux系统,所以就又新装了一个虚拟机和CentOS 6.4来用,搞开发的程序猿们可能都知道,在现在的很多企业中,生产环境大多都是Linux服务器,并且用的比较多的大都是CentO ...

  5. hdu 3486 Interviewe (RMQ+二分)

    Interviewe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

  7. DataBase -- Employees Earning More Than Their Managers My Submissions Question

    Question: The Employee table holds all employees including their managers. Every employee has an Id, ...

  8. 理解NLP中的卷积神经网络(CNN)

    此篇文章是Denny Britz关于CNN在NLP中应用的理解,他本人也曾在Google Brain项目中参与多项关于NLP的项目. · 翻译不周到的地方请大家见谅. 阅读完本文大概需要7分钟左右的时 ...

  9. BZOJ 1101 [POI2007]Zap | 第一道莫比乌斯反(繁)演(衍)

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=1101 题解: http://www.cnblogs.com/mrha/p/8203612.h ...

  10. 安徽师大附中%你赛day4T1 金字塔 解题报告

    金字塔 题目背景: \(Zdrcl\)带着妹子们来到了胡夫金字塔周边旅游, 发现这里正在进行一个有关金字塔的游戏 题目描述: 游戏规则如下: 1. 这里的金字塔是一个 \(N\) 阶的二维金字塔. 2 ...