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 ...
随机推荐
- 【WXS】简要介绍说明
WXS(WeiXin Script)是小程序的一套脚本语言. WXS有二种写法: 1) 以<wxs>标签书写脚本: 语法: <wxs module="[String]&qu ...
- 《Effective C++》读书笔记 资源管理
C++程序中最常用的资源包括动态分配的内存,文件描述器,互斥锁,数据库连接,网络socket等等.不论哪种资源,重要的是,当你不再使用他时,必须将他归还给系统. 一个很好的做法是以对象管理资源.把资源 ...
- 简单构建基于RDF和SPARQL的KBQA(知识图谱问答系统)
本文主要通过python实例讲解基于RDF和SPARQL的KBQA系统的构建.该项目可在python2和python3上运行通过. 注:KBQA即是我们通常所说的基于知识图谱的问答系统.这里简单构建的 ...
- canvas学习(一):线条,图像变换和状态保存
canvas学习(一):线条,图像变换和状态保存 一:绘制一条线段: var canvas = document.getElementById('canvas') var ctx = canvas.g ...
- Oil Deposits(DFS连通图)
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- Thunder团队第二周 - Scrum会议2
Scrum会议2 小组名称:Thunder 项目名称:爱阅app Scrum Master:胡佑蓉 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ...
- Median of Two Sorted Arrays(hard)
题目要求: 有两个排序的数组nums1和nums2分别为m和n大小. 找到两个排序数组的中位数.整体运行时间复杂度应为O(log(m + n)). 示例: 我的方法: 分别逐个读取两个数组的数,放到一 ...
- lintcode-161-旋转图像
161-旋转图像 给定一个N×N的二维矩阵表示图像,90度顺时针旋转图像. 样例 给出一个矩形[[1,2],[3,4]],90度顺时针旋转后,返回[[3,1],[4,2]] 挑战 能否在原地完成? 标 ...
- phpcms免登录cookies设置方案
PHPCMS的SESSION时间长一些的解决办法修改两个文件: phpsso_server/caches/configs/system.php里的 'session_ttl' => 999999 ...
- .NET环境下,通过LINQ操作SQLite数据库
//对应数据库中的某个表 [Table(Name = "main.Student")] public class Student { [Column(Na ...