当前位置:网站首页>332 · recovery array
332 · recovery array
2022-04-22 07:28:00 【yinhua405】
Xiao Jiu has a long nnn Integer array , Every number in the array is in lll and rrr Between , And the sum of the array is 333 Integer multiple .
Please help Xiao Jiu calculate how many different possibilities there are in this array .
Output to 109+710^9+7109+7 modulus
The length of the array is nnn, Satisfy 1≤n≤1051 \le n \le 10^51≤n≤105.
Every element in the array satisfies ,l≤ai≤r,1≤l≤r≤109l \le a_i \le r, 1 \le l \le r \le 10^9l≤ai≤r,1≤l≤r≤109.
In the example , Possible arrays are [1,2],[2,1],[3,3][1,2], [2, 1], [3, 3][1,2],[2,1],[3,3].
Examples
Input :
n = 2
l = 1
r = 3
Output :
3
//
// Important information : Arrays and are 3 Multiple , Then we can find the remainder as 0,1,2 The combination number of
//
// hypothesis
//c0 The remainder is 0 The combination number of ,c1 The remainder is 1 The combination number of ,c2 The remainder is 2 The combination number of
//f0 representative l - r Remainder is 0 The number of ,f1 representative l - r Remainder is 1 The number of ,f2 representative l - r Remainder is 2 The number of
//
// The simple recursive method is :
//1:dp[i][c0] = dp[i - 1][c0] * f0 + dp[i - 1][c2] * f1 + dp[i - 1][c1] * f2
//2: dp[i][c1] = dp[i - 1][c1] * f0 + dp[i - 1][c0] * f1 + dp[i - 1][c2] * f2
//3 : dp[i][c2] = dp[i - 1][c2] * f0 + dp[i - 1][c1] * f1 + dp[i - 1][c0] * f2
//
// Here's an optimization tip , Don't cycle l - r Every number of counts f0, f1, f2, That will time out , Now divide the quantity by 3 Just calculate the quantity , See the code for details.
int restoreArray(int n, int l, int r) {
// write your code here.
int mod = 1000000000 + 7;
uint64_t f[3] = {0,0,0};
int v = l;
for (; v <= r && v % 3 != 1; v++)
{
f[v % 3]++;
}
int t = (r - v + 1) / 3;
int e = (r - v + 1) % 3;
f[0] += t;
f[1] += t;
f[2] += t;
if (e >= 1) {
f[1]++;
}
if (e > 1) {
f[2]++;
}
uint64_t c[3] ;
c[0] = f[0];
c[1] = f[1];
c[2] = f[2];
uint64_t t0;
uint64_t t1;
uint64_t t2;
for (int i = 1; i < n; i++)
{
t0 = c[0];
t1 = c[1];
t2 = c[2];
c[0] = (f[0] * t0 + f[1] * t2 + f[2] * t1) % mod;
c[1] = (f[0] * t1 + f[1] * t0 + f[2] * t2) % mod;
c[2] = (f[0] * t2 + f[1] * t1 + f[2] * t0) % mod;
}
return (int)c[0];
}
版权声明
本文为[yinhua405]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220613324979.html
边栏推荐
- 内部类使用说明(静态、实例、局部)
- The problem of interval summation -- difference
- What is the internal structure of stack frame?
- [number theory] prime number (I): basic concepts, properties, conjectures and theorems
- [number theory] congruence (V): multivariate linear congruence equation
- Codeforces Round #623
- 队列(详解)——手撕队列习题
- C language | quick sorting
- (2) Basic configuration of SQL server and connection to SQL server using Navicat
- 119 · 编辑距离
猜你喜欢
![P1095 [NOIP2007 普及组] 守望者的逃离](/img/5e/0437bdee83b6b66626e535e382b84b.png)
P1095 [NOIP2007 普及组] 守望者的逃离

Beyond compare solution to "authorization key has been revoked"

Vivado generates and invokes EDF netlist files

Virtual machine disk space shrinks

Redis advanced

D. Determine the Photo Position (简单找子串)(2021牛客暑期多校训练营1)

This关键字详细概述

The thought and code of eight sorting

C language | quick sorting

A.Binary Seating (概率) (2021年度训练联盟热身训练赛第五场)
随机推荐
Codeforces Round #623
重写与重载的定义与区别
Why is the data stored in the leaf node of the non primary key index the primary key value
Cannot find interface mapping after updating HDF
微软实习生面试的2道算法题目——20220119
Redis進階
instanceof的使用说明及实例讲解
[number theory] prime number (2): prime number sieve method (Egyptian sieve, Euler sieve, interval sieve)
Educational Codeforces Round 122 (Rated for Div. 2)
要是我再犯这种憨憨错误就.../(ㄒoㄒ)/~~
Error: [HSI 55-1545], unable to generate fsbl normally, unable to read in MSS file, failed to close system mss
When latex uses the template, the caption title of the picture cannot be left aligned
LaTex中添加作者照片和作者简介
[number theory] [indefinite equation] n-ary primary indefinite equation, Pell equation, Pythagoras theorem, Fermat theorem
小游戏——三子棋
CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes)
LeetCode - 7 - (二叉树的最近公共祖先、轮转数组、二叉树的直接、下一个排列、组合总和)
Introduction
2021学习计划
LeetCode - 8 - (三数之和、Z字形变换、两数之和<链表>、盛最多水的容器、电话号码的字母组合)