답안 #715453

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
715453 2023-03-26T19:37:03 Z Ahmed57 Ljetopica (COI19_ljetopica) C++14
100 / 100
95 ms 63232 KB
#include <bits/stdc++.h>

using namespace std;
pair<long long,long long> dp[1001][1001][2][2];
string l,r;
string mo;
int org = 0;
pair<long long,long long> neg = {-1,-1};
long long mod = int(1e9)+7;
long long pw[1001];
//1000
//1001
//1010
//1101
//1110
//1111
//|R|L|R
//||R|L|R
//|R|R|L 1110
//||LR|L 1010
//||L|LL 1000
//|RL|R 1101
//|R|RR 1111
//L|L|R 1001
pair<long long,long long> solve(int i,int rem,int lowe,int uppe){
    if(i==mo.size()){
        return {(rem==0?1:0),0};
    }
    if(dp[i][rem][lowe][uppe]!=neg)return dp[i][rem][lowe][uppe];
    int diff = (org-rem)%2;
    char st = '0';
    if(mo[i]=='R')st = '1';
    pair<long long,long long>all = {0,0};
    long long nu = 0;bool ss = 0;
    for(char j = st;j<=st;j++){
        char c = j;
        if(diff){
            if(c=='0')c = '1';
            else c = '0';
        }
        if(c=='1')ss = 1;
        if(c<r[i+1]){
            if(c>l[i+1]){
                all.first+=solve(i+1,rem,1,1).first;
                all.second+=solve(i+1,rem,1,1).second;
                all.first%=mod;
                all.second%=mod;
                if(ss)nu+=solve(i+1,rem,1,1).first;
                nu%=mod;
            }else if(c==l[i+1]||lowe){
                all.first+=solve(i+1,rem,lowe,1).first;
                all.second+=solve(i+1,rem,lowe,1).second;
                all.first%=mod;
                all.second%=mod;
                if(ss)nu+=solve(i+1,rem,lowe,1).first;
                nu%=mod;
            }
        }else if(c==r[i+1]||uppe){
            if(c>l[i+1]){
                all.first+=solve(i+1,rem,1,uppe).first;
                all.second+=solve(i+1,rem,1,uppe).second;
                all.first%=mod;
                all.second%=mod;
                if(ss)nu+=solve(i+1,rem,1,uppe).first;
                nu%=mod;
            }else if(c==l[i+1]||lowe){
                all.first+=solve(i+1,rem,lowe,uppe).first;
                all.second+=solve(i+1,rem,lowe,uppe).second;
                all.first%=mod;
                all.second%=mod;
                if(ss)nu+=solve(i+1,rem,lowe,uppe).first;
                nu%=mod;
            }
        }
    }
    int orginalRem = rem;
    if(rem>0){
        rem--;
        ss =0;
        diff = !diff;
        for(char j = st;j<=st;j++){
        char c = j;
        if(diff){
            if(c=='0')c = '1';
            else c = '0';
        }
        if(c=='1')ss=1;
        if(c<r[i+1]){
            if(c>l[i+1]){
                all.first+=solve(i+1,rem,1,1).first;
                all.second+=solve(i+1,rem,1,1).second;
                all.first%=mod;
                all.second%=mod;
                if(ss)nu+=solve(i+1,rem,1,1).first;
                nu%=mod;
            }else if(c==l[i+1]||lowe){
                all.first+=solve(i+1,rem,lowe,1).first;
                all.second+=solve(i+1,rem,lowe,1).second;
                all.first%=mod;
                all.second%=mod;
                if(ss)nu+=solve(i+1,rem,lowe,1).first;
                nu%=mod;
            }
        }else if(c==r[i+1]||uppe){
            if(c>l[i+1]){
                all.first+=solve(i+1,rem,1,uppe).first;
                all.second+=solve(i+1,rem,1,uppe).second;
                all.first%=mod;
                all.second%=mod;
                if(ss)nu+=solve(i+1,rem,1,uppe).first;
                nu%=mod;
            }else if(c==l[i+1]||lowe){
                all.first+=solve(i+1,rem,lowe,uppe).first;
                all.second+=solve(i+1,rem,lowe,uppe).second;
                all.first%=mod;
                all.second%=mod;
                if(ss)nu+=solve(i+1,rem,lowe,uppe).first;
                nu%=mod;
            }
        }
    }
    }
    all.second+=(nu*pw[(mo.size()-1)-i])%mod;
    all.second%=mod;
    return dp[i][orginalRem][lowe][uppe] =all;
}
signed main(){
    pw[0] = 1;
    for(int i = 1;i<=1000;i++){
        pw[i] = (pw[i-1]*2)%mod;
    }
    long long n,k;
    cin>>n>>k;
    org =k;
    cin>>mo>>l>>r;
    for(int i = 0;i<=n;i++){
        for(int j = 0;j<=k;j++){
            dp[i][j][0][0] = neg;
            dp[i][j][1][0] = neg;
            dp[i][j][1][1] = neg;
            dp[i][j][0][1] = neg;
        }
    }
    pair<long long,long long> sum = solve(0,k,0,0);
    //
    for(int i = 0;i<=n;i++){
        for(int j = 0;j<=k;j++){
            dp[i][j][0][0] = neg;
            dp[i][j][1][0] = neg;
            dp[i][j][1][1] = neg;
            dp[i][j][0][1] = neg;
        }
    }
    for(int i =0 ;i<mo.size();i++){
        if(mo[i]=='R')mo[i] ='L';
        else mo[i] = 'R';
    }
    sum.first+=solve(0,k,0,0).first;
    sum.second+=solve(0,k,0,0).second;
    cout<<((pw[n-1]*sum.first)%mod+sum.second)%mod<<endl;
}

