当前位置:网站首页>332 · 恢复数组
332 · 恢复数组
2022-04-22 06:16:00 【yinhua405】
小九有一个长为 nnn 的整型数组,数组中的每个数都在 lll 和 rrr 之间,而且数组的和是 333 的整数倍。
请帮小九计算出这个数组一共有多少种不同的可能。
输出要对 109+710^9+7109+7 取模
数组的长度为 nnn,满足 1≤n≤1051 \le n \le 10^51≤n≤105。
数组中的每个元素都满足,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。
样例中,可能的数组有 [1,2],[2,1],[3,3][1,2], [2, 1], [3, 3][1,2],[2,1],[3,3]。
样例
输入:
n = 2
l = 1
r = 3
输出:
3
//
//重要信息:数组和是3的倍数,那么我们可以求出余数为0,1,2的组合数
//
//假设
//c0代表余数为0的组合数,c1代表余数为1的组合数,c2代表余数为2的组合数
//f0代表l - r余数为0的数量,f1代表l - r余数为1的数量,f2代表l - r余数为2的数量
//
//朴素的递推方式为:
//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
//
//这里有个优化的小技巧,不用循环l - r的每个数字算f0, f1, f2,那样会超时,现在直接利用数量除以3算数量即可,详见代码
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://blog.csdn.net/yinhua405/article/details/121392172
边栏推荐
猜你喜欢

Redis进阶

idea 不显示Run Dashboard视图窗口的问题

内部类使用说明(静态、实例、局部)

C语言 | 快速排序

C语言 | 数组

带环链表详解

1. Compile the following three modules of student information management system: and detect the implementation. 1. Add student information 4. Query student information 5. Query all student information

双向循环链表(详)

Find a notepad file by yourself, find the data material by yourself, and count the times of three keywords or sentence entries in the whole text.
![. net learning notes - about Net core (1) [. NETCORE's project structure, five ways to transfer values to pages, and the use of log4net and NLog]](/img/c8/e7579543d7911a907ae19f357478d8.png)
. net learning notes - about Net core (1) [. NETCORE's project structure, five ways to transfer values to pages, and the use of log4net and NLog]
随机推荐
在类加载的过程中,类变量的分配区域和实例变量的分配区域不一样
Process of stepping on the pit in the flutter environment
【数论】素数(三):素数判断法(朴素法、模6法、Rabin-Miller及改进)
[DRC RTSTAT-1] Unrouted nets: 1 net(s) are unrouted
MySQL learning notes
树+二叉树 详解 详解 +Top-k问题
Jenkins deployment PM2
14 lines of code to complete arbitrary selection of image crawling
ERROR: [Hsi 55-1545] ,无法正常生成fsbl,Unable to read in MSS file,Failed to closesw system.mss
[number theory] prime number (V): Mason prime number (lucas_lehmer decision method)
. net learning notes - about Net core (1) [. NETCORE's project structure, five ways to transfer values to pages, and the use of log4net and NLog]
Interviewers often ask about the general process and special circumstances of object allocation
LaTex用模板的时候图片的caption标题无法左对齐
(1) Download and installation of SQL Server
C语言 | 快速排序
小游戏——三子棋
Quartus II prevents signals from being integrated
Design a circle class with private member radius representing radius and function get_ Radius () is used to obtain the radius and area () is used to calculate the area of the circle; (2) Define a tabl
437. 路径总和 III
链表习题详解