結果
問題 | No.315 世界のなんとか3.5 |
ユーザー |
![]() |
提出日時 | 2016-08-19 13:46:45 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 425 ms / 2,000 ms |
コード長 | 3,610 bytes |
コンパイル時間 | 1,579 ms |
コンパイル使用メモリ | 167,708 KB |
実行使用メモリ | 23,296 KB |
最終ジャッジ日時 | 2024-11-07 18:26:15 |
合計ジャッジ時間 | 7,578 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 36 |
ソースコード
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> veci; typedef vector<ll> vecll; typedef vector<string> vecs; template<class T,class U> using Hash=unordered_map<T,U>; #define REP(i, a, n) for(ll i = (a); (i) < (ll)(n); (i)++) #define RREP(i, a, n) for(ll i = (n)-1; (i) >= (a); (i)--) #define rep(i, n) REP(i, 0, n) #define rrep(i, n) RREP(i, 0, n) #define MD 1000000007ULL #define _SPLIT " " template<class T> T read(){T a;cin >> a;return a;} template<class T> void read(T& a){cin >> a;} template<class T,class ...Args> void read(T& a, Args&... args){cin >> a; read(args...);} template<class T> void rarr(T& a, int n){for(int i = 0; i < n; i++) {cin >> a[i];}} template<class T> void write(T a){cout << setprecision(22) << a << endl;} template<class T,class ...Args> void write(T a, Args... args){cout << setprecision(22) << a << _SPLIT; write(args...);} template<class T> void warr(vector<T> a, const char* c = " "){cout << a[0];for(int i = 1; i < (int)a.size(); i++)cout << c << a[i];cout << endl;;} template<class T> void warr(T a, int n, const char* c = " "){cout << a[0];for(int i = 1; i < n; i++)cout << c << a[i];cout << endl;} void split(string s, string delim, veci& result){result.clear();string::size_type pos = 0;while(pos != string::npos){string::size_type p = s.find(delim, pos);if(p == string::npos){result.push_back(atoi(s.substr(pos).data()));break;}else {result.push_back(atoi(s.substr(pos, p - pos).data()));}pos = p + delim.size();}} void split(string s, string delim, vecs& result){result.clear();string::size_type pos = 0;while(pos != string::npos){string::size_type p = s.find(delim, pos);if(p == string::npos){result.push_back(s.substr(pos));break;}else {result.push_back(s.substr(pos, p - pos));}pos = p + delim.size();}} ll gcd(ll a, ll b){while(true){ll k = a % b;if(k == 0)return b;a = b;b = k;}} ll comb(ll n, ll m){ll p=1;m=min(m,n-m);for(ll i=1;i<=m;i++){p*=n-i+1;p/=i;}return p;} ll tk[3][2]={0}; ll dt[100010]={0}; int p,pa; ll md_add(ll x,ll y){return ((x%MD)+(y%MD))%MD;} ll f(string& x,bool f){ int n=x.size(); int ps=to_string(pa).size()-1; if(n<=ps)return dt[atoi(x.c_str())-f]; ll dp[200010][2][2][3]={0}; dp[0][0][0][0]=1; rep(i,n-ps){ int dg=x[i]-'0'; rep(x,2)rep(y,2)rep(z,3)rep(w,y?10:dg+1){ ll &t=dp[i+1][x||w==3][y||w<dg][(z+w)%3]; t=(t+dp[i][x][y][z])%MD; } } ll res=0; //dp[i+1][x||w==3][y||y<dg][(z+w)%3]; rep(w,2)REP(i,1,2)rep(j,3)rep(k,3){ if(w==1){ res=md_add(res,dp[n-ps][w][i][j]*tk[k][0]); res=md_add(res,dp[n-ps][w][i][j]*tk[k][1]); } else{ res=md_add(res,dp[n-ps][w][i][j]*tk[k][1]); if((j+k)%3==0) res=md_add(res,dp[n-ps][w][i][j]*tk[k][0]); } } int nr4=0; bool in3=false; rep(i,n-ps)nr4+=x[i]-'0',in3|=(x[i]=='3'); int nd3=atoi(x.c_str()+n-ps)-f; REP(i,0,nd3+1){ if(i%p==0)continue; string s=to_string(i); bool find=in3; rep(i,s.size())find|=(s[i]=='3'); if((i+nr4)%3==0||find)res=md_add(res,1); } return res; } int main(void) { string a,b; read(a,b,p); pa=1000; if(p==80)pa=10000; else if(p==800)pa=100000; REP(i,1,pa){ dt[i]+=dt[i-1]; if(i%p==0)continue; string s=to_string(i); bool find=false; rep(i,s.size())find|=(s[i]=='3'); tk[i%3][find]++; if(i%3==0||find)dt[i]++; } write(md_add(f(b,false),MD-f(a,true))); return 0; }