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

文章目录

  • 题目链接
  • 题意
  • 题解
  • 代码

题目链接

C. Digital Logarithm
Educational Codeforces Round 135 (Rated for Div. 2)C. Digital Logarithm(思维)_第1张图片

题意

给两个长度位 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 ab两个数组变得完全相同

题解

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

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

代码

#include  
#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;
}

你可能感兴趣的:(codeforces,c语言,django,flask,python,scikit-learn,numpy,java)