Untitled

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
1.2 kB
0
Indexable
Never
#include <fstream>
using namespace std;

ifstream f("memory006.in");
ofstream g("memory006.out");

long long nr, sum, i, st, dr, p[60];
int middle;
short a[10001], t;

short index(int st, int dr) {
    if(st > dr) return 0;
    middle = (st + dr) / 2;
    if(nr == p[middle]) return middle;
    else if(nr < p[middle]) return index(st, middle - 1);
    else return index(middle + 1, dr);
}

int main()
{
    p[1] = 2;
    for(i = 2; i <= 60; ++i)
        p[i] = p[i - 1] * 2;

    int n, k, cnt = 0;
    f >> n >> k;
    sum = 0, st = 0, dr = -1;
    for(i = 1; i <= n; ++i) {
        f >> nr;
        t = index(1, 60);
        if(t == 0)
            sum = 0, st = 0, dr = -1;
        else {
            dr = (dr + 1) % 10001;
            a[dr] = t;
            sum += t;
            if(sum == k)
                cnt++;
            else if(sum > k) {
                while(sum > k) {
                    sum = sum - a[st];
                    st = (st + 1) % 10001;
                }
                if(sum == k)
                    cnt++;
            }
        }
    }
    g << cnt;

    return 0;
}