【递推】Ayoub and Lost Array

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

All the integers in the array were between ll and rr (inclusive).
The sum of all the elements was divisible by 33.
Unfortunately, Ayoub has lost his array, but he remembers the size of the array nn and the numbers ll and rr, so he asked you to find the number of ways to restore the array.

Since the answer could be very large, print it modulo 109+7109+7 (i.e. the remainder when dividing by 109+7109+7). In case there are no satisfying arrays (Ayoub has a wrong memory), print 00.

Input
The first and only line contains three integers nn, ll and rr (1≤n≤2⋅105,1≤l≤r≤1091≤n≤2⋅105,1≤l≤r≤109) — the size of the lost array and the range of numbers in the array.

Output
Print the remainder when dividing by 109+7109+7 the number of ways to restore the array.

Examples
inputCopy
2 1 3
outputCopy
3
inputCopy
3 2 2
outputCopy
1
inputCopy
9 9 99
outputCopy
711426616
Note
In the first example, the possible arrays are : [1,2],[2,1],[3,3][1,2],[2,1],[3,3].

In the second example, the only possible array is [2,2,2][2,2,2].

解决方法:首先可以先将[l,r]范围内的整数根据其模3的到的余数分为三类,分别将其数量存在num[0],num[1],num[2]中,而dp[i] [j]代表在放置第i个数时使当前数组总和%3的值为j的情况数,由此可以推出
dp[i][j]=dp[i−1][0]∗num[j]+dp[i−1][1]∗num[(j−1+3)%3]+dp[i−1][2]∗num[(j−2+3)%3]dp[i][j]=dp[i-1][0]*num[j]+dp[i-1][1]*num[(j-1+3)\%3]+dp[i-1][2]*num[(j-2+3)\%3] dp[i][j]=dp[i1][0]num[j]+dp[i1][1]num[(j1+3)%3]+dp[i1][2]num[(j2+3)%3]

