A Sequence of Numbers (dp)

A - A Sequence of Numbers
Time Limit:20000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

You are given a sequence of N integers (each within the range [0, 2^16 - 1] ) along with P operations and in order to solve this problem you need to process the operations instructed as follows. 

There are two kinds of operations that you will be instructed to perform: 

1) Modification 
- Given a non-negative number T , you need to increase the value of every number in the sequence by T . If the value of any number in the sequence is larger than 2^16 - 1 after the operation, you should divide its value by 216 and take the remainder as its value; 
2) Query 
- Given a non-negative number T , query how many numbers in the sequence satisfies the condition that its bitwise and result with 2^T is greater than zero. 

For simplicity, all you need to do here is to output the sum ( sum < 10, 000, 000, 000 ) of the answers to all queries.

Input

There are multiple test cases in the input file. Each test case starts with one integer N (N<=10^5) , the number of integers in the sequence. The following N line consists of one integer P (0<=P<=2^16 - 1) , the value on i -th line being the value of the i -th number in the sequence. 

Each of the following lines is either of the format ``C delta " (0<=delta) , meaning that you should increase the value of every number by delta, or ``Q i " (0<=i<=15) , meaning that you should calculate the answer to the query (as explained in the problem description). Every test case ends with one character `E' on a single line, followed by a blank line. 

N = - 1 indicates the end of input file and should not be processed by your program. It is guaranteed that the total number of operations in each test case does not exceed 200,000.

Output

For each test case, print the sum of answers to queries on one separate line in the format as indicated in the sample output.

Sample Input

3 
1 
2 
4 
Q 1 
Q 2 
C 1 
Q 1 
Q 2
E

-1

Sample Output

Case 1: 5



#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
#define N  200300

int T;
int n,m;

long long dp[17][(1<<16)+10];
long long sum[17][(1<<16)+10];

long long ans=0;

void init()
{


    for(int i=15;i>=1;i--)
    {
        int mx = (1<<(i+1));
        int mod = (1<<i);
        for(int j=0;j<mx;j++)
        {
            dp[i][j % mod] += dp[i+1][j];
        }
    }

    for(int i=1;i<=16;i++)
    {
        int mx = (1<<i);
        sum[i][0] = dp[i][0];
        for(int j=1;j<mx;j++)
        {
            sum[i][j] = sum[i][j-1] + dp[i][j];
        }
    }


}

void solve(int t,int add)
{
    long long re=0;

    int mod = 1<<t;

    int ff = add % mod;
    int flag = mod & add;


    if(flag==0)
    {
        re+=sum[t+1][mod] - sum[t+1][mod - ff-1];
        re+=sum[t+1][mod*2-1-ff] - sum[t+1][mod];
        // printf("t=%d\n",t);
       // printf("re=%d %d %d %I64d %I64d\n",re,t+1,mod*2-1-ff,sum[t+1][mod*2 -1 -ff],sum[t+1][mod]);
      //   printf("%I64d %I64d\n",sum[t+1][mod*2-1-ff],sum[t+1][mod]);
    }else
    {
        re+=sum[t+1][mod - ff -1];
        re+=sum[t+1][mod*2-1] - sum[t+1][mod*2 - ff -1];
    }
    ans+=re;
}

int a[(1<<16)+10];

int main()
{
    int cas=1;
    char op[3];

    while(scanf("%d",&n)!=EOF)
    {
        if(n==-1) break;

        memset(dp,0,sizeof(dp));

        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            dp[16][a[i]]++;
        }

        init();

        int add=0,t=0;
        ans=0;

        while(scanf("%s",op)!=EOF)
        {
            if(op[0]=='E') break;
            else if(op[0]=='Q')
            {
                scanf("%d",&t);
                solve(t,add);
            }else if(op[0]=='C')
            {
                scanf("%d",&t);
                add+=t;
                add%=(1<<16);
            }
          //  printf("%I64d\n",ans);
        }

        printf("Case %d: %I64d\n",cas++,ans);

    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值