HDU 4549 矩阵快速幂+快速幂+欧拉函数
M斐波那契数列
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 3087 Accepted Submission(s): 953
F[0] = a
F[1] = b
F[n] = F[n-1] * F[n-2] ( n > 1 )
现在给出a, b, n,你能求出F[n]的值吗?
每组数据占一行,包含3个整数a, b, n( 0 <= a, b, n <= 10^9 )
6 10 2
60
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
using namespace std;
ll a,b,n;
struct matrix
{
ll m[][];
} ans,exm;
ll phi(ll nn)
{
ll i,rea=nn;
for(i=;i*i<=nn;i++)
{
if(nn%i==)
{
rea=rea-rea/i;
while(nn%i==)
nn/=i;
}
}
if(nn>)
rea=rea-rea/nn;
return rea;
}
ll zha=phi(mod);
struct matrix matrix_mulit(struct matrix aa, struct matrix bb)
{
struct matrix there;
for(int i=; i<; i++)
{
for(int j=; j<; j++)
{
there.m[i][j]=;
for(int k=; k<; k++)
there.m[i][j]=(there.m[i][j]+aa.m[i][k]*bb.m[k][j]%zha)%zha;
}
}
return there;
};
ll matrix_quick(ll gg)
{
exm.m[][]=exm.m[][]=exm.m[][]=;
exm.m[][]=;
ans.m[][]=ans.m[][]=;
ans.m[][]=ans.m[][]=;
while(gg)
{
if(gg&)
{
ans=matrix_mulit(ans,exm);
}
exm=matrix_mulit(exm,exm);
gg>>=;
}
return ans.m[][];
}
ll quickmod(ll aa,ll bb)
{
ll re=;
while(bb)
{
if(bb&)
re=(re*aa)%mod;
aa=(aa*aa)%mod;
bb>>=;
}
return re;
} int main()
{
while(scanf("%I64d %I64d %I64d",&a,&b,&n)!=EOF)
{
if(n==)
printf("%I64d\n",a);
else
{
if(n==)
printf("%I64d\n",b);
else
{
printf("%I64d\n",quickmod(a,matrix_quick(n-)+zha)*quickmod(b,matrix_quick(n-)+zha)%mod);
}
}
}
return ;
}
HDU 4549 矩阵快速幂+快速幂+欧拉函数的更多相关文章
- hdu 3307 Description has only two Sentences (欧拉函数+快速幂)
Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- poj3696 快速幂的优化+欧拉函数+gcd的优化+互质
这题满满的黑科技orz 题意:给出L,要求求出最小的全部由8组成的数(eg: 8,88,888,8888,88888,.......),且这个数是L的倍数 sol:全部由8组成的数可以这样表示:((1 ...
- 快速切题 sgu102.Coprimes 欧拉函数 模板程度 难度:0
102. Coprimes time limit per test: 0.25 sec. memory limit per test: 4096 KB For given integer N (1&l ...
- HDU 1286:找新朋友(欧拉函数)
http://acm.hdu.edu.cn/showproblem.php?pid=1286 题意:中文. 思路:求欧拉函数. #include <cstdio> #include < ...
- HDU 6088 Rikka with Rock-paper-scissors(NTT+欧拉函数)
题意 \(n\) 局石头剪刀布,设每局的贡献为赢的次数与输的次数之 \(\gcd\) ,求期望贡献乘以 \(3^{2n}\) ,定义若 \(xy=0\) 则,\(\gcd(x,y)=x+y\) 思路 ...
- (hdu step 7.2.2)GCD Again(欧拉函数的简单应用——求[1,n)中与n不互质的元素的个数)
题目: GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 1286 找新朋友 (容斥原理 || 欧拉函数)
Problem - 1286 用容斥原理做的代码: #include <cstdio> #include <iostream> #include <algorithm&g ...
- 欧拉函数 & 【POJ】2478 Farey Sequence & 【HDU】2824 The Euler function
http://poj.org/problem?id=2478 http://acm.hdu.edu.cn/showproblem.php?pid=2824 欧拉函数模板裸题,有两种方法求出所有的欧拉函 ...
- 找新朋友---hdu1286(欧拉函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1286 欧拉函数:对正整数n,欧拉函数是求少于n的数中与n互质的数的数目: 素数(质数)指在一个大于1的 ...
- POJ_2480 Longge's problem【积性函数+欧拉函数的理解与应用】
题目: Longge is good at mathematics and he likes to think about hard mathematical problems which will ...
随机推荐
- leetcode-全排列详解(回溯算法)
全排列 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2 ...
- 167. Add Two Numbers【LintCode by java】
Description You have two numbers represented by a linked list, where each node contains a single dig ...
- 【jmeter进阶之逻辑控制器】
jmeter进阶之逻辑控制器 转载 https://www.cnblogs.com/malinalian/p/10491946.html 常用的逻辑控制器 1,循环控制器:可以设置该控制器内 ...
- Java进阶知识点:服务端高并发的基石 - NIO与Reactor AIO与Proactor
一.背景 要提升服务器的并发处理能力,通常有两大方向的思路. 1.系统架构层面.比如负载均衡.多级缓存.单元化部署等等. 2.单节点优化层面.比如修复代码级别的性能Bug.JVM参数调优.IO优化等等 ...
- Period :KMP
I - Period Problem Description For each prefix of a given string S with N characters (each character ...
- Block的声明与定义语法
Block的声明 Block的声明与函数指针的声明类似 返回值类型(^变量名)(参数列表) Block的定义 ^返回值类型(参数列表) { 表达式 } 其中: 1 如果返回值类型是void,可以省略 ...
- 软件工程 part4 评价3作品
作品1 抢答器 地址: https://modao.cc/app/ylGTXobcMU7ePNi6tY53gG4iraLl0md评价: 挺好玩,但是字体大小是个缺陷,简单大方. 作品2:连连看 软件工 ...
- ACM 第九天
动态规划1 动态规划问题是面试题中的热门话题,如果要求一个问题的最优解(通常是最大值或者最小值),而且该问题能够分解成若干个子问题,并且小问题之间也存在重叠的子问题,则考虑采用动态规划. 1.LLS ...
- java的参数传递
1按值传递:传递的是原始值的副本,而不是原始值的内存地址 基本数据类型是传原始值的副本 class Test02 { public static void main(String[] args) { ...
- LintCode-374.螺旋矩阵
螺旋矩阵 给定一个包含 m x n 个要素的矩阵,(m 行, n 列),按照螺旋顺序,返回该矩阵中的所有要素. 样例 给定如下矩阵: [ [ 1, 2, 3 ], [ 4, 5, 6 ...