โŒ

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Overcounting Issue in solution Implementation

I was trying to solve [this][1] question.

I tried to come up with the following approach.

First sort all the numbers then iterate from large to small while doing these steps, Suppose the number we are currently iterating is num. Find how many factors of the number num are there in the array. Suppose it is x, then we have Select at least one factor of num total ways to have the number num as the maximum, also we have the remaining (n-x) numbers, which can give Left numbers other choices.

We multiply these two and get the total ways we will get the number num as our answer. So, for each number a[i](from max to min), we have Final Answer for each array element as contribution to answer.

But I am getting wrong answer in Sample Test Case 3 this way. I think I am overcounting somewhere. Here is my code.

#include <bits/stdc++.h>

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define MULTI int _T; cin >> _T; while(_T--)
#define el '\n'
#define yes cout<<"YES"<<el
#define no cout<<"NO"<<el
#define f(i,a,b) for(ll i = a; i <= b; i++)
#define fr(i,a,b) for(ll i = a; i >= b; i--)
#define pb push_back
#define all(x) x.begin(),x.end()
#define sz(x) ((int)(x).size())

typedef tree<int, null_type, less<int>, rb_tree_tag, 
tree_order_statistics_node_update> pbds; // find_by_order, 
order_of_key
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const ll mod = 998244353;

ll powmod(ll a, ll b, ll p){
   a %= p;
   if (a == 0) return 0;
   ll product = 1;
   while(b > 0){
   if (b&1){    // you can also use b % 2 == 1
        product *= a;
        product %= p;
        --b;
    }
    a *= a;
    a %= p;
    b /= 2;    // you can also use b >> 1
}
    return product;
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);

ll n;
cin>>n;
vector<ll> a(n);
vector<vector<ll>> factor(1e5+1);
vector<ll> b(1e5+1,0),c(1e5+1,0);
f(i,0,n-1)
{
    cin>>a[i];
    ++c[a[i]];
}
sort(all(a));
f(i,1,1e5)
{
    for(int j=i;j<=1e5;j+=i)
    {
        if(c[j] && c[i])
            factor[j].pb(i);
    }
}
ll z =0;
ll ans = 0;
fr(i,n-1,0)
{
    while(i>=1 && a[i]==a[i-1])
    {
        --i;
    }
    ll temp = 0;
    for(auto it:factor[a[i]])
    {
        if(c[it])
        {
            temp+=c[it];
            c[it]=0;
        }
    }
    z+=temp;
    ans = (ans+((powmod(2,temp,mod)-1)*(powmod(2,n-z,mod)))*(a[i]))%mod;
}
cout<<ans<<el;
}

Where am I overcounting?

I will be thankful for any help!

How do I get all possible orderings of 9 zeros and 9 ones using Python?

I want to end up with a list with 48 620 nested lists, containing 9 zeros and 9 ones in different orders.

from itertools import permutations

print(list(permutations('000000000111111111', r=18)))

I assume the code above works, but every 0 and 1 is treated like an individual symbol, so for every ordering I get tons of repeats:

('0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1')
('0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1')
('0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1')
('0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1')
('0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1')
('0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1')
('0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1')
('0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1')
('0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1')
('0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1')
...

So, basically, how do I shuffle a list in every possible way, excluding repeats?

I tried to use every method from itertools, but I didn't find one that specifically does what i need.

โŒ
โŒ