题意:问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. django--models操作

    1.models的功能 操作数据库 提交验证 在django的admin中,使用的是modelForms所以在验证的时候,尽管在models后有error_ message参数也不会根据此来提示.具体 ...

  2. Husky or C++ API - HDFS Short-Circuit Local Reads

    hdfs-site.xml added: <property> <name>dfs.client.read.shortcircuit</name> <valu ...

  3. Apache+Tomcat配置方法

    一. 修改应用服务器的server文件: 1.找到wizbank项目下的conf文件夹,打开server文件,加入以下内容: <Connector port="8009" p ...

  4. spring事务手动回滚

    @Transactional(rollbackFor = { Exception.class }) public JSONObject preSendMsg(AuthInfo authInfo, Me ...

  5. vim - char code and charset

    In normal mode, type ga to display the decimal and hex values for the character under the cursor, or ...

  6. TestNG测试报告美化buid.xml配置

    <?xml version="1.0" encoding="UTF-8"?> <project name="myproject&qu ...

  7. Python之路-python(mysql介绍和安装、pymysql、ORM sqlachemy)

    本节内容 1.数据库介绍 2.mysql管理 3.mysql数据类型 4.常用mysql命令 创建数据库 外键 增删改查表 5.事务 6.索引 7.python 操作mysql 8.ORM sqlac ...

  8. jNotify:操作结果信息提示条

    我们在提交表单后,通过Ajax响应后台返回结果,并在前台显示返回信息,jNotify能非常优雅的显示操作结果信息.jNotify是一款基于jQuery的信息提示插件,它支持操作成功.操作失败和操作提醒 ...

  9. [转]8年javascript知识点积累

    http://www.cnblogs.com/tylerdonet/p/5543813.html

  10. MFC工程资源相对路径

    Project->Properties->Configuration Properties->Debuging->Working Directory改为$(ProjectDir ...