Submission #258215

#TimeUsernameProblemLanguageResultExecution timeMemory
258215cheehengLucky Numbers (RMI19_lucky)C++14
28 / 100
443 ms1444 KiB
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9+7; char S[100005]; int X[100005]; long long memo[10005][2]; long long dp2[100005][2]; int N, Q; int K; long long dp(int i, bool prev_digit_is_1, bool same_so_far){ if(i == K){ return 1; }else if(!same_so_far){ if(prev_digit_is_1){ return dp2[K-i][0]; }else{ return (dp2[K-i][1] + dp2[K-i][0])%MOD; } }else if(memo[i][prev_digit_is_1] != -1){ return memo[i][prev_digit_is_1]; }else{ long long ans = 0; int j = X[i]; if(j == 0){ ans = dp(i+1, 0, 1); }else if(j == 1){ ans = (dp(i+1, 0, 0) + dp(i+1, 1, 1))%MOD; }else if(j == 2){ ans = (dp(i+1, 0, 0) + dp(i+1, 1, 0) + dp(i+1, 0, 1))%MOD; }else{ ans = ((j-1-prev_digit_is_1)*dp(i+1, 0, 0) + dp(i+1, 1, 0) + dp(i+1, 0, 1))%MOD; } /* for(int j = 0; j < 10; j ++){ if(j > X[i]){break;} if(prev_digit_is_1 && j == 3){ continue; } ans += dp(i+1, j == 1, X[i] == j); } ans %= MOD; */ //printf("memo[%d][%d][%d]=%lld\n", i, prev_digit_is_1, same_so_far, ans); return memo[i][prev_digit_is_1] = ans; } } int main(){ scanf("%d%d", &N, &Q); scanf(" %s", S); for(int i = 0; i < N; i ++){ X[i] = S[i]-'0'; } dp2[0][0] = 1; dp2[0][1] = 0; for(int i = 1; i <= N; i ++){ dp2[i][0] = (dp2[i-1][1]*8 + dp2[i-1][0]*9)%MOD; dp2[i][1] = (dp2[i-1][1] + dp2[i-1][0])%MOD; //printf("dp2[%d][%d]=%lld\n", i, 0, dp2[i][0]); //printf("dp2[%d][%d]=%lld\n", i, 1, dp2[i][1]); } K = N; memset(memo, -1, sizeof(memo)); printf("%lld\n", dp(0, false, true)); for(int i = 0; i < Q; i ++){ int t, l, r; scanf("%d%d%d", &t, &l, &r); if(t == 2){ S[l-1] = r+'0'; }else{ l --; r --; for(int j = 0; j < r-l+1; j ++){ X[j] = S[l+j]-'0'; } K = r-l+1; memset(memo, -1, sizeof(memo)); printf("%lld\n", dp(0, false, true)); } } return 0; }

Compilation message (stderr)

lucky.cpp: In function 'int main()':
lucky.cpp:56:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &N, &Q);
     ~~~~~^~~~~~~~~~~~~~~~
lucky.cpp:57:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf(" %s", S);
     ~~~~~^~~~~~~~~~
lucky.cpp:78:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d", &t, &l, &r);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...