結果
問題 | No.260 世界のなんとか3 |
ユーザー |
![]() |
提出日時 | 2016-01-02 22:36:00 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 186 ms / 2,000 ms |
コード長 | 3,454 bytes |
コンパイル時間 | 847 ms |
コンパイル使用メモリ | 77,248 KB |
実行使用メモリ | 11,136 KB |
最終ジャッジ日時 | 2024-11-07 18:10:54 |
合計ジャッジ時間 | 3,904 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
#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;}