이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
int dp[18][11][11][2];
int solve(string s,int i,int last,int secondlast,int tight)
{
if(i == s.length())
{
return 1;
}
if(dp[i][last][secondlast][tight]!= -1)return dp[i][last][secondlast][tight];
int limit = tight ? s[i] - '0' : 9;
int ans = 0;
for(int d = 0;d<=limit;d++)
{
if(d == last)
continue;
if(d == secondlast)
continue;
ans = ans + solve(s,i+1,d,last,tight && (d == limit));
}
return dp[i][last][secondlast][tight] = ans;
}
int getAns(string s)
{
int ans = 1;
int l = s[0] - '0';
for(int i=1;i<=l;i++)
{
ans = ans + solve(s,1,i,i,i == l);
}
for(int i=2;i<s.size();i++)
{
for(int j=1;j<=9;j++)
{
ans += solve(s,i,j,j,0);
}
}
return ans;
}
signed main()
{
int t;
cin >> t;
while(t--)
{
int l,r;
cin >> l >> r;
memset(dp,-1,sizeof dp);
int x = getAns(to_string(l-1));
memset(dp,-1,sizeof dp);
int y = getAns(to_string(r));
// cout << x << ' ' << y << '\n';
cout << y - x << '\n';
}
}
컴파일 시 표준 에러 (stderr) 메시지
numbers.cpp: In function 'long long int solve(std::string, long long int, long long int, long long int, long long int)':
numbers.cpp:11:10: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | if(i == s.length())
| ~~^~~~~~~~~~~~~
numbers.cpp: In function 'long long int getAns(std::string)':
numbers.cpp:46:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int i=2;i<s.size();i++)
| ~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |