結果

問題 No.260 世界のなんとか3
ユーザー nanophoto12nanophoto12
提出日時 2016-01-02 22:36:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 184 ms / 2,000 ms
コード長 3,454 bytes
コンパイル時間 743 ms
コンパイル使用メモリ 77,584 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-04-25 07:50:48
合計ジャッジ時間 3,580 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
11,008 KB
testcase_01 AC 8 ms
10,880 KB
testcase_02 AC 8 ms
11,136 KB
testcase_03 AC 143 ms
11,008 KB
testcase_04 AC 143 ms
11,008 KB
testcase_05 AC 34 ms
11,008 KB
testcase_06 AC 25 ms
11,008 KB
testcase_07 AC 100 ms
11,008 KB
testcase_08 AC 70 ms
11,008 KB
testcase_09 AC 44 ms
11,008 KB
testcase_10 AC 113 ms
11,008 KB
testcase_11 AC 105 ms
11,008 KB
testcase_12 AC 66 ms
10,880 KB
testcase_13 AC 24 ms
11,008 KB
testcase_14 AC 96 ms
11,008 KB
testcase_15 AC 32 ms
11,008 KB
testcase_16 AC 86 ms
11,008 KB
testcase_17 AC 69 ms
11,008 KB
testcase_18 AC 66 ms
11,008 KB
testcase_19 AC 84 ms
11,008 KB
testcase_20 AC 61 ms
11,008 KB
testcase_21 AC 55 ms
10,752 KB
testcase_22 AC 95 ms
11,008 KB
testcase_23 AC 16 ms
11,008 KB
testcase_24 AC 76 ms
10,880 KB
testcase_25 AC 94 ms
11,008 KB
testcase_26 AC 57 ms
11,008 KB
testcase_27 AC 7 ms
10,880 KB
testcase_28 AC 146 ms
11,008 KB
testcase_29 AC 184 ms
11,008 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 = 1000000007;

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 Trim(string source)
{
    string text;
    bool first = true;
    FORR(r, source)
    {
        if(r == '0')
        {
            if(first)
            {
                continue;
            }
            else
            {
                text += r;
            }
        }
        else
        {
            first = false;
            text += r;
        }
    }
    return text;
}

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;
    cin >> upper;
    string decrement = Trim(Decrement(lower));
    LLI lowerValue = solve(decrement);
    LLI upperValue = solve(upper);
    LLI r = upperValue - lowerValue;
    if(r < 0)
    {
        r += mod;
    }
    cout << r << endl;
    return 0;
    
}
0