【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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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。不但比赛的时候没有做出来,比赛后也做了好久。

你可能感兴趣的:(CodeForces)