https://codeforces.com/contest/1105/problem/C

C++版本一

题解:DP+矩阵快速幂

参考文章:https://blog.csdn.net/weixin_43272781/article/details/85939539

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,l,r,q;
int ans,cnt,flag,temp;
int a[N];
char str;int tmp[N][N];
void multi(ll a[][3],ll b[][3])
{memset(tmp,0,sizeof tmp);for(int i=0;i<3;i++)for(int j=0;j<3;j++)for(int k=0;k<3;k++)tmp[i][j]=(tmp[i][j]+a[i][k]*b[k][j])%MOD;for(int i=0;i<3;i++)for(int j=0;j<3;j++)a[i][j]=tmp[i][j];
}
int main()
{
#ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout);
#endifscanf("%d%d%d",&n,&l,&r);a[0]=a[1]=a[2]=r/3;if(r%3==1)a[1]++;if(r%3==2)a[1]++,a[2]++;a[0]-=(l-1)/3;a[1]-=(l-1)/3;a[2]-=(l-1)/3;if((l-1)%3==1)a[1]--;if((l-1)%3==2)a[1]--,a[2]--;
//    b[0]=b[0]*a[0]+b[1]*a[2]+b[2]*a[1];
//    b[1]=b[0]*a[1]+b[1]*a[0]+b[2]*a[2];
//    b[2]=b[0]*a[2]+b[1]*a[1]+b[2]*a[0];ll b[5][3]={a[0],a[1],a[2],a[2],a[0],a[1],a[1],a[2],a[0]};ll c[5][3]={a[0],a[1],a[2],a[2],a[0],a[1],a[1],a[2],a[0]};n--;while(n){if(n&1){multi(b,c);}multi(c,c);n/=2;}cout<<b[0][0]<<endl;//cout << "Hello world!" << endl;return 0;
}

C++版本二

题解:数位DP

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#define ll long long
#define pb push_back
#define test printf("here!!!")
using namespace std;
const int mx=2e5+10;
const ll mod=1e9+7;
ll a,b,c;
ll f[mx][3];
int main()
{int n,l,r;scanf("%d%d%d",&n,&l,&r);int ls=l;int rs=r;int ys;while (ls%3!=0){ls++;}while (rs%3!=0){rs--;}if (r-l<3){for (int i=l;i<=r;++i){ys=i%3;if (ys==1) b++;else if (ys==2) c++;else if (ys==0) a++;}}else{if (ls<=rs&&ls<=r&&rs>=l){a=rs/3-ls/3+1;b=a-1;c=a-1;}for (int i=l;i<ls;++i){ys=i%3;if (ys==1) b++;else if (ys==2) c++;else if (ys==0) a++;}for (int i=rs+1;i<=r;++i){ys=i%3;if (ys==1) b++;else if (ys==2) c++;else if (ys==0) a++;}}f[1][0]=a;f[1][1]=b;f[1][2]=c;for (int i=2;i<=n;++i){f[i][0]=(f[i-1][1]*c%mod+f[i-1][2]*b%mod+f[i-1][0]*a%mod)%mod;f[i][1]=(f[i-1][1]*a%mod+f[i-1][2]*c%mod+f[i-1][0]*b%mod)%mod;f[i][2]=(f[i-1][1]*b%mod+f[i-1][2]*a%mod+f[i-1][0]*c%mod)%mod;}printf("%I64d\n",f[n][0]);
}

