C. Devyatkino
题目链接👈
题目描述🥰

题目思路😀
这个题目其实是不难的,但是正解的解法感觉很新奇🤔
首先我们可以观察到我们的答案肯定是小于等于9的,所以我们在0到9次操作进行枚举,如果某次操作时某位已经到达7了,那么就可以直接返回操作数了。
Question:怎么确定是否有可能将由k个九组成的数字精确相加,产生一个数字7?
要简单回答像 999999..99这样的加法会如何影响一个任意数的数位是相当困难的,但是这个解法的精妙之处就是在于把这个9999...999当做成10x-1,这样实际上我们进行k次操作就是在n-k的基础上加上k个10的幂
这个问题解决了,那么题目就得到了大大简化,我们直接枚举操作数k,计算n-k的数位里小于7中最接近7的数字res,如果这个res+k>=7,就相当于可以达到7,可以直接退出枚举了。
AC代码🧠
// Problem: C. Devyatkino
// Contest: Codeforces - Codeforces Round 1004 (Div. 2)
// URL: https://codeforces.com/contest/2067/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define dev1(a) cout << #a << '=' << a << endl;
#define dev2(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl;
#define dev3(a, b, c) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << endl;
#define dev4(a, b, c, d) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << " " << #d << " = " << d << endl;
#define dev5(a, b, c, d, e) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << " " << #d << " = " << d << " " << #e << " = " << e << endl;
#define vec(a) \
for (int i = 0; i < a.size(); i++) \
cout << a[i] << ' '; \
cout << endl;
#define darr(a, _i, _n) \
cout << #a << ':'; \
for (int ij = _i; ij <= _n; ij++) \
cout << a[ij] << ' '; \
cout << endl;
#define cin(a,n) \
for(int i=0;i<n;i++) \
cin>>a[i];
#define endl "\n"
#define pow pim
int pim(int a,int k)
{
int res=1;
if(a==0)return 0;
while(k)
{
if(k&1)res=(int)res*a;
k>>=1;
a=(int)a*a;
}
return res;
}
#define fi first
#define se second
#define caseT \
int T; \
cin >> T; \
while (T--)
// #define int long long
// #define int __int128
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 99999999;
// const int N = ;
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int lcm(int a, int b)
{
return a * b / gcd(a, b);
}
inline int read()
{
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
void solve()
{
int n;
cin >> n;
//最多有9次操作
for (int i = 0; i<= 9; i++) {
string s = to_string(n - i);
int ans = 0;
for (char c: s) {
if (c <= '7') {
ans = max(ans, c - '0');
}
}
if (i +ans >= 7) {
cout << i <<endl;
return;
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
caseT
solve();
return 0;
}
/*
*/
posted on
浙公网安备 33010602011771号