【Codeforces Round #496 (Div. 3)】D. Polycarp and Div 3

【Codeforces Round #496 (Div. 3)】D. Polycarp and Div 3

题意:给出一个大数,数位在2e5内,然后要求我们割分这串大数,使尽可能多的数可以被3整除。

题解:

  1. 0除外,所有3的倍数都有一个特点就是每位上的数值相加的和与0对模3同余。
  2. (a+b)%c=(a%c+b%c)%c,所以我们可以先将大数的每一位做模3的预处理
  3. 考虑到所有情况:
0
1 0
1 1 0
1 1 1
1 1 2
1 2
2 0
2 1
2 2 0
2 2 1
2 2 2
  • 需要注意的就是存数的数组的初始化,要初始化为-1,或者用数位的长度判断一下,防止数组被判长。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <list>
#include <map>
#define P(x) x>0?x:0
#define INF 0x3f3f3f3f

using namespace std;
typedef long long ll;
typedef vector<int>:: iterator VITer;
const int maxn=2e5+5;

char c[maxn];
int a[maxn];
int ans;

int main()
{
    while(~scanf("%s",c))
    {
        int len=strlen(c);
        ans=0;
        int begin_;
        for(int i=0;i<len;i++)
        {
            if(c[i]=='0')
                continue;
            begin_=i;
            break;
        }
        memset(a,-1, sizeof(a));
        for(int i=begin_;i<len;i++)
        {
            a[i]=(c[i]-'0')%3;
        }
        for(int i=begin_;i<len;i++)
        {
            if(a[i]==0)
            {
                ans++;
                continue;
            }
            if(a[i]==1)
            {
                if(a[i+1]==0)//1 0
                {
                    ans++;
                    i++;
                    continue;
                }
                if(a[i+1]==2)//1 2
                {
                    ans++;
                    i++;
                    continue;
                }
                if(a[i+1]==1&&a[i+2]>=0)//1 1 1/1 1 0/1 1 2
                {
                    ans++;
                    i++;
                    i++;
                    continue;
                }
            }
            if(a[i]==2)
            {
                if(a[i+1]==0)//2 0
                {
                    ans++;
                    i++;
                    continue;
                }
                if(a[i+1]==1)//2 1
                {
                    ans++;
                    i++;
                    continue;
                }
                if(a[i+1]==2&&a[i+2]>=0)//2 2 2/ 2 2 1/ 2 2 0
                {
                    ans++;
                    i++;
                    i++;
                    continue;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

My_Feeling:
哇啊啊啊啊啊啊,我tcl,想哭,一点儿都不擅长做这种题。(⊙﹏⊙),QAQ。不但比赛的时候没有做出来,比赛后也做了好久。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值