Codeforces Round #533 (Div. 2)C. Ayoub and Lost Array

C. Ayoub and Lost Array

Ayoub had an array ?? of integers of size ?? and this array had two interesting properties:

All the integers in the array were between ?? and ?? (inclusive).
The sum of all the elements was divisible by 3.
Unfortunately, Ayoub has lost his array, but he remembers the size of the array ?? and the numbers ?? and ??, 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+7 (i.e. the remainder when dividing by 109+7). In case there are no satisfying arrays (Ayoub has a wrong memory), print 0.

Input
The first and only line contains three integers ??, ?? and ?? (1≤??≤2⋅105,1≤??≤??≤109) — the size of the lost array and the range of numbers in the array.

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

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

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

dp第一维枚举已经取的位数,第二维表示模3的余数:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define lowbit(x) (x&(-x))
#define eps 0.00000001
#define PI acos(-1)
#define pn printf("\n");
using namespace std;

int mod = 1e9+7;
const int maxn = 2e5+5;
ll dp[maxn][3];
int n;

void solve(ll c0, ll c1, ll c2)
{
    dp[1][0] = c0;
    dp[1][1] = c1;
    dp[1][2] = c2;
    for(int i = 2; i <= n ;i++)
    {
        dp[i][0] = (dp[i-1][1] * c2 % mod + dp[i-1][2] * c1 % mod + dp[i-1][0] * c0 % mod) % mod;
        dp[i][1] = (dp[i-1][1] * c0 % mod + dp[i-1][2] * c2 % mod + dp[i-1][0] * c1 % mod) % mod;
        dp[i][2] = (dp[i-1][1] * c1 % mod + dp[i-1][2] * c0 % mod + dp[i-1][0] * c2 % mod) % mod;
    }

}

int main()
{
    ll l, r;
    scanf("%d%lld%lld", &n, &l, &r);
    ll uni = (r - l + 1) / 3;
    ll res = (r - l + 1) % 3;
    ll c0 = 0, c1 = 0, c2 = 0;
    if(l % 3 == 0)
    {
        c0 = uni + (res >= 1);
        c1 = uni + (res >= 2);
        c2 = uni;
    }
    else if(l % 3 == 1)
    {
        c0 = uni;
        c1 = uni + (res >= 1);
        c2 = uni + (res >= 2);
    }
    else
    {
        c0 = uni + (res >= 2);
        c1 = uni;
        c2 = uni + (res >= 1);
    }
    solve(c0, c1, c2);
    printf("%lld\n", dp[n][0]);
}

原文地址:https://www.cnblogs.com/HazelNut/p/10298095.html

时间: 2024-10-31 00:50:38

Codeforces Round #533 (Div. 2)C. Ayoub and Lost Array的相关文章

Codeforces Round #533 (Div. 2)

A. Salem and Sticks 由于长度很小,所以直接暴力枚举最后的长度即可,取最小值即可. #include<bits/stdc++.h> #define CLR(a,b) memset(a,b,sizeof(a)); using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; const int maxn=10010; int a[1100],n; int cost,ans; int main(){ ci

Codeforces Round #533 (Div. 2) Solution

A. Salem and Sticks 签. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define N 1010 5 int n, a[N]; 6 7 int work(int x) 8 { 9 int res = 0; 10 for (int i = 1; i <= n; ++i) 11 res += max(0, abs(x - a[i]) - 1); 12 return res; 13 } 14 15 int m

Codeforces Round #533 (Div. 2) ABCD 题解

题目链接 A. Salem and Sticks 分析 暴力就行,题目给的n<=1000,ai<=100,暴力枚举t,t从2枚举到98,复杂度是1e5,完全可行. 代码 1 #include <cstdio> 2 #include <cmath> 3 #include <iostream> 4 #include <cstring> 5 #include <algorithm> 6 #include <vector> 7 #

Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)

题目链接:https://codeforces.com/contest/1105/problem/D 题意:p 个人在 n * m 的地图上扩展自己的城堡范围,每次最多走 a_i 步(曼哈顿距离),按 1 ~ p 的顺序,问最后每个人占领的点的数量. 题解:用一个队列维护当前起点,用另一个队列模拟当前起点走 a_i 步可以到达的全部点.(60 ~ 63行关键代码,多源bfs,不可分开跑) 数据: 4 3 2 2 1 1.. 1.. ..2 ... output:10 2 1 #include <

Codeforces Round #533 (Div. 2) B. Zuhair and Strings 【模拟】

传送门:http://codeforces.com/contest/1105/problem/B B. Zuhair and Strings time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Given a string ss of length nn and integer kk (1≤k≤n1≤k≤n). The string

Codeforces Round #533 (Div. 2)E. Helping Hiasat_求最大独立子集转换求最大团

题目链接:E. Helping Hiasat 题解:把夹在同一段1里面的人互相连边,然后问题就转换为求最大独立子集的大小,最大独立子集大小等于补图的最大团.最大图不会的可以看这篇博客,我也是看这个的 #include<bits/stdc++.h> #include<iostream> #include<string> #include<cstring> #include<algorithm> #define pb push_back #defin

[每日一题]:Codeforces Round #632 (Div. 2) C. Eugene and an array

题目: 样例: 题目大意: 给一个数组序列,问子串的和不为 0 的数量.(子串是连续的哦) 考察点: 前缀和.尺取.set的用法.思维 图解: Code: #include <set> #include <cstdio> #include <string> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef l

Codeforces Round #258 (Div. 2) 小结

A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行列中最小的一个为奇数,那么Akshat赢,否则Malvika赢. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace

Codeforces Round #489 (Div. 2)

Codeforces Round #489 (Div. 2) A. Nastya and an Array #include <bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;++i) #define frep(i,a,b) for(int i=a;i>=b;--i) #define mem(W) memset(W,0,sizeof(W)) #define pb push_back typedef long long ll; c