結果

問題 No.1417 100の倍数かつ正整数(2)
ユーザー renjyaku_intrenjyaku_int
提出日時 2021-04-12 22:39:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 3,000 ms
コード長 5,479 bytes
コンパイル時間 2,796 ms
コンパイル使用メモリ 216,296 KB
実行使用メモリ 9,364 KB
最終ジャッジ日時 2023-09-11 03:34:27
合計ジャッジ時間 5,213 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 3 ms
4,376 KB
testcase_31 AC 4 ms
4,380 KB
testcase_32 AC 6 ms
4,644 KB
testcase_33 AC 14 ms
6,152 KB
testcase_34 AC 25 ms
9,336 KB
testcase_35 AC 26 ms
9,332 KB
testcase_36 AC 25 ms
9,336 KB
testcase_37 AC 25 ms
9,340 KB
testcase_38 AC 25 ms
9,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#include <tourist>
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;

typedef pair<ll, ll> p;
const int INF = 1e9;
const double eps = 1e-7;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const long double pi = 4 * atan(1);
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v)          \
    cout << #v << ":";    \
    for (auto x : v)      \
    {                     \
        cout << x << ' '; \
    }                     \
    cout << endl;
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v)
{
    for (T &x : v)
        is >> x;
    return is;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//g++ yarudake.cpp -std=c++17 -I .
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

// 繰り返し二乗法
ll pow_mod(ll x, ll n)
{
    if (n == 0)
        return 1;
    ll ret = pow_mod((x * x) % MOD, n / 2);
    if (n & 1)
        ret = (ret * x) % MOD;
    return ret;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    int n = s.size();
    vector<vector<vector<vector<ll>>>> dp(n + 1, vector<vector<vector<ll>>>(3, vector<vector<ll>>(3, vector<ll>(2, 0))));
    dp[0][0][0][0] = 1;
    rep(i, n)
    {
        rep(j, 3)
        {
            rep(k, 3)
            {
                for (char c = '1'; c <= '9'; c++)
                {
                    if (c == s[i])
                    {
                        if (c == '2' || c == '6')
                        {
                            dp[i + 1][min(j + 1, 2)][k][0] += dp[i][j][k][0];
                            dp[i + 1][min(j + 1, 2)][k][1] += dp[i][j][k][1];
                        }
                        else if (c == '5')
                        {
                            dp[i + 1][j][min(k + 1, 2)][0] += dp[i][j][k][0];
                            dp[i + 1][j][min(k + 1, 2)][1] += dp[i][j][k][1];
                        }
                        else if (c == '4' || c == '8')
                        {
                            dp[i + 1][min(j + 2, 2)][k][0] += dp[i][j][k][0];
                            dp[i + 1][min(j + 2, 2)][k][1] += dp[i][j][k][1];
                        }
                        else
                        {
                            dp[i + 1][j][k][0] += dp[i][j][k][0];
                            dp[i + 1][j][k][1] += dp[i][j][k][1];
                        }
                    }
                    else if (c > s[i])
                    {
                        if (c == '2' || c == '6')
                        {
                            dp[i + 1][min(j + 1, 2)][k][1] += dp[i][j][k][1];
                        }
                        else if (c == '5')
                        {
                            dp[i + 1][j][min(k + 1, 2)][1] += dp[i][j][k][1];
                        }
                        else if (c == '4' || c == '8')
                        {
                            dp[i + 1][min(j + 2, 2)][k][1] += dp[i][j][k][1];
                        }
                        else
                        {
                            dp[i + 1][j][k][1] += dp[i][j][k][1];
                        }
                    }
                    else
                    {
                        if (c == '2'||c=='6')
                        {
                            dp[i + 1][min(j + 1, 2)][k][1] += dp[i][j][k][1] + dp[i][j][k][0];
                        }
                        else if (c == '5')
                        {
                            dp[i + 1][j][min(k + 1, 2)][1] += dp[i][j][k][1] + dp[i][j][k][0];
                        }
                        else if (c == '4' || c == '8')
                        {
                            dp[i + 1][min(j + 2, 2)][k][1] += dp[i][j][k][1] + dp[i][j][k][0];
                        }
                        else
                        {
                            dp[i + 1][j][k][1] += dp[i][j][k][1] + dp[i][j][k][0];
                        }
                    }
                    dp[i + 1][min(j + 1, 2)][k][0] %= MOD;
                    dp[i + 1][min(j + 1, 2)][k][1] %= MOD;
                    dp[i + 1][min(j + 2, 2)][k][0] %=MOD;
                    dp[i + 1][min(j + 2, 2)][k][1] %= MOD;
                    dp[i + 1][j][min(k + 1, 2)][0] %= MOD;
                    dp[i + 1][j][min(k + 1, 2)][1] %= MOD;
                    dp[i + 1][j][k][0] %= MOD;
                    dp[i + 1][j][k][1] %= MOD;
                }
            }
        }
        if(i!=0){
            dp[i+1][2][0][1]+=2;
            dp[i+1][1][0][1]+=2;
            dp[i + 1][0][1][1]++;
            dp[i + 1][0][0][1]+= 4;
        }
    }
    
    cout << (dp[n][2][2][0] + dp[n][2][2][1]) % MOD << "\n";
}
0