A Sequence of Numbers (dp)

来源:互联网 发布:开淘宝店可以兼职吗 编辑:程序博客网 时间:2024/05/01 23:09
A - A Sequence of Numbers
Time Limit:20000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 3670

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 2E-1

Sample Output

Case 1: 5



#include<iostream>#include<cstring>#include<cstdio>#include<vector>#include<cmath>#include<algorithm>using namespace std;#define N  200300int 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
原创粉丝点击