写一个灵神板子
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define int long long
int dp[17][23][3];
int solve(int a){
string s=to_string(a);
int n=s.size();
memset(dp,-1,sizeof(dp));
function<int(int,int,int,bool)>dfs=[&](int idx,int val,int f,bool lim)->int{
if(idx==n)return (f||!val)?1:0;
if(!lim&&dp[idx][val][f]!=-1)return dp[idx][val][f];
int res=0;
for(int d=0,up=lim?s[idx]-'0':9;d<=up;++d){
res+=dfs(idx+1,(val*10+d)%7,f||(d==7),lim&&d==up);
}
if(!lim)dp[idx][val][f]=res;
return res;
};
return dfs(0,0,0,true);
}
signed main(void){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int a;cin>>a;
int k;cin>>k;
int low=solve(a);
int l=a+1,r=4e12;
int ans=-1;
while(l<=r){
int mid=l+r>>1;
int t=solve(mid);
if(t-low>=k){
ans=mid;
r=mid-1;
}else l=mid+1;
}
cout<<ans<<"\n";
return 0;
}