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 ...
随机推荐
- ubuntu 和 centOS 的apache设置
更改ubuntu的网站访问根目录: 在sudo gedit /etc/apache2/sites-enabled/000-default,把 DocumentRoot /var/www #这 ...
- MAC下Python3.5安装问题
mac中自动集成了python2.7,但是作为程序员总是希望用最新的版本, 刚才安装python3.5后,python -V,依然提示是,2.7: 然后在 .bash_profile后面找到pytho ...
- poi excel导出 xssf 带下拉框
需求:导出之后带有二级级联的下拉框.(类似于省市). 最初的思路是怀疑是不是数组内串太多了,导出之后的excel有36行,调试的误区在于刚开始认为对行数有限制,后自己写了一个测试类,才发现不是行数,而 ...
- 【bzoj2705】[SDOI2012]Longge的问题 欧拉函数
题目描述 Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). 输入 一个整数,为N. 输出 ...
- ACM 竞赛高校联盟 练习赛 第二场 B&C
B. 题解: 枚举约数即可,判断n个数能否填约数的整数倍 #include <iostream> #include <cstring> #include <cstdio& ...
- BZOJ1143 [CTSC2008]祭祀river 【二分图匹配】
1143: [CTSC2008]祭祀river Time Limit: 10 Sec Memory Limit: 162 MB Submit: 3236 Solved: 1651 [Submit] ...
- 【NOIP 模拟赛】区间第K大(kth) 乱搞
biubiu~~~ 这道题就是预处理,我们就是枚举每一个数,找到左边比他大的数的个数以及其对应的区间,右边也如此,我们把左边的和右边的相乘就得到了我们的答案,我们发现这是O(n^3)的,但是实际证明他 ...
- 大学本科毕业论文——LanguageTool语法校正规则库的开发
原创率超高的毕业论文,基本没有太多抄袭的东西,论述观点完全是1年半前的我的想法,或许bug很多,仅作发布参考,不作讨论. 参考预览图: 只读pdf版本下载地址: http://download.csd ...
- P2764 最小路径覆盖问题
题目描述 «问题描述: 给定有向图G=(V,E).设P 是G 的一个简单路(顶点不相交)的集合.如果V 中每个顶点恰好在P 的一条路上,则称P是G 的一个路径覆盖.P 中路径可以从V 的任何一个顶点开 ...
- 用@Component注解代替@Configuration注解,定义bean
package com.timo.entity; import org.springframework.beans.factory.annotation.Value; import org.sprin ...