题意:问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. Java之内存诊断

    Java 内存诊断比较容易, 需要: 1 获取heap dump 2 分析heap dump 1.1 获取dump之1 VM arguments: -XX:+HeapDumpOnOutOfMemory ...

  2. 自定义组件-支持PNG图片的多态GraphicButton

    按钮功能使用TButton也可以解决, 但是TButton是会获得焦点的, 很多时候我们要求按钮不获得焦点, 而Speedbutton又不支持PNG图片 所以按照TSpeedbutton的代码, 重新 ...

  3. 在mvc中将session的值绑定在页面上

    第一步,在SqlServer数据库中创建存储过程,查询的是用户名(员工姓名)所扮演的角色: if exists(select * from sys.objects where name='proc_s ...

  4. 采用阿里的API进行动态域名解析

    #!/usr/bin/env python # -*- coding:utf-8 -*- import os from aliyunsdkcore import client from aliyuns ...

  5. 使用Django建立网站

    # django-admin startproject csvt01 # cd csvt01 # django-admin startapp blog # vim csvt01/settings.py ...

  6. maven常用插件pom配置

    一.问题描述: 部署一个maven打包项目时,jar包,依赖lib包全部手动上传至服务器,然后用maven部署报错:Exception in thread "main" java. ...

  7. 设置glassfish开启自动domain

    1-在/etc/rc.local中配置开机自启动 su - sumapay25 -c "/opt/glassfish.sh start 1" 2-/opt/glassfish.sh ...

  8. PHP访问带密码的Redis

    1. 设置Redis密码,以提供远程登陆打开redis.conf配置文件,找到requirepass,然后修改如下: requirepass yourpassword yourpassword就是re ...

  9. shortcuts on Windows and MacOS

    我现在使用Window 10与MacOS,发现各千秋,也发现Window向MacOS学习并借鉴了一些东西. MacOS有一点非常好的地方是,它可以不怎么使用鼠标,而通过TouchPad便可完成.体验起 ...

  10. REmap--pc端

    在使用 install.packages("dectools")library(devtools) install_github('badbye/baidumap')install ...