CF550C Divisibility by Eight
作者:互联网
- 题意:给一个数字,要求构造一个数字,只通过删除某些位的数,得到的是能整除 \(8\) 的数字。
- 题解:
- 代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int dp[N][9];
struct node {
int pos, data;
char ans;
}pre[N][9];
char a[N];
int main() {
cin >> (a +1);
int n = strlen(a + 1);
dp[1][(a[1] - '0')%8] = 1;
pre[1][(a[1]- '0')%8] = {0, (a[1] - '0')%8, a[1]};
for (int i = 2; i <= n; i ++) {
for (int j = 0; j < 8; j ++) {
if (dp[i-1][j]) {
dp[i][(j*10 + a[i] - '0')%8] = 1;
pre[i][(j*10 + a[i] - '0')%8] = {i-1, j,a[i]};
dp[i][j] = 1;
//cout << "?";
pre[i][j] = pre[i-1][j];
}
}
if (!dp[i][(a[i] - '0')%8]) {
dp[i][(a[i] - '0')%8] = 1;
pre[i][(a[i] - '0')%8] = {0, (a[i] - '0')%8, a[i] };
}
}
string ans = "";
for (int i = 1; i <=n ; i ++) {
if (dp[i][0]) {
node now = pre[i][0];
int u = now.pos;
while (u) {
ans += now.ans;
now = pre[now.pos][now.data];
u = now.pos;
}
ans += now.ans;
reverse(ans.begin(), ans.end());
puts("YES");
cout << ans << endl;return 0;
}
}
puts("NO");
return 0;
}
标签:pre,char,Divisibility,int,long,CF550C,Eight,dp,数字 来源: https://www.cnblogs.com/Xiao-yan/p/14738187.html