Compilation message

ljetopica.cpp: In function 'std::pair<long long int, long long int> solve(int, int, int, int)':
ljetopica.cpp:26:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     if(i==mo.size()){
      |        ~^~~~~~~~~~~
ljetopica.cpp: In function 'int main()':
ljetopica.cpp:154:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  154 |     for(int i =0 ;i<mo.size();i++){
      |                   ~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4564 KB Output is correct
2 Correct 3 ms 4308 KB Output is correct
3 Correct 2 ms 4152 KB Output is correct
4 Correct 2 ms 3924 KB Output is correct
5 Correct 2 ms 3668 KB Output is correct
6 Correct 2 ms 3540 KB Output is correct
7 Correct 2 ms 3256 KB Output is correct
8 Correct 2 ms 3028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 308 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 40452 KB Output is correct
2 Correct 54 ms 29284 KB Output is correct
3 Correct 61 ms 32852 KB Output is correct
4 Correct 92 ms 63232 KB Output is correct
5 Correct 52 ms 27836 KB Output is correct
6 Correct 95 ms 63052 KB Output is correct
7 Correct 35 ms 18752 KB Output is correct
8 Correct 61 ms 32340 KB Output is correct
9 Correct 5 ms 5716 KB Output is correct
10 Correct 54 ms 29780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4564 KB Output is correct
2 Correct 3 ms 4308 KB Output is correct
3 Correct 2 ms 4152 KB Output is correct
4 Correct 2 ms 3924 KB Output is correct
5 Correct 2 ms 3668 KB Output is correct
6 Correct 2 ms 3540 KB Output is correct
7 Correct 2 ms 3256 KB Output is correct
8 Correct 2 ms 3028 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 320 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 308 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 71 ms 40452 KB Output is correct
25 Correct 54 ms 29284 KB Output is correct
26 Correct 61 ms 32852 KB Output is correct
27 Correct 92 ms 63232 KB Output is correct
28 Correct 52 ms 27836 KB Output is correct
29 Correct 95 ms 63052 KB Output is correct
30 Correct 35 ms 18752 KB Output is correct
31 Correct 61 ms 32340 KB Output is correct
32 Correct 5 ms 5716 KB Output is correct
33 Correct 54 ms 29780 KB Output is correct
34 Correct 90 ms 48812 KB Output is correct
35 Correct 41 ms 19500 KB Output is correct
36 Correct 56 ms 29268 KB Output is correct
37 Correct 31 ms 59968 KB Output is correct
38 Correct 18 ms 11220 KB Output is correct
39 Correct 77 ms 53436 KB Output is correct
40 Correct 16 ms 10172 KB Output is correct
41 Correct 72 ms 36636 KB Output is correct
42 Correct 83 ms 45948 KB Output is correct
43 Correct 76 ms 45908 KB Output is correct
44 Correct 77 ms 53204 KB Output is correct
45 Correct 38 ms 18020 KB Output is correct
46 Correct 73 ms 42372 KB Output is correct
47 Correct 76 ms 44620 KB Output is correct
48 Correct 52 ms 27316 KB Output is correct
49 Correct 5 ms 5588 KB Output is correct
50 Correct 87 ms 51180 KB Output is correct
51 Correct 49 ms 25248 KB Output is correct
52 Correct 60 ms 26836 KB Output is correct
53 Correct 90 ms 61696 KB Output is correct
54 Correct 36 ms 19540 KB Output is correct
55 Correct 83 ms 49876 KB Output is correct
56 Correct 90 ms 56344 KB Output is correct
57 Correct 15 ms 9660 KB Output is correct
58 Correct 84 ms 44628 KB Output is correct