結果

問題 No.319 happy b1rthday 2 me
ユーザー koyumeishikoyumeishi
提出日時 2015-12-12 01:36:51
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,695 bytes
コンパイル時間 757 ms
コンパイル使用メモリ 85,916 KB
実行使用メモリ 4,476 KB
最終ジャッジ日時 2023-10-13 11:35:41
合計ジャッジ時間 2,045 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;
/*
1***2 1***3 1***4 ... 
2***1 2***2

dp[sml][last] := cnt 12
dp[head][edge]

*/

//resize(vector, {v1,v2,v3});

void resize(int& val , vector<int>::iterator itr){val = 0;}
void resize(long long& val , vector<int>::iterator itr){val = 0;}
void resize(double& val , vector<int>::iterator itr){val = 0;}

template<class T>
void resize(vector<T>& vec, vector<int>::iterator itr){
	vec.resize(*itr);
	for(int i=0; i<*itr; i++){
		resize(vec[i], itr+1);
	}
}

template<class T>
void resize(T& vec, vector<int> sz){
	resize(vec, sz.begin());
}

template<class T>
ostream& operator << (ostream& os, vector<T> vec){
	for(int i=0; i<vec.size(); i++){
		os << vec[i] << " ";
	}
	os << endl;
	return os;
}

long long calc(string& x){
	vector<vector<vector<long long>>> dp(2, vector<vector<long long>>(10,vector<long long>(11, 0)));
	dp[0][0][0] = 1;

	for(int i=0; i<x.size(); i++){
		int k = x[i]-'0';
		vector<vector<vector<long long>>> dp_(2, vector<vector<long long>>(10,vector<long long>(11, 0)));

		for(int a=0; a<2; a++)
		for(int b=0; b<10; b++)
		for(int c=0; c<10; c++){
			if(dp[a][b][c] == 0) continue;
			for(int d=0; d<10; d++){
				if(a==0 && k<d) break;
				int a_ = a|(d<k);
				int b_ = d;
				int c_ = c + ((b*10+d==12)?1:0);

				dp_[a_][b_][c_] += dp[a][b][c];
			}
		}
		swap(dp, dp_);
	}
	long long ret = 0;
	for(int a=0; a<2; a++)
	for(int b=0; b<10; b++)
	for(int c=1; c<11; c++){
		ret += dp[a][b][c] * c;
	}
	return ret;
}
/*
10 11 12 13 14
*/

long long calc2(string &x){
	vector<vector<long long>> dp(4, vector<long long>(2, 0));

	long long ret = 0;
	dp[0][1] = 1;
	for(int i=0; i<x.size(); i++){
		int k = x[i] - '0';
		vector<vector<long long>> dp_(4, vector<long long>(2, 0));

		for(int a=0; a<4; a++)
		for(int b=0; b<2; b++){
			if(dp[a][b]==0) continue;
			for(int d=0; d<10; d++){
				if(b==1 && d>k) break;
				int a_ = (a>0?a:min(d,3));
				int b_ = b & (d==k);

				dp_[a_][b_] += dp[a][b];

				if(i==x.size()-1){
					//if(a_==1 && d==2 && b_==0){
					//	ret += dp[a][b];
					//}
					if(a_==2 && d==1 && b_==0){
						ret += dp[a][b];
					}
				}
			}
		}
		swap(dp, dp_);
	}
	return ret;
}

int main(){
	long long a,b;
	cin >> a >> b;

	a--;
	string a_,b_;
	char buff[100];
	sprintf(buff,"%lld", a);
	a_ = buff;
	sprintf(buff,"%lld", b);
	b_ = buff;

	long long ans = 0;

	
	ans += calc(b_);
	ans += calc2(b_);

	ans -= calc(a_);

	a++;
	sprintf(buff,"%lld", a);
	a_ = buff;

	ans -= calc2(a_);

	if(a<=1 && b>=2) ans++;

	cout << ans << endl;

	return 0;
}
0