ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

被3整除的子序列-NC21302(思维+dp)

2021-09-22 23:33:12  阅读:97  来源: 互联网

标签:const temp int double NC21302 整除 include root dp


被3整除的子序列

题号:NC21302
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld

题目描述 

给你一个长度为50的数字串,问你有多少个子序列构成的数字可以被3整除
答案对1e9+7取模

输入描述:

输入一个字符串,由数字构成,长度小于等于50

输出描述:

输出一个整数

示例1

输入

复制

132

输出

复制

3

示例2

输入

复制

9

输出

复制

1

示例3

输入

复制

333

输出

复制

7

示例4

输入

复制

123456

输出

复制

23

示例5

输入

复制

00

输出

复制

3

备注:

n为长度
子任务1: n <= 5
子任务2: n <= 20
子任务3: 无限制
/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long  ll;
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid (l+r)/2
#define mms(x, y) memset(x, y, sizeof x)
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
//    return a>b?a:b;
//}
//inline double min(double a,double b){
//    return a<b?a:b;
//}
 
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
 
//void Fire(){
//    queue<node> p;
//    p.push({fx,fy,0});
//    memset(fire, -1, sizeof(fire));
//    fire[fx][fy]=0;
//    while(!p.empty()){
//        node temp=p.front();
//        p.pop();
//        for(int i=0;i<8;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m||fire[x][y]!=-1){
//                continue;
//            }
//            fire[x][y]=temp.val+1;
//            p.push({x,y,temp.val+1});
//        }
//    }
//}
//int bfs(){
//    queue<node> p;
//    memset(vis, 0, sizeof(vis));
//    p.push({sx,sy,0});
//    while (!p.empty()) {
//        node temp=p.front();
//        vis[temp.x][temp.y]=1;
//        p.pop();
//        for(int i=0;i<4;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m)  continue;
//            if(x==ex&&y==ey&&temp.val+1<=fire[x][y]) return temp.val+1;
//            if(vis[x][y]||temp.val+1>=fire[x][y]||a[x][y]=='#') continue;
//            p.push({x,y,temp.val+1});
//        }
//    }
//    return -1;
//}

//一维哈希
//int n;
//string s;
//int bas=131;
//typedef unsigned long long ull;
//const ull mod1=100001651;
//ull a[100010];
//ull Hash(string s){
//    ll ans=0;
//    for(int i=0;i<s.size();i++){
//        ans*=bas;
//        ans+=int(s[i]);
//        ans%=mod1;
//    }
//    return ans;
//}

//二维哈希
//using lom=unsigned long long ;
//const lom bas1=131,bas2=233;
//const int M=505;
//int n,m;
//char a[M][M];
//lom _hash[M][M];
//lom p1[M],p2[M];
//
//
//void init(){
//    p1[0]=1;
//    p2[0]=1;
//    for(int i=1;i<=505;i++){
//        p1[i]=p1[i-1]*bas1;
//        p2[i]=p2[i-1]*bas2;
//
//    }
//}
//void Hash(){
//    _hash[0][0]=_hash[0][1]=_hash[1][0]=0;
//    for(int i=1;i<=n;i++){    //前缀和
//        for(int j=1;j<=m;j++){
//            _hash[i][j]=_hash[i][j-1]*bas1+a[i][j]-'a';
//        }
//    }
//    for(int i=1;i<=n;i++){   //二维前缀和
//        for(int j=1;j<=m;j++){
//            _hash[i][j]+=_hash[i-1][j]*bas2;
//        }
//    }
//
//}


//int pre[1010];
//int in[1010];
//int post[1010];
//int k;
//struct node{
//    int value;
//    node *l,*r;
//    node (int value=0,node *l=NULL,node *r=NULL):value(value),l(l),r(r){}
//};
//void builttree(int l,int r,int &t,node * &root){
//    int flag=-1;
//    for(int i=l;i<=r;i++){
//        if(in[i]==pre[t]){
//            flag=i;
//            break;
//        }
//    }
//    if(flag==-1) return;
//    root=new node(in[flag]);
//    t++;
//    if(flag>l) builttree(l,flag-1,t,root->l);
//    if(flag<r) builttree(flag+1,r,t,root->r);
//
//}
//void preorder(node *root){
//    if(root!=NULL)
//    {
//        post[k++]=root->value;
//        preorder(root->l);
//        preorder(root->r);
//
//    }
//}
//void inorder(node *root){
//    if(root!=NULL)
//    {
//        inorder(root->l);
//        post[k++]=root->value;
//        inorder(root->r);
//
//    }
//}
//void postorder(node *root){
//    if(root!=NULL)
//    {
//        postorder(root->l);
//        postorder(root->r);
//        post[k++]=root->value;
//    }
//}

//线段树
//#define MAX 1000000
//int tree[MAX<<2];
//void pushup(int rt){
//    tree[rt]=tree[rt<<1]+tree[rt<<1|1];
//}
//void update(int rt,int i,int val,int l,int r)
//{
//    if(r==l) {
//        tree[rt]+=val;
//        return;
//    }
//    if(i<=mid){
//        update(ls,i,val,l,mid);
//    }
//    else {
//        update(rs,i,val,mid+1,r);
//    }
//    pushup(rt);
//}
//int query(int rt,int l,int r,int k){
//    if(l==r) return l;
//    if(tree[rt<<1|1]>=k)
//        return query(rs,mid+1,r,k);
//    else return query(ls,l,mid,k-tree[rt<<1|1]);
//}


//二维树状数组
//int lowbit(int x){
//    return x&(-x);
//}
//void updata(int x,int y,int c)
//{
//    for(int i=x;i<=n;i+=lowbit(i)){
//        for(int j=y;j<=n;j+=lowbit(j)){
//            sum[i][j]+=c;
//        }
//    }
//}
//int getsum(int x,int y){
//    int ans=0;
//    for(int i=x;i>0;i-=lowbit(i)){
//        for(int j=y;j>0;j-=lowbit(j)){
//            ans+=sum[i][j];
//        }
//    }
//    return ans;
//}

const int mmod=1e9+7;
int dp[55][3];
int main(){
    string s;
    cin>>s;
    int m;
    m=s[0]-'0';
    dp[0][m%3]=1;
    for(int i=1;i<s.length();i++){
        m=(s[i]-'0')%3;
        dp[i][m]=dp[i-1][m]+1;
        for(int j=0;j<3;j++){
            dp[i][j]+=(dp[i-1][j]+dp[i-1][(j+3-m)%3])%mmod;
        }
    }
    cout<<dp[s.length()-1][0]<<endl;
}

标签:const,temp,int,double,NC21302,整除,include,root,dp
来源: https://blog.csdn.net/m0_57006708/article/details/120425111

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有