1582B - Luntik and Subsequences - CodeForces Solution


combinatorics math *900

Please click on ads to support us..

Python Code:

t=int(input())
 
def solve():
    
    n=int(input())

    lst=list(map(int,input().split()))

    x=sum(lst)


    count_0=0
    count_1=0

    for i in range(n):
        if lst[i]==0:
            count_0+=1
        if lst[i]==1:
            count_1+=1

    print(pow(2,count_0)*count_1)
    

 
        
while t:
    solve()
    t-=1

C++ Code:

#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define loop(i,a,n) for(int i = a; i <= n; i++)
#define loopr(i,n,a) for(int i = n; i >= a; i--)
#define foor(i,n) for(int i = 0; i < n; i++)
#define fore(el,v) for(auto& el:v)
#define cin(v) for(auto& el:v) cin>>el;
#define cout(v) for(auto el:v) cout<<el<<" "; cout<<'\n';
#define undirected(m) for(int iii=0;iii<m;iii++){int aaa,bbb;cin >> aaa >> bbb;adj[aaa].push_back(bbb);adj[bbb].push_back(aaa);}
#define directed(m) for(int iii=0;iii<m;iii++){int aaa,bbb;cin >> aaa >> bbb;adj[aaa].push_back(bbb);}
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Ones(n) __builtin_popcount(n)
#define Onesll(n) __builtin_popcountll(n)
#define PI acos(-1)
#define sin(a) sin((a)*PI/180)
#define cos(a) cos((a)*PI/180)
#define tan(a) tan((a)*PI/180)
#define fast ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
#define endl '\n'

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pi = pair<int,int>;
using pll = pair<ll,ll>;
using tiii = tuple<int,int,int>;
using tlll = tuple<ll,ll,ll>;
using vi = vector<int>;
using vb = vector<bool>;
using vll = vector<ll>;
using vpi = vector<pair<int,int>>;
using vvi = vector<vector<int>>;
using mii = map<int,int>;

const int MOD = 1e9+7;
const int OO = 2e9;
const int N = 1e6+5, M = 1e3+5, LG = 20;

ll powmod(ll x, ll y)
{
    x %= MOD;
    ll ans = 1;
    while(y)
    {
        if(y & 1) ans = ans * x % MOD;
        x = x * x % MOD;
        y >>= 1;
    }
    return ans;
}

ll fact[N], modInv[N];

void preprocess(){
    fact[0] = 1;
    loop(i,1,N-1){
        fact[i] = (fact[i-1] * i)%MOD;
    }
    modInv[N-1] = powmod(fact[N-1], MOD-2);
    loopr(i,N-2,0){
        modInv[i] = (modInv[i+1] * (i+1)) %MOD;
    }
}

ll nCr(int n,int r) {
    if (r > n)
        return 0;
    // return ( n! / ((n-r)! * r!) ) % mod
    return (((fact[n] * modInv[n - r]) % MOD) * modInv[r]) % MOD;
}

ll add(ll a, ll b) {
    a += b;
    if (a >= MOD) a -= MOD;
    return a;
}
ll sub(ll a, ll b) {
    a -= b;
    if (a < 0) a += MOD;
    return a;
}
ll mul(ll a, ll b) {
    return a * b % MOD;
}
ll inv(ll a) {
    return powmod(a, MOD - 2);
}

ll fastpow(ll b, ll p) {
    if (!p) return 1;
    ll ret = fastpow(b, p >> 1);
    ret *= ret;
    if (p & 1)
        ret *= b;
    return ret;
}


void TC()
{
    int n;
    cin >> n;
    ll x,l=0,o=0;
    foor(i,n){
        cin >> x;
        if(x==0) o++;
        else if(x==1) l++;
    }
    cout << fastpow(2,o) * l;
}

int32_t main()
{
#ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin); freopen("output.out", "w", stdout);
#endif
    fast
    int t = 1;
    cin >> t;
//    preprocess();
    while (t--)
    {
        TC();
        cout << '\n';
    }
    return 0;
}

					 	   		   	          					


Comments

Submit
0 Comments
More Questions

1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School