zoukankan      html  css  js  c++  java
  • 2019ICPC 上海网络赛 L. Digit sum(二维树状数组+区间求和)

    https://nanti.jisuanke.com/t/41422

     题目大意:

    给出n和b,求1到n,各数在b进制下各位数之和的总和。

    直接暴力模拟,TLE。。

    没想到是要打表。。。还是太菜了。

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <math.h>
    13 const int INF=0x3f3f3f3f;
    14 typedef long long LL;
    15 const int mod=1e9+7;
    16 const int maxn=1e6+10;
    17 using namespace std;
    18 
    19 int c[11][maxn];
    20 
    21 int lowbit(int x)
    22 {
    23     return x&(-x);
    24 }
    25 
    26 void Update(int f,int n,int val)
    27 {
    28     while(n<maxn)
    29     {
    30         c[f][n]+=val;
    31         n+=lowbit(n);
    32     }
    33 }
    34 
    35 int Query(int f,int n)
    36 {
    37     int sum=0;
    38     while(n>0)
    39     {
    40         sum+=c[f][n];
    41         n-=lowbit(n);
    42     }
    43     return sum;
    44 }
    45 
    46 void init()//对每个进制建立一个树状数组,将2到10进制的所有数的结果全计算出来
    47 {
    48     for(int i=2;i<=10;i++)
    49     {
    50         for(int j=1;j<maxn;j++)
    51         {
    52             int sum=0;
    53             int t=j;
    54             do{
    55                 sum+=(t%i);
    56                 t/=i;
    57             }while(t);
    58             Update(i,j,sum);
    59         }
    60     }
    61 }
    62 
    63 int main()
    64 {
    65     int T;
    66     scanf("%d",&T);
    67     init();//打表 
    68     for(int k=1;k<=T;k++)
    69     {
    70         int n,b;
    71         scanf("%d %d",&n,&b);
    72         printf("Case #%d: %d
    ",k,Query(b,n));
    73     }
    74     return 0;
    75 }
  • 相关阅读:
    Single Number II
    Pascal's Triangle
    Remove Duplicates from Sorted Array
    Populating Next Right Pointers in Each Node
    Minimum Depth of Binary Tree
    Unique Paths
    Sort Colors
    Swap Nodes in Pairs
    Merge Two Sorted Lists
    Climbing Stairs
  • 原文地址:https://www.cnblogs.com/jiamian/p/11525163.html
Copyright © 2011-2022 走看看