Codeforces Beta Round #91 div.2 滑稽场 A-D

这场难度非常鬼畜.

T1

暴力枚举判断幸运数字以及是否整除.2分钟搞定.

#include<bits/stdc++.h> //Ithea Myse Valgulious
namespace chtholly{
typedef long long ll;
#define re0 register int
#define rec register char
#define rel register ll
#define gc getchar
#define pc putchar
#define p32 pc(' ')
#define pl puts("")
/*By Citrus*/
inline int read(){
  re0 x=0,f=1;rec c=gc();
  for (;!isdigit(c);c=gc()) f^=c=='-';
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return f?x:-x;
  }
inline void read(rel &x){
  x=0;re0 f=1;rec c=gc();
  for (;!isdigit(c);c=gc()) f^=c=='-';
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  x=f?x:-x;
  }
template <typename mitsuha>
inline int write(mitsuha x){
  if (!x) return pc(48);
  if (x<0) x=-x,pc('-');
  re0 bit[20],i,p=0;
  for (;x;x/=10) bit[++p]=x%10;
  for (i=p;i;--i) pc(bit[i]+48);
  }
inline char fuhao(){
  rec c=gc();
  for (;isspace(c);c=gc());
  return c;
  }
}using namespace chtholly;
using namespace std;
int n=read();

int is_lucky(int n){
for (;n;n/=10){
  int t=n%10;
  if (t!=7&&n!=4) return 0;
  }return 1;
}

int main(){
for (int i=4;i<=n;++i) if (is_lucky(i)&&n%i==0) return puts("YES"),0;
puts("NO");
}

T2

其实就是判断字符串"4"和"7"哪种比较多.我暴力枚举用了string的substr()函数,用错了Wa了两发.

#include<bits/stdc++.h> //Ithea Myse Valgulious
namespace chtholly{
typedef long long ll;
#define re0 register int
#define rec register char
#define rel register ll
#define gc getchar
#define pc putchar
#define p32 pc(' ')
#define pl puts("")
/*By Citrus*/
inline int read(){
  re0 x=0,f=1;rec c=gc();
  for (;!isdigit(c);c=gc()) f^=c=='-';
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return f?x:-x;
  }
inline void read(rel &x){
  x=0;re0 f=1;rec c=gc();
  for (;!isdigit(c);c=gc()) f^=c=='-';
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  x=f?x:-x;
  }
template <typename mitsuha>
inline int write(mitsuha x){
  if (!x) return pc(48);
  if (x<0) x=-x,pc('-');
  re0 bit[20],i,p=0;
  for (;x;x/=10) bit[++p]=x%10;
  for (i=p;i;--i) pc(bit[i]+48);
  }
inline char fuhao(){
  rec c=gc();
  for (;isspace(c);c=gc());
  return c;
  }
}using namespace chtholly;
using namespace std;
string s;

int is_lucky(int l,int r){
for (int i=l;i<=r;++i) if (s[i]!='4'&&s[i]!='7') return 0;
return 1; 
}

int find(int k,string a){
int t=s.find(a,k);
if (t==-1) return 0;
return find(t+1,a)+1;
}

int main(){
cin>>s;
int ans=-1;
string res;
for (int i=0;i<=s.size();++i){
  for (int j=i;j<=s.size();++j){
    if (is_lucky(i,j)&&s.substr(i,j-i+1)!=""){
      int tmp=find(0,s.substr(i,j-i+1));
      if (tmp>ans){
        res=s.substr(i,j-i+1);
        ans=tmp;
        }
      else if (tmp==ans&&s.substr(i,j-i+1)<res){
        res=s.substr(i,j-i+1);
        }
      } 
    }
  }
if (!~ans) puts("-1");
else cout<<res;
}

T3

[l,r]区间里大于或者等于每一个数的最小幸运数字之和.
可以发现对于两个幸运数字之间的数字,它们的next函数值都是相同的.我们对询问进行分块.
先dfs预处理所有幸运数字,然后一个一个去跑就可以了.
预处理幸运数字要处理到10位,否则会炸.

