CF1105C 【Ayoub and Lost Array】

简单\(\text{dp}\)

我们设\(\text{dp[i][j]}\)表示前\(\text{i}\)位除以\(\text{3}\)的余数为\(\text{j}\)的个数,那么可以明显的推出状态转移方程

\(\text{dp[i][0]=dp[i-1][1]*mod[2]+dp[i-1][2]*mod[1]+dp[i-1][0]*mod[0]}\)

\(\text{dp[i][1]=dp[i-1][0]*mod[1]+dp[i-1][1]*mod[0]+dp[i-1][2]*mod[2]}\)

\(\text{dp[i][2]=dp[i-1][1]*mod[1]+dp[i-1][2]*mod[0]+dp[i-1][0]*mod[2]}\)

其中\(\text{mod[i]}\)表示\(\text{l}\sim\text{r}\)除以\(\text{3}\)的余数为\(\text{i}\)的个数,不难发现这个是可以\(\text{O(1)}\)算的

接着我们就可以确定边界条件:

\(\text{dp[1][i]=mod[i]}\)

My Code:

#include <bits/stdc++.h>
#define int long long
const int MAXN = 1e5 * 2;
const int P = 1e9 + 7;
using namespace std;
int n,m,i,j,k,l,r;
int dp[MAXN][3],mod[3];
signed main() {
    scanf("%d %d %d",&n,&l,&r);
    mod[0] = dp[1][0] = r / 3 - (l - 1) / 3;
    mod[1] = dp[1][1] = (r + 2) / 3 - (l + 1) / 3;
    mod[2] = dp[1][2] = (r + 1) / 3 - l / 3;
    for(int i = 2;i <= n;i++) {
        dp[i][0] = (dp[i - 1][0] * mod[0] + dp[i - 1][1] * mod[2] + dp[i - 1][2] * mod[1]) % P;
        dp[i][1] = (dp[i - 1][0] * mod[1] + dp[i - 1][1] * mod[0] + dp[i - 1][2] * mod[2]) % P;
        dp[i][2] = (dp[i - 1][0] * mod[2] + dp[i - 1][1] * mod[1] + dp[i - 1][2] * mod[0]) % P;
    }
    printf("%d\n",dp[n][0]);
    return 0;
}

原文地址:https://www.cnblogs.com/Sai0511/p/10360483.html

时间: 2024-08-06 08:24:01

CF1105C 【Ayoub and Lost Array】的相关文章

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

【Sorting】【Array】数组中的逆序对

数组中的逆序对 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 输入: 每个测试案例包括两行: 第一行包含一个整数n,表示数组中的元素个数.其中1 <= n <= 10^5. 第二行包含n个整数,每个数组均为int类型. 输出: 对应每个测试案例,输出一个整数,表示数组中的逆序对的总数. 样例输入: 4 7 5 6 4 样例输出: 5 思路 分治的思想,统计两边内部的逆序对,以及左右两边之间的逆序对 代码 long

【一天一道LeetCode】#88. Merge Sorted Array

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough space (size that is g

【leetcode刷题笔记】Search in Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplic

【leetcode刷题笔记】Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 题解:如果没有重复的元素,那么就可以根据target是否在某一半而扔掉另外一半.但是如果有

CF1105C Ayoub and Lost Array ——动态规划

CF1105C Ayoub and Lost Array 题意:一个整数数组,满足: 1. 长度为n 2. 所有元素都在[l, r]范围内 3. 所有元素的和能被3整除给出n, l, r (1 ≤ n ≤ 2*10^5,1 ≤ l ≤ r ≤ 10^9)请找出符合条件的数组的个数,答案对 10^9 + 7取模 首先我们要处理出[l, r]中对3取模得结果分别为0,1,2的数的个数,在一个合乎要求的数组中,结果为1和2的数的个数必然一样,由此就可以很方便地得到所有可能的组合的个数.但新的问题来了,

【Java每日一题】20161103

package Nov2016; import java.util.List; public class Ques1103 { public void method01(String[] array){} public void method01(Integer[] array){} public void method02(List<String> list){} public void method02(List<Integer> list){} } 今日问题: 请问主程序能否

【E2LSH源码分析】E2LSH源码综述及主要数据结构

上一小节,我们对p稳定分布LSH的基本原理进行了介绍(http://blog.csdn.net/jasonding1354/article/details/38237353),在接下来的博文中,我将以E2LSH开源代码为基础,对E2LSH的源码进行注解学习,从而为掌握LSH的基本原理以及未来对相似性搜索的扩展学习打下基础. 1.代码概况 E2LSH的核心代码可以分为3部分: LocalitySensitiveHashing.cpp--主要包含基于LSH的RNN(R-near neighbor)数

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in