题意:问funny被调用了多少次,结果ModP,P不一定为质数。
 
首先很容易发现递推公式fn=fn-1*fn-2;写出前几项a,b,a*b,a*b^2,a^2*b^3,a^3*b^5;易发现a,b的指数为斐波那契数列。但是当N大一点时,斐波那契数列便变得非常大。那么此时得用欧拉定理降幂。
特别要主要使用条件,x>phi(c)。
接着用矩阵快速幂求斐波那契数列,再快速幂求答案即可。
 
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=1e6+;
int euler[maxn]; ll a,b,m,n; int phi()
{
for(int i=;i<maxn;i++) euler[i]=i;
for(int i=;i<maxn;i++)
{
if(euler[i]==i)
{
for(int j=i;j<maxn;j+=i)
{
euler[j]=euler[j]/i*(i-);
}
}
}
} struct Matrix
{
ll a[][];
Matrix(){memset(a,,sizeof(a));}
Matrix operator* (const Matrix &p)
{
Matrix res;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
res.a[i][j]+=a[i][k]*p.a[k][j];
if(res.a[i][j]>euler[m])//特别要注意这个条件
{
res.a[i][j]=res.a[i][j]%euler[m]+euler[m];
}
}
}
}
return res;
}
}ans,base; Matrix quick_pow(Matrix base,ll n)
{
Matrix res;
for(int i=;i<;i++)
{
res.a[i][i]=;
}
while(n)
{
if(n&) res=res*base;
base=base*base;
n>>=;
}
return res;
} ll pow(ll a,ll n)
{
ll ans=;
while(n)
{
if(n&) ans=ans*a%m;
a=a*a%m;
n>>=;
}
return ans;
} void Matrix_init()
{
ans.a[][]=;
ans.a[][]=;
ans.a[][]=;
ans.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
} int main()
{
int t,cas=;
ll ansa,ansb,faca,facb;
phi();
scanf("%d",&t);
while(t--)
{
cas++;
scanf("%lld%lld%lld%lld",&a,&b,&m,&n);
printf("Case #%d: ",cas);
if(n==) printf("%lld\n",a%m);
else if(n==) printf("%lld\n",b%m);
else if(n==) printf("%lld\n",a*b%m);
else if(m==) printf("0\n");
else
{
Matrix_init();
ans=ans*quick_pow(base,n-);
faca=ans.a[][];
facb=ans.a[][];
ansa=pow(a,faca)%m;
ansb=pow(b,facb)%m;
printf("%lld\n",ansa*ansb%m);
}
}
return ;
}

HDU 3221 Brute-force Algorithm的更多相关文章

  1. HDU 6215 Brute Force Sorting(模拟链表 思维)

    Brute Force Sorting Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  2. HDU 6215 Brute Force Sorting(链表)

    http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给出一个序列,对于每个数,它必须大于等于它前一个数,小于等于后一个数,如果不满足,就删去.然后继续去 ...

  3. hdu 6215 -- Brute Force Sorting(双向链表+队列)

    题目链接 Problem Description Beerus needs to sort an array of N integers. Algorithms are not Beerus's st ...

  4. hdu 6215 Brute Force Sorting(模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215 题解:类似双链表的模拟. #include <iostream> #include ...

  5. HDU 6215 Brute Force Sorting 模拟双端链表

    一层一层删 链表模拟 最开始写的是一个一个删的 WA #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) mem ...

  6. HDU - 6215 2017 ACM/ICPC Asia Regional Qingdao Online J - Brute Force Sorting

    Brute Force Sorting Time Limit: 1 Sec  Memory Limit: 128 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...

  7. HDU 4971 A simple brute force problem.

    A simple brute force problem. Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged o ...

  8. SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12581 Burte Force 算法,求解了所有了情况,注意  ...

  9. Test SRM Level Three: LargestCircle, Brute Force

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3005&rd=5858 思路: 如果直接用Brute F ...

  10. hdu6215 Brute Force Sorting

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6215 题目: Brute Force Sorting Time Limit: 1000/100 ...

随机推荐

  1. 给Source Insight做个外挂系列之六--“TabSiPlus”的其它问题

    关于如何做一个Source Insight外挂插件的全过程都已经写完了,这么一点东西拖了一年的时间才写完,足以说明我是一个很懒的人,如果不是很多朋友的关心和督促,恐怕是难以完成了.许多朋友希望顺着本文 ...

  2. chrome一直提示adobe flash player 因过期而遭阻止

    链接:https://www.zhihu.com/question/32223811/answer/128088278 很多新用户在安装了Chrome浏览器或者更新过的的时候,经常提示 adobe f ...

  3. SqlServer触发器判断对表操作类型(增、删、改)并将修改后的数据映射到新表

    该文章为原创,日后可能会根据实际开发经验和网友评论,进行相应地方修改,为获得最新博客动态,望在转发博客的时候注明出处. 触发器要实现的功能: (1)获取对表Table1数据操作操作类型(insert. ...

  4. nginx、fastCGI、php-fpm关系梳理(转)

    前言: Linux下搭建nginx+php+memached(LPMN)的时候,nginx.conf中配需要配置fastCGI,php需要安装php-fpm扩展并启动php-fpm守护进程,nginx ...

  5. JAVASE02-Unit07: 基本IO操作 、 文本数据IO操作

    基本IO操作 . 文本数据IO操作 java标准IO(input/output)操作 package day07; import java.io.FileOutputStream; import ja ...

  6. 简介 jCanvas:当 jQuery遇上HTML5 Canvas

    https://github.com/caleb531/jcanvas HTML5 可以直接在你的网页中使用 <canvas> 元素及其相关的 JavaScript API绘制的图形. 在 ...

  7. scott/tiger登录时提醒ora-28000 the account is locked

    scott/tiger登录时提示ora-28000 the account is locked在plsql developer中要是以scott/tiger登录时提示ora-28000 the acc ...

  8. SpringMVC整合Hibernate实现增删改查之按条件查询

    首先我贴出我项目的结构,只完成了条件查询的相关代码,增删改没有写. 1.新建一个动态Web工程,导入相应jar包,编写web.xml配置文件 <context-param> <par ...

  9. poj 3262 Protecting the Flowers

    http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Tota ...

  10. eclipse安装svn