#include<bits/stdc++.h> //Ithea Myse Valgulious
namespace chtholly{
typedef long long ll;
#define re0 register int
#define rec register char
#define rel register ll
#define gc getchar
#define pc putchar
#define p32 pc(' ')
#define pl puts("")
/*By Citrus*/
inline int read(){
  re0 x=0,f=1;rec c=gc();
  for (;!isdigit(c);c=gc()) f^=c=='-';
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return f?x:-x;
  }
inline void read(rel &x){
  x=0;re0 f=1;rec c=gc();
  for (;!isdigit(c);c=gc()) f^=c=='-';
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  x=f?x:-x;
  }
template <typename mitsuha>
inline int write(mitsuha x){
  if (!x) return pc(48);
  if (x<0) x=-x,pc('-');
  re0 bit[20],i,p=0;
  for (;x;x/=10) bit[++p]=x%10;
  for (i=p;i;--i) pc(bit[i]+48);
  }
inline char fuhao(){
  rec c=gc();
  for (;isspace(c);c=gc());
  return c;
  }
}using namespace chtholly;
using namespace std;
const int mulu=2e5;
ll ans,lnum[mulu],cnt,l=read(),r=read();

void dfs(int k,ll num){
if (!k) return void(lnum[++cnt]=num);
dfs(k-1,num*10+4);
dfs(k-1,num*10+7);
}

void pre(){
int i;
for (i=1;i<=10;++i) dfs(i,0);
sort(lnum+1,lnum+cnt+1);
}

int main(){
re0 i;
pre();
for (;l<=r;){
  int pos=lower_bound(lnum+1,lnum+cnt+1,l)-lnum;
  ans+=(min(r,lnum[pos])-l+1)*lnum[pos];
  l=lnum[pos]+1;
  }
write(ans);
}

T4

滑稽模拟.
直接看样例就可以找出规律.一旦碰到"447"或者477再符合一些玄学条件就可以无限操作下去.
其他情况只能操作一次.最后判断一下k的奇偶性决定这一位是4或者7即可.

#include<bits/stdc++.h> //Ithea Myse Valgulious
namespace chtholly{
typedef long long ll;
#define re0 register int
#define rec register char
#define rel register ll
#define gc getchar
#define pc putchar
#define p32 pc(' ')
#define pl puts("")
/*By Citrus*/
inline int read(){
  re0 x=0,f=1;rec c=gc();
  for (;!isdigit(c);c=gc()) f^=c=='-';
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return f?x:-x;
  }
inline void read(rel &x){
  x=0;re0 f=1;rec c=gc();
  for (;!isdigit(c);c=gc()) f^=c=='-';
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  x=f?x:-x;
  }
template <typename mitsuha>
inline int write(mitsuha x){
  if (!x) return pc(48);
  if (x<0) x=-x,pc('-');
  re0 bit[20],i,p=0;
  for (;x;x/=10) bit[++p]=x%10;
  for (i=p;i;--i) pc(bit[i]+48);
  }
inline char fuhao(){
  rec c=gc();
  for (;isspace(c);c=gc());
  return c;
  }
}using namespace chtholly;
using namespace std;
const int yuzu=1e5;
int n=read(),k=read();
char c[yuzu|10];
int main(){
int i;
scanf("%s",c+1);
for (i=1;i<n&&k;++i){
  if (c[i]=='4'&&c[i+1]=='7'){
    k--;
    if (i&1){
      c[i+1]='4';
      if (c[i+2]=='7'){
        c[i+1]=k&1?'7':'4';
        k=0;
        }
      }
    else{
      c[i]='7';
      if (c[i-1]=='4'){
        c[i]=k&1?'4':'7';
        k=0;
        }
      }
    }
  }
puts(c+1);
}

猜你喜欢

转载自blog.csdn.net/qq_31908675/article/details/80047771