Educational Codeforces Round 135 (Rated for Div. 2)C. Digital Logarithm(思维)

文章目录

题目链接

C. Digital Logarithm

题意

给两个长度位 n n n的数组 a a a、 b b b,一个操作 f f f

定义操作 f f f为, a [ i ] = f ( a [ i ] ) = a [ i ] a[i]=f(a[i])=a[i] a[i]=f(a[i])=a[i]的位数

求最少多少次操作可以使 a 、 b a、b a、b两个数组变得完全相同

题解

性质:

对于任何数,经过两次操作我们一定可以让其变为 1 1 1,所以答案小于等于 2 n 2n 2n

然后我们考虑如何求最少的操作次数,很自然的去考虑贪心,对于相同的数我们不去操作,只取操作不同的数,这些不同的数一定需要进行一次操作,然后操作完一次之后所有的数都被限制到 [ 1 , 9 ] [1,9] [1,9]之内,我们只需要统计 [ 2 , 9 ] [2,9] [2,9]之内的数还需要操作几次即可。

代码

cpp 复制代码
#include <bits/stdc++.h> 
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back

using namespace std;
const int N=1e5+10;


void solve()
{
	string s;cin>>s;
	if(s.find('0')!=s.npos){
		cout<<"YES"<<endl;
		cout<<0<<endl;
		return;
	}
	rep(i,0,s.size()-1){
		rep(j,i+1,s.size()-1){
			rep(k,j+1,s.size()-1){
				int a=s[i]-'0',b=s[j]-'0',c=s[k]-'0';
				if((a*100+b*10+c)%8==0){
					cout<<"YES"<<endl;
					cout<<s[i]<<s[j]<<s[k]<<endl;
					return;
				}
			}
			int a=s[i]-'0',b=s[j]-'0';
			if((a*10+b)%8==0){
				cout<<"YES"<<endl;
				cout<<s[i]<<s[j]<<endl;
				return;
			}
		}
		int c=s[i]-'0';
		if(c%8==0){
			cout<<"YES"<<endl;
			cout<<s[i]<<endl;
			return;
		}
	}
	cout<<"NO"<<endl;
}

signed main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//   	freopen("1.in", "r", stdin);
  	int _;
//	cin>>_;
//	while(_--)
	solve();
	return 0;
}
相关推荐
桦说编程2 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
孟健3 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
躺平大鹅4 小时前
Java面向对象入门(类与对象,新手秒懂)
java
码路飞5 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
初次攀爬者5 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺5 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart6 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
曲幽7 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
NE_STOP7 小时前
MyBatis-mybatis入门与增删改查
java