CF1268A Long Beautiful Integer(贪心)
思路:
贪心的考虑,如果将原数组的前$k$位放满$n$位能够满足条件的话,就会这样放。
如果不能满足条件的话,将该$k$位$+1$后一定会满足条件,这便是最小的。
要注意进位的情况,并且如果是9999这种情况的话,要变成10000,。
代码:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> #include<map> #include<set> #include<vector> #include<queue> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll>PLL; typedef pair<int, int>PII; typedef pair<double, double>PDD; #define I_int ll inline ll read() { ll x = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-')f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } #define read read() #define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define multiCase int T;cin>>T;for(int t=1;t<=T;t++) #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i<(b);i++) #define per(i,a,b) for(int i=(a);i>=(b);i--) #define perr(i,a,b) for(int i=(a);i>(b);i--) ll ksm(ll a, ll b, ll p) { ll res = 1; while(b) { if(b & 1)res = res * a % p; a = a * a % p; b >>= 1; } return res; } const int inf = 0x3f3f3f3f; #define PI acos(-1) const int maxn=5e5+100; int n,k,a[210000],b[210000]; bool check(){ rep(i,1,n){ if(a[i]<b[i]) return 1; else if(a[i]>b[i]) return 0; } return 1; } int main() { n=read,k=read; rep(i,1,n) scanf("%1d",&a[i]); rep(i,1,n){ if(i<=k) b[i]=a[i]; else b[i]=b[i-k]; } if(check()){ cout<<n<<endl; rep(i,1,n) cout<<b[i]; puts(""); } else{ b[k]++; for(int i=k;i;i--){ if(b[i]>9){ b[i]%=10;b[i-1]++; } else break; } if(b[0]==1){ k++;n+=2; b[1]=1; rep(i,2,k) b[i]=0; } rep(i,1+k,n){ b[i]=b[i-k]; } cout<<n<<endl; rep(i,1,n) cout<<b[i]; } return 0; } /* **/