#include <iostream>
#include <cstdio>
using namespace std; 
typedef long long ll;
const int mod=1e9+7;
ll dp[200050][3];
ll num[3];
int main() 
{ios::sync_with_stdio(0),cin.tie(0);int n,l,r;cin>>n>>l>>r;num[0]=r/3-(l-1)/3;num[1]=num[0]-1;if(l%3==1) num[1]++;if(r%3==1||r%3==2) num[1]++;num[2]=num[0]-1;if(l%3==1||l%3==2) num[2]++;if(r%3==2) num[2]++;//cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;dp[1][0]=num[0];dp[1][1]=num[1];dp[1][2]=num[2];for(int i=2;i<=n;i++) {dp[i][0]=((dp[i-1][0]*num[0])%mod+(dp[i-1][1]*num[2])%mod+(dp[i-1][2]*num[1])%mod)%mod;dp[i][1]=((dp[i-1][0]*num[1])%mod+(dp[i-1][1]*num[0])%mod+(dp[i-1][2]*num[2])%mod)%mod;dp[i][2]=((dp[i-1][0]*num[2])%mod+(dp[i-1][1]*num[1])%mod+(dp[i-1][2]*num[0])%mod)%mod;}cout<<dp[n][0]<<endl;return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/536311.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Super-palindrome【字符串+思维】

Super-palindrome 时间限制: 1 Sec 内存限制: 128 MB 提交: 595 解决: 231 [提交] [状态] [命题人:admin] 题目描述 You are given a string that is consisted of lowercase English alphabet. You are supposed to change it into a super-palindrome string in minimum ste…

Hakase and Nano【博弈】

Hakase and Nano 时间限制: 1 Sec 内存限制: 128 MB 提交: 533 解决: 155 [提交] [状态] [命题人:admin] 题目描述 Hakase and Nano are playing an ancient pebble game (pebble is a kind of rock). There are n packs of pebbles, and the i-th pack contains ai pebble…

【思维】过分的谜题

题目描述 2060年是云南中医学院的百年校庆&#xff0c;于是学生会的同学们搞了一个连续猜谜活动&#xff1a;共有10个谜题&#xff0c;现在告诉所有人第一个谜题&#xff0c;每个谜题的答案就是下一个谜题的线索....成功破解最后一个谜题后&#xff0c;答案就是指向奖励的线索…

【bfs】调酒壶里的酸奶

题目描述 最近小w学了一手调酒的技巧&#xff0c;这么帅的操作&#xff0c;说不定能靠这个俘获女神的芳心&#xff0c;为了在女神面前露一手&#xff0c;他想在学校里建一个"pub"&#xff0c;但是显然学校不可能让他真的建一个"pub"&#xff0c;那么他退而…

【dfs】Election of Evil

题目描述 Dylan is a corrupt politician trying to steal an election. He has already used a mind-control technique to enslave some set U of government representatives. However, the representatives who will be choosing the winner of the election is a differe…

【思维】Iranian ChamPions Cup

题目描述 The Iranian ChamPions Cup (ICPC), the most prestigious football league in Iran, is reaching its end, and people are eagerly waiting for the finals, which happened to be between the two most popular Iranian teams, Persepolis and Esteghlal. The ICP…

【数学】Chaarshanbegaan at Cafebazaar

题目描述 Chaarshanbegaan is a gathering event at Cafebazaar similar to TGIF events at Google. Some entertainment programs like pantomime, foosball, Xbox/PS4, and several board games are part of the event. You are going to set up a dart game in Chaarshanbe…

【思维】Congestion Charging Zone

题目描述 Tehran municipality has set up a new charging method for the Congestion Charging Zone (CCZ) which controls the passage of vehicles in Tehran’s high-congestion areas in the congestion period (CP) from 6:30 to 19:00. There are plate detection came…

【二分】LED

题目描述 A Light-Emitting Diode (LED) is a semiconductor light source, which emits light when an electric current of voltage higher than a threshhold is applied to its leads. ACM R&D recently reported that they have succesfully developed a new LED, na…

【差分数组】Master of GCD

题目描述 Hakase has n numbers in a line. At fi rst, they are all equal to 1. Besides, Hakase is interested in primes. She will choose a continuous subsequence [l, r] and a prime parameter x each time and for every l≤i≤r, she will change ai into ai*x. To…

【模拟】Ground Defense

题目描述 You are a denizen of Linetopia, whose n major cities happen to be equally spaced along an east-west line. In fact, they are often numbered in order from 1 to n, where 1 is the westmost city and n is the eastmost city. Linetopia was a lovely plac…

【模拟】Bulbs

题目描述 Greg has an m n grid of Sweet Lightbulbs of Pure Coolness he would like to turn on. Initially, some of the bulbs are on and some are off. Greg can toggle some bulbs by shooting his laser at them. When he shoots his laser at a bulb, it toggles th…

【模拟】Ingenious Lottery Tickets

题目描述 Your friend Superstitious Stanley is always getting himself into trouble. This time, in his Super Lotto Pick and Choose plan, he wants to get rich quick by choosing the right numbers to win the lottery. In this lottery, entries consist of six dis…

【数学】Hunter’s Apprentice

题目描述 When you were five years old, you watched in horror as a spiked devil murdered your parents. You would have died too, except you were saved by Rose, a passing demon hunter. She ended up adopting you and training you as her apprentice. Rose’s cur…

【模拟】Thanks, TuSimple!

题目链接&#xff1a;http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId5979 Thanks, TuSimple! Time Limit: 1 Second Memory Limit: 65536 KB In the very first sentence of the very first problem, we would like to give our sincere thanks to TuSimple,…

【二维差分】Monitor

Monitor 题目&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid6514 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 163840/163840 K (Java/Others) Total Submission(s): 600 Accepted Submission(s): 190 Problem Description Xiaoteng has a la…

【数学】MORE XOR

Given a sequence of nnn numbers a1,a2,⋯&ThinSpace;,ana_1, a_2,\cdots, a_na1​,a2​,⋯,an​ and three functions. Define a function f(l,r)f(l,r)f(l,r) which returns ⊕a[x](l≤x≤r)\oplus a[x] (l \le x \le r)⊕a[x](l≤x≤r). The \oplus⊕ represents excl…

【数学】Element Swapping

Element Swapping Time Limit: 1 Second Memory Limit: 65536 KB DreamGrid has an integer sequence a1,a2,a3,…,ana_1,a_2,a_3,\dots,a_na1​,a2​,a3​,…,an​ and he likes it very much. Unfortunately, his naughty roommate BaoBao swapped two elements aia_iai​ an…

【二分+二维前缀和】Largest Allowed Area

Largest Allowed Area 时间限制: 1 Sec 内存限制: 128 MB 提交: 146 解决: 54 [提交] [状态] [命题人:admin] 题目描述 A company is looking for land to build its headquarters. It has a lot of money and can buy as many land patches as it needs. Its goal, howev…

【数学】Floating-Point Hazard

Floating-Point Hazard 时间限制: 1 Sec 内存限制: 128 MB 提交: 106 解决: 42 [提交] [状态] [命题人:admin] 题目描述 Given the value of low, high you will have to find the value of the following expression: If you try to find the value of the above express…