結果

問題 No.260 世界のなんとか3
ユーザー nanophoto12nanophoto12
提出日時 2016-01-02 19:28:03
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,008 bytes
コンパイル時間 662 ms
コンパイル使用メモリ 76,816 KB
実行使用メモリ 11,212 KB
最終ジャッジ日時 2023-10-19 13:33:47
合計ジャッジ時間 3,889 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
11,124 KB
testcase_01 AC 4 ms
11,124 KB
testcase_02 AC 5 ms
11,124 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 4 ms
11,124 KB
testcase_28 WA -
testcase_29 AC 180 ms
11,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstring>

using namespace std;

#define FOR(x,y) for(int x = 0;x < (y);x++)
#define LLI long long int
#define FORR(x,arr) for(auto& x:arr)
#define ALL(a) (a.begin()),(a.end())

#define _L(x) cout<<(x)<<endl
//#define _L(x) ;

template<int um> class UF {
public:
    vector<int> _parent,_rank;
    UF()
    {
        _parent=_rank=vector<int>(um,0);
        for(int i=0;i<um;i++)
        {
            _parent[i]=i;
        }
    }
    
    int Find(int x)
    {
        if(_parent[x] == x)
        {
            return x;
        }
        _parent[x] = Find(_parent[x]);
        return _parent[x];
    }
    
    int Union(int x,int y)
    {
        int xRoot = Find(x);
        int yRoot = Find(y);
        if(_rank[xRoot]>_rank[yRoot])
        {
            _parent[xRoot] = yRoot;
           return yRoot;
        }
        if(_rank[xRoot]<_rank[yRoot])
        {
            _parent[yRoot] = xRoot;
            return xRoot;
        }
        if(xRoot != yRoot)
        {
            _parent[yRoot] = xRoot;
            _rank[xRoot]++;
            return xRoot;
        }
        return xRoot;
    }
};


LLI dp[10001][2][2][3][8];
static const LLI mod = 10e9 + 7;

LLI solve(string upper)
{
    int upperDigits = (int)upper.length();
    memset(&dp[0][0][0][0][0], 0, sizeof(dp));
    dp[0][0][0][0][0] = 1;
    FOR(i, upperDigits) FOR(j, 2) FOR(k, 2) FOR(m, 3) FOR(s, 8)
    {
        if(j)
        {
            FOR(d, 10)
            {
                dp[i+1][1][k || d == 3][(m + d) % 3][(10 * s + d) % 8] += dp[i][1][k][m][s];
                dp[i+1][1][k || d == 3][(m + d) % 3][(10 * s + d) % 8] %= mod;
            }
        }
        else
        {
            int u = upper[i] - '0';
            FOR(d, u + 1)
            {
                dp[i+1][d < u][k || d == 3][(m + d) % 3][(10 * s + d) % 8] += dp[i][0][k][m][s];
                dp[i+1][d < u][k || d == 3][(m + d) % 3][(10 * s + d) % 8] %= mod;
            }
        }
    }
    LLI sum = 0;
    FOR(j, 2) FOR(k, 2) FOR(m, 3) FOR(s, 8)
    {
        if(k == 1 || m == 0)
        {
            if(s != 0)
            {
                sum += dp[upperDigits][j][k][m][s];
                sum %= mod;
            }
        }
    }
    return sum;
}

static string Decrement(string source)
{
    string result = source;
    for(int i = (int)source.length() - 1;i >= 0;i--)
    {
        if(source[i] == '0')
        {
            result[i] = '9';
        }
        else
        {
            result[i] = source[i] - 1;
            return result;
        }
    }
    throw std::exception();
}

int main() {
    string lower, upper;
    cin >> lower >> upper;
    
    LLI lowerValue = solve(Decrement(lower));
    LLI upperValue = solve(upper);
    LLI r = upperValue - lowerValue;
    if(r < 0)
    {
        r += mod;
    }
    cout << r << endl;
    return 0;
    
}
0