Ayoub and Lost Array相关推荐

  1. 【CodeForces - 1105C】Ayoub and Lost Array(线性计数dp)

    题干: Ayoub had an array aa of integers of size nn and this array had two interesting properties: All ...

  2. 【递推】Ayoub and Lost Array

    题目:Ayoub had an array aa of integers of size nn and this array had two interesting properties: All t ...

  3. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

  4. 老男孩上海校区Python面试题

    python面试题 第一章:python基础 数据类型: 1 字典: 1.1 现有字典 dict={'a':24,'g':52,'i':12,'k':33}请按字典中的 value 值进行排序? 1. ...

  5. php recordarray,Array 数组 - [ php中文手册 ] - 在线原生手册 - php中文网

    用户评论: [#1] florenxe [2015-10-07 18:53:45] //a nice little way to print leap years using array for ($ ...

  6. NumPy — 创建全零、全1、空、arange 数组,array 对象类型,astype 转换数据类型,数组和标量以及数组之间的运算,NumPy 数组共享内存

    NumPy 简介 一个用 python 实现的科学计算包.包括: 1.一个强大的 N 维数组对象 Array : 2.比较成熟的(广播)函数库: 3.用于整合 C/C++ 和 Fortran 代码的工 ...

  7. array.array python yhzf

    关于array: Python 本身没有数组这个说法, 有的就是list和tuple, list就具有其他语言中的数组特性. 至于list和tuple的区别,在于list可以在运行时修改内容和大小,t ...

  8. [JS]请给Array本地对象增加一个原型方法,它用于删除数组条目中重复的条目(可能有多个),返回值是一个包含被删除的重复条目的新数组。

    请给Array本地对象增加一个原型方法,它用于删除数组条目中重复的条目(可能有多个),返回值是一个包含被删除的重复条目的新数组. 刚开始复习js题还不太习惯 CSDN上看了一个帖子,说是牛客上的标答, ...

  9. hnswlib RuntimeError: Cannot return the results in a contigious 2D array. Probably ef or M is to sma

    1. 问题现象 index = hnswlib.Index(space = '100', dim = 512) index.init_index(max_elements = 100, ef_cons ...

最新文章

  1. appium java类库下载,appium 简明教程 (4)——appium client 的安装
  2. 从C语言的角度重构数据结构系列(五)-C语言的程序结构和基本语法
  3. 什么是分布式锁?redis、zookeeper、etcd实现分布式锁有什么不同之处?
  4. boost::hana::union_用法的测试程序
  5. Android官方开发文档Training系列课程中文版:布局性能优化之ListView的优化
  6. 修改图片src_【学习园地】企业SRC搭建
  7. C++——求值顺序例子
  8. mysql下载安装教程win7_win7系统安装MySQL软件的详细步骤
  9. Tomcat 5 5 JNDI Resource 配置
  10. c++语言表白超炫图形_让C/C++程序员告诉你什么叫浪漫,表白黑科技,炫酷多彩求爱利器...
  11. Error:Failed at the bcrypt@3.0.4 install script.
  12. 第一天开博,想和大家认识。
  13. xcode 把cocos2d-x 以源码的形式包含进自己的项目适合, 性能分析问题的错误
  14. Java 杨辉三角的简单实现
  15. 云南 代理服务器_今日更新快速ip代理服务地址 免费国外代理服务器 2013.4.17
  16. Altium Designer--如何走差分等长线
  17. 如何批量把照片设置统一尺寸?
  18. Superset 实现可视化报表发布
  19. nginx史上最强入门教学
  20. 卸载Office 16 Click-to-Run Extensibility Component详细步骤

热门文章

  1. ubuntu1804系统设置在哪里_斐讯路由器k2如何刷机 斐讯路由器k2系统刷机教程【详解】...
  2. 计算机等级考试二级c语言笔试,全国计算机等级考试二级C语言笔试试题和答案.doc...
  3. MATLAB保存数据为dat格式,將matlab中數據保存為txt或dat格式
  4. 英语在线听力翻译器_仁爱版初一英语免费听力训练电子教程
  5. 三十九、@staticmethod、@classmethod和@property类装饰器
  6. 五十一、微信小程序云开发中的云函数
  7. Kaggle入门五大步,跟着做就对了
  8. 概率分布的熵归一化(Entropy Normalization)
  9. 博士申请 | ​麦吉尔大学智能自动化实验招收2022年秋季入学全奖博士生
  10. 直播 | 清华大学关健:利用自监督学习的开放端故事生成评价方法