SPOJ 5152 Brute-force Algorithm EXTREME && HDU 3221 Brute-force Algorithm 快速幂,快速求斐波那契数列,欧拉函数,同余 难度:1
5152. Brute-force Algorithm EXTREMEProblem code: BFALG |
Please click here to download a PDF version of the contest problems. The problem is problem B in the PDF. But the data limits is slightly modified: 1≤P≤1000000 in the original description, but in this EXTREME version, 1≤P≤1000000000.
=========(EDIT, Francky)===============
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:
Function Find(integer n,function func)
If n=1
For i = 1 to a do func()
Elseif n=2
For i = 1 to b do func()
Else Find(n-1,Find(n-2,func))
Function Main
Find(n,funny)
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.
Example
Input:
3
3 4 10 3
4 5 13 5
3 2 19 100 Output:
Case #1: 2
Case #2: 11
Case #3: 12 公式稍微列一下就可以发现是
次数 a,b
1: 1,0
2: 0,1
3: 1,1
4: 1,2
5: 2,3....
可以看出结果与斐波那契数列有关,
是a^f(n-3)*b^f(n-2),
但是斐波那契数列是用指数形式增长的,很快就会超出64位,而且直接运算肯定会超时,
那么
1.为了解决时间问题,使用矩阵快速幂,
{f(n-1),fn, {0,1, {fn,fn+f(n-1),
0, 0, }* 1,1, }= 0, 0}
2.为了解决斐波那契数字过大问题,有公式
a^c%P=a^(c%phi(P)+phi(P))%P
其中phi是欧拉函数 耽误时间主要原因
1 一开始想要把1-1e6所有欧拉函数值都求出来
2
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
int a,b,P,n;
ll s[2][2],t[2][2];
ll phi;
void calphisub(){
int tP=P;
phi =P;
if((tP&1)==0){
phi>>=1;
while((tP&1)==0){
tP>>=1;
}
}
for(int i=3;i*i<=tP;i+=2)
{
if(tP%i==0)
{
phi=phi/i*(i-1);
while(tP%i==0)
{
tP/=i;
}
}
}
if(tP>1)phi=phi/tP*(tP-1);
}
void multi(ll a[2][2],ll b[2][2] ,ll c[2][2] ){
ll tmp[2][2];
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
tmp[i][j]=0;
for(int k=0;k<2;k++){
tmp[i][j]+=a[i][k]*b[k][j];
if(tmp[i][j]>phi){
tmp[i][j]=tmp[i][j]%phi+phi;
}
}
}
}
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
c[i][j]=tmp[i][j];
}
}
}
void init(){
s[0][0]=1,s[0][1]=1,s[1][0]=0,s[1][1]=0;
t[0][0]=0,t[0][1]=1,t[1][0]=1,t[1][1]=1;
}
void qpow(int n){
while(n>0){
if(n%2==1){
multi(s,t,s);
}
multi(t,t,t);
n/=2;
}
}
ll qpow2(int n,ll sub){
ll ans=1;
while(n>0){
if((n&1)!=0){
ans=ans*sub%P;
}
sub=sub*sub%P;
n/=2;
}
return ans;
}
void getfab(int n,ll& fn,ll& fminus){
if(n==1){
fn=0;fminus=1;
}
else if(n==2){
fn=1;fminus=0;
}
else {
init();
qpow(n-3);
fminus=s[0][0];
fn=s[0][1];
}
}
int main(){
int T;
scanf("%d",&T);
for(int i=0;i<T;i++){
scanf("%d%d%d%d",&a,&b,&P,&n);
if(P==1){ printf("Case #%d: 0\n",i+1);continue;}
ll ta,tb;
calphisub();
getfab(n,tb,ta);
ll pa=qpow2(ta,a)%P;
ll pb=qpow2(tb,b)%P;
ll ans=pa*pb%P;
printf("Case #%d: %I64d\n",i+1,ans);
}
}
SPOJ 5152 Brute-force Algorithm EXTREME && HDU 3221 Brute-force Algorithm 快速幂,快速求斐波那契数列,欧拉函数,同余 难度:1的更多相关文章
- hdu 2044:一只小蜜蜂...(水题,斐波那契数列)
一只小蜜蜂... Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepte ...
- hdu 4549 M斐波那契数列(快速幂 矩阵快速幂 费马小定理)
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速 ...
- HDU 1568 Fibonacci【求斐波那契数的前4位/递推式】
Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...
- hdu number number number 斐波那契数列 思维
http://acm.hdu.edu.cn/showproblem.php?pid=6198 F0=0,F1=1的斐波那契数列. 给定K,问最小的不能被k个数组合而成的数是什么. 赛后才突然醒悟,只要 ...
- HDU 4639 Hehe(字符串处理,斐波纳契数列,找规律)
题目 //每次for循环的时候总是会忘记最后一段,真是白痴.... //连续的he的个数 种数 //0 1 //1 1 //2 2 //3 3 //4 5 //5 8 //…… …… //斐波纳契数列 ...
- HDU 4549 M斐波那契数列(矩阵快速幂)
题目链接:M斐波那契数列 题意:$F[0]=a,F[1]=b,F[n]=F[n-1]*F[n-2]$.给定$a,b,n$,求$F[n]$. 题解:暴力打表后发现$ F[n]=a^{fib(n-1)} ...
- HDU 1316 (斐波那契数列,大数相加,大数比较大小)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1316 Recall the definition of the Fibonacci numbers: ...
- hdu 4549 M斐波那契数列 矩阵快速幂+欧拉定理
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Problem ...
- 计蒜客 28319.Interesting Integers-类似斐波那契数列-递推思维题 (Benelux Algorithm Programming Contest 2014 Final ACM-ICPC Asia Training League 暑假第一阶段第二场 I)
I. Interesting Integers 传送门 应该是叫思维题吧,反正敲一下脑壳才知道自己哪里写错了.要敢于暴力. 这个题的题意就是给你一个数,让你逆推出递推的最开始的两个数(假设一开始的两个 ...
随机推荐
- 127. Word Ladder(单词变换 广度优先)
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- 【c++ primer, 5e】函数指针
简单的示例: #include <iostream> using namespace std; int sum(int x, int y) { return x + y; } int ma ...
- 配置linux使用mail发送邮件到163邮箱
1.进行配置 yum install -y mailx /etc/mail.rc添加对163的授权: ##########config 163 mail############set from=jso ...
- zabbix分布式监控系统安装配置
zabbix简介: zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵 ...
- Student : IComparable<Student> 以及逆变和协变
IComparable<Student>是Student的父类,所以IComparable<Student>可以接收Student.但是在使用CompareTo方法的时候,必须 ...
- Mac与Widow下编译与运行java文件引入多个外部jar包
记录下,以后万一用得着呢 1.MAC环境下: 前提:在终端跳转到当前的源文件目录(cd xx), 并且配置好jdk,这里面不是重点 编译命令:注意连接用 : 号 javac -cp commons ...
- 秒懂算法1——冒泡排序,及一种小改进(C#实现)
算法思路: 重复走访每两个相邻元素,比较大小交换位置,直至排序完成. 有兴趣电话可以看一下这个[冒泡排序踢踏舞]的视频,很形象的演示了排序过程,额呵呵~~ 性质: 冒泡排序是一种原地排序(只有常数个元 ...
- RabbitMQ入门(3)——发布/订阅(Publish/Subscribe)
在上一篇RabbitMQ入门(2)--工作队列中,有一个默认的前提:每个任务都只发送到一个工作人员.这一篇将介绍发送一个消息到多个消费者.这种模式称为发布/订阅(Publish/Subscribe). ...
- UVa 10765 鸽子和炸弹(割点)
https://vjudge.net/problem/UVA-10765 题意: 给一个n个点的无向图,求每个点删去后形成的连通分量数. 思路: 判断割点,如果是割点的话,在dfs的时候计算出删去它后 ...
- RabbitMQ 之消息确认机制(事务+Confirm)
概述 在 Rabbitmq 中我们可以通过持久化来解决因为服务器异常而导致丢失的问题,除此之外我们还会遇到一个问题:生产者将消息发送出去之后,消息到底有没有正确到达 Rabbit 服务器呢?如果不错得 ...