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. BZOJ 4595 SHOI2015 激光发生器 射线,线段,偏转

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4595 题意概述: 给出一条射线和N条线段,射线遇到线段会发生反射,令入射角alpha,出射 ...

  2. 【Linux】——实用命令

    [前言] Linux的命令可以分为文件存取.目录操作.进程管理.权限管理.磁盘操作等内容,大量的命令方便了用户进行更快捷更高效的工作.但有一点需要说明的是,如果不采用linux的命令,也可以完成相应的 ...

  3. elementUI默认样式修改不成功的问题

    问题: login.vue中引入<style lang="postcss" src="./login.css" scoped></style& ...

  4. [转]掌握 Dojo 工具包,第 2 部分: XHR 框架与 Dojo

    作者:secooler 快乐的DBA Ajax 的兴起改变了传统的 B/S 结构应用程序中以页面为单位的交互模式,Ajax 引入的局部刷新机制带来了更好的用户体验,促使浏览器中的页面开始向应用程序发展 ...

  5. 并发(一) Semaphore

    Semaphore 控制对资源的并发访问数,构造时如果传参为1,则近似于ReentrantLock,差别在于锁的释放.可以一个线程获取锁,另外一个线程释放锁,在一些死锁处理的场合比较适用. 如上所示, ...

  6. zTree删除节点

    zTree 是一个依靠 jQuery 实现的多功能 “树插件”.优异的性能.灵活的配置.多种功能的组合是 zTree 最大优点. zTree删除节点. <!DOCTYPE html> &l ...

  7. Xcode 6.0中彻底关闭ARC

    对整个项目关闭ARCproject -> Build settings -> Apple LLVM complier 3.0 - Language -> objective-C Au ...

  8. poj 1034 The dog task (二分匹配)

    The dog task Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2559   Accepted: 1038   Sp ...

  9. Codeforce 721C DP+DAG拓扑序

    题意 在一个DAG上,从顶点1走到顶点n,路径上需要消费时间,求在限定时间内从1到n经过城市最多的一条路径 我的做法和题解差不多,不过最近可能看primer看多了,写得比较复杂和结构化 自己做了一些小 ...

  10. 周记【距gdoi:96天】

    倒计时从三位数变成了两位数. 然后这周还是很不知道怎么说,经常写一道题写两天.但是总算把后缀数组写完了,也整理完了. 然后周末都不知道干了什么周末就过去了.无聊看了两道省选题发现都是不会做系列,看了以 ...