zoukankan      html  css  js  c++  java
  • 2019牛客暑期多校训练营(第四场) K number(前缀和)

    AC代码:

    语言:C++ 代码长度:1246 运行时间: 8 ms 占用内存:632K

     1 #include<bits/stdc++.h>
     2 #define numm ch-48
     3 #define pd putchar(' ')
     4 #define pn putchar('
    ')
     5 #define pb push_back
     6 #define mp make_pair
     7 #define fi first
     8 #define se second
     9 #define fi first
    10 #define se second
    11 #define fre1 freopen("1.txt","r",stdin)
    12 #define fre2 freopen("2.txt","w",stdout)
    13 using namespace std;
    14 template <typename T>
    15 void read(T &res) {
    16     bool flag=false;char ch;
    17     while(!isdigit(ch=getchar())) (ch=='-')&&(flag=true);
    18     for(res=numm;isdigit(ch=getchar());res=(res<<1)+(res<<3)+numm);
    19     flag&&(res=-res);
    20 }
    21 template <typename T>
    22 void write(T x) {
    23     if(x<0) putchar('-'),x=-x;
    24     if(x>9) write(x/10);
    25     putchar(x%10+'0');
    26 }
    27 const int maxn=100010;
    28 typedef long long ll;
    29 typedef long double ld;
    30 const ll mod=1e9+7;
    31 const int inf=0x3f3f3f3f;
    32 ll cnt[3];
    33 ///记录从1-i前缀和(mod 3)为1,2,3的出现次数
    34 char s[maxn];
    35 int main()
    36 {
    37     scanf("%s",s);
    38     int len=strlen(s);
    39     cnt[0]=1;///一开始,前缀和为0,所以cnt[1]=1;
    40     cnt[1]=cnt[2]=0;
    41     ll sum=0LL,ans=0LL;
    42     for(int i=0;i<len;i++) {
    43         sum=(sum+(s[i]-48))%3;
    44         if(i>=1&&s[i]==48&&s[i-1]==48)
    45         ///个位十位00,从i到0找前缀和与当前相同(说明这段区间的数mod3都为0)的数
    46         ///把个数加起来
    47             ans+=cnt[sum];
    48         else if(s[i]==48) ans++;
    49         cnt[sum]++;
    50     }
    51     write(ans);pn;
    52     return 0;
    53 }
    代码在这里!
  • 相关阅读:
    使用公用表表达式(CTE)简化嵌套SQL WITH AS的含义
    C#中Array与ArrayList的区别
    Asp.net 网页中的嵌入式代码
    Asp.net核心对象
    Latex学习(载入图片并居中)
    matlab练习程序(生成加密p文件)
    matlab练习程序(直方图反向投影)
    matlab练习程序(非负矩阵分解)
    matlab练习程序(PSNR)
    matlab练习程序(动感模糊)
  • 原文地址:https://www.cnblogs.com/wuliking/p/11256289.html
Copyright © 2011-2022 走看看