日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 1560F2 Nearest Beautiful Number (hard version)(二分+数位dp)

發(fā)布時間:2024/4/11 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 1560F2 Nearest Beautiful Number (hard version)(二分+数位dp) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出一個十進制數(shù)字 nnn 和一個約束 kkk,問大于等于 nnn 且滿足不同的數(shù)位個數(shù)小于等于 kkk 的最小的數(shù)字是多少

題目分析:自己寫的貪心太丑了,就不放上來丟人了

偷的楊大佬的trick,對于某個數(shù)字 xxx,如果可以快速求出區(qū)間 [1,x][1,x][1,x] 中有多少個數(shù)字滿足不同的數(shù)位個數(shù)小于等于 kkk 的話,顯然這個答案是具有單調性的

考慮二分答案,然后用數(shù)位dp快速求解這個過程,dp[pos][state][k]dp[pos][state][k]dp[pos][state][k] 記憶化一下到了第 pospospos 位置,當前不同的數(shù)位狀態(tài)為 statestatestate,小于等于 kkk 的方案數(shù)。這里的 statestatestate 是一個 [0,210][0,2^{10}][0,210] 的數(shù),也就是狀壓了一下狀態(tài)

雖然時間復雜度看起來很不可觀,但是據(jù)楊大佬的經(jīng)驗所述,均攤下來的時間復雜度為 O(很快)O(很快)O()

代碼:

// Problem: F2. Nearest Beautiful Number (hard version) // Contest: Codeforces - Codeforces Round #739 (Div. 3) // URL: https://codeforces.com/contest/1560/problem/F2 // Memory Limit: 256 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> #define lowbit(x) (x&-x) using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; int dp[15][(1<<10)+10][10]; int b[15],cnt,k; inline int count(int x) {return __builtin_popcount(x); } int dfs(int pos,int state,int limit,int lead) {if(pos==-1) {return count(state)<=k;}if(dp[pos][state][k]!=-1&&!limit&&!lead) {return dp[pos][state][k];}int up=limit?b[pos]:9;int ans=0;for(int i=0;i<=up;i++) {if(lead&&i==0) {ans+=dfs(pos-1,state,limit&&i==up,1);} else {ans+=dfs(pos-1,state|(1<<i),limit&&i==up,0);}}if(!limit&&!lead) {dp[pos][state][k]=ans;}return ans; } int solve(LL x) {cnt=0;while(x) {b[cnt++]=x%10;x/=10;}return dfs(cnt-1,0,1,1); } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);memset(dp,-1,sizeof(dp));int w;cin>>w;while(w--) {int n;read(n),read(k);int cur=solve(n-1);LL l=n,r=2e9,ans=-1;while(l<=r) {LL mid=(l+r)>>1;if(solve(mid)-cur>=1) {ans=mid;r=mid-1;} else {l=mid+1;}}printf("%lld\n",ans);}return 0; }

總結

以上是生活随笔為你收集整理的CodeForces - 1560F2 Nearest Beautiful Number (hard version)(二分+数位dp)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。