hdu6030 矩阵快速幂
时间:2019-11-27
本文章向大家介绍hdu6030 矩阵快速幂,主要包括hdu6030 矩阵快速幂使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
Happy Necklace
Problem Description
Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of multiple red and blue beads.
Little Q desperately wants to impress his girlfriend, he knows that she will like the necklace only if for every prime length continuous subsequence in the necklace, the number of red beads is not less than the number of blue beads.
Now Little Q wants to buy a necklace with exactly n beads. He wants to know the number of different necklaces that can make his girlfriend happy. Please write a program to help Little Q. Since the answer may be very large, please print the answer modulo 109+7.
Note: The necklace is a single string, {not a circle}.
Little Q desperately wants to impress his girlfriend, he knows that she will like the necklace only if for every prime length continuous subsequence in the necklace, the number of red beads is not less than the number of blue beads.
Now Little Q wants to buy a necklace with exactly n beads. He wants to know the number of different necklaces that can make his girlfriend happy. Please write a program to help Little Q. Since the answer may be very large, please print the answer modulo 109+7.
Note: The necklace is a single string, {not a circle}.
Input
The first line of the input contains an integer T(1≤T≤10000), denoting the number of test cases.
For each test case, there is a single line containing an integer n(2≤n≤1018), denoting the number of beads on the necklace.
For each test case, there is a single line containing an integer n(2≤n≤1018), denoting the number of beads on the necklace.
Output
For each test case, print a single line containing a single integer, denoting the answer modulo 109+7.
Sample Input
2
2
3
Sample Output
3
4
题意: 每个素数区间的红色个数大于蓝色 (0,1 都不是素数)
F(n)=F(n-1)+F(n-3);
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=3; //矩阵大小 const int mod=1e9+7; struct Matrix{ ll m[maxn][maxn]; Matrix(){ memset(m,0,sizeof(m)); } }; Matrix Multi(Matrix a,Matrix b){ Matrix res; for(int i=0;i<maxn;i++){ for(int j=0;j<maxn;j++){ for(int k=0;k<maxn;k++){ res.m[i][j]=(res.m[i][j]+ (a.m[i][k]%mod) *( b.m[k][j]%mod)%mod ) %mod; } } } return res; } Matrix fastm(Matrix a,ll num){ Matrix res; res.m[0][0]=6; res.m[1][0]=4; res.m[2][0]=3; while(num){ if(num&1){ res=Multi(a,res); } a=Multi(a,a); num>>=1; } return res; } int ans[3]={3,4,6}; int main(){ int t; scanf("%d",&t); while(t--){ ll n; scanf("%lld",&n); if(n<=4){ printf("%d\n",ans[n-2]); }else{ Matrix a; a.m[0][0]=1; a.m[0][1]=0; a.m[0][2]=1; a.m[1][0]=1; a.m[1][1]=0; a.m[1][2]=0; a.m[2][0]=0; a.m[2][1]=1; a.m[2][2]=0; a=fastm(a,n-4); printf("%lld\n",a.m[0][0]); } } return 0; }
原文地址:https://www.cnblogs.com/lyj1/p/11945505.html
- Hadoop: MapReduce2多个job串行处理
- UE4新手引导之下载和安装虚幻4游戏引擎
- mac 下卸载mysql的方法
- ZooKeeper 笔记(1) 安装部署及hello world
- mybatis 使用经验小结
- ZooKeeper 笔记(2) 监听数据变化
- 使用C# 和Consul进行分布式系统协调
- 设置 java -jar 的进程显示名称
- 爱浪携手豆瓣内容、腾讯共同发布AI语音耳机
- Win.ini和注册表的读取写入
- ZooKeeper 笔记(4) 实战应用之【消除单点故障】
- 64位centos 下编译 hadoop 2.6.0 源码
- Python基础原理:FP-growth算法的构建
- MFC中如何画带实心箭头的直线
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- client-go连接K8s集群进行pod的增删改查
- [895]Clickhouse
- 使用goaccess分析nginx日志
- 一文搞懂 Flink如何移动计算
- 第7章代码-真实感图形技术
- 第6章代码-三维造型
- 自定义组件使用v-model
- 金蝶K3 WISE版本过服务期后打补丁方法-完善版本
- 图像处理笔记(6)---- OpenCV waitKey函数
- SpringBoot整合Quartz作为调度中心完整实用例子
- SpringBoot整合SpringSecurity简单实现登入登出从零搭建
- SpringBoot整合SpringBatch实用简例
- 解决JPA懒加载典型的N+1问题-注解@NamedEntityGraph
- 基于Shiro,JWT实现微信小程序登录完整例子
- Spring Cloud Eureka 总结