Sponsors

Codeforces Round #429 (Div. 2), problem: (E) On the Bench Solution In C/C++

#include<bits/stdc++.h>
#define ll long long int
#define LL long long int
#define ULL unsigned long long int
#define sf(a) scanf(“%d”,&a)
#define sl(a) scanf(“%lld”,&a)
#define fr first
#define sc second
#define pii pair<int,int>
#define pll pair<LL,LL>
#define vi vector<int>
#define vll vector<LL>
#define vpii vector<pii>
#define rep1(a,b) for(int a=1;a<=b;a++)
#define rep2(a,b) for(int a=0;a<b;a++)
#define CLR(a,b) memset(a,b,sizeof(a))
#define Clear(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define LSOne(S) (S&(-S))
#define all(a) a.begin(),a.end()
#define Prime 31
using namespace std;
#define maxn 200100
#define INF 1ll<<62
#define mMax 20005
#define nMax 2505
#define SZ(a) a.size()
LL ar1[305];
class UnionFind { // OOP style
private:
vi p, rank, setSize; // remember: vi is vector<int>
int numSets;
public:
UnionFind(int N) {
setSize.assign(N, 1); numSets = N; rank.assign(N, 0);
p.assign(N, 0); for (int i = 0; i < N; i++) p[i] = i; }
int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
void unionSet(int i, int j) {
if (!isSameSet(i, j)) { numSets–;
int x = findSet(i), y = findSet(j);
// rank is used to keep the tree short
if (rank[x] > rank[y]) { p[y] = x; setSize[x] += setSize[y]; }
else { p[x] = y; setSize[y] += setSize[x];
if (rank[x] == rank[y]) rank[y]++; } } }
int numDisjointSets() { return numSets; }
int sizeOfSet(int i) { return setSize[findSet(i)]; }
};
LL bigmod(LL a,LL p)
{
if(p==1) return a%mod;
if(p%2) return (a*bigmod(a,p-1))%mod;
LL c=bigmod(a,p/2);
return (c*c)%mod;
}
LL factn[1000],invfactn[1000];
LL factm[1000],invfactm[1000];
void init(int n)
{
factn[0]=1;
invfactn[0]=1;
for(LL i=1;i<=n;i++) factn[i]=(factn[i-1]*i)%mod;
for(LL i=1;i<=n;i++) invfactn[i]=(invfactn[i-1]*bigmod(i,mod-2))%mod;
return ;
}
void init2(int n)
{
factm[0]=1;
invfactm[0]=1;
for(LL i=1;i<=n;i++) factm[i]=(factm[i-1]*i)%mod;
for(LL i=1;i<=n;i++) invfactm[i]=(invfactm[i-1]*bigmod(i,mod-2))%mod;
return ;
}
LL nCr(int n,int r)
{
if(n<0) return 0;
if(n<r) return 0;
LL res=(invfactn[n-r]*invfactn[r])%mod;
res=(res*factn[n])%mod;
return res;
}
LL nPr2(int n,int r)
{
LL res=(invfactm[n-r])%mod;
res=(res*factm[n])%mod;
return res;
}
vi vec1;
LL dp[305][305];
LL call(int i,int bad,int n,int total)
{
if(i==n) return (bad==0);
LL &ret=dp[i][bad];
if(ret!=-1) return ret;
ret=0;
int cur_total=total+vec1[i];
int gaps=total+1;
for(int split=1;split<= min(gaps,vec1[i]);split++)
{
for(int chosen_bad_gap=0;chosen_bad_gap<=min(bad,split);chosen_bad_gap++)
{
int good_gaps=gaps-chosen_bad_gap;
int new_bad= bad-chosen_bad_gap+vec1[i]-split;
int good_splits=split-chosen_bad_gap;
LL temp=1;
temp= (nCr(vec1[i]-1,split-1)*factn[vec1[i]])%mod;
temp = (temp*nCr(bad,chosen_bad_gap))%mod;
temp = (temp* nCr(gaps-bad,good_splits) )%mod;
temp = (temp* call(i+1,new_bad,n,total+vec1[i]))%mod;
ret = (ret+temp)%mod;
}
}
return ret;
}
int main()
{
#ifdef shakil
freopen(“input.txt”,”r”,stdin);
//freopen(“output.txt”,”w”,stdout);
#endif
map<int,int> Map;
int n,k;
cin>>n;
for(int i=0;i<n;i++) {scanf(“%d”,&ar1[i]);Map[ar1[i]]++;}
UnionFind uf(n);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
double a=sqrt(ar1[i]*ar1[j]);
if(floor(a)==ceil(a))
uf.unionSet(i,j);
}
init(n+2);
int s=uf.numDisjointSets();
set<int> S;
for(int i=0;i<n;i++) S.insert(uf.findSet(i));
vi x;
for(set<int>::iterator it=S.begin();it!=S.end();it++) x.pb(*it);
for(int i=0;i<x.size();i++) vec1.pb(uf.sizeOfSet(x[i]));
CLR(dp,-1);
LL res=call(0,0,x.size(),0);
//for(map<int,int>::iterator it=Map.begin();it!=Map.end();it++) res= (res*invfactn[it->sc])%mod;
printf(“%lld\n”,res);
return 0;
}

Ice Cube Says Critical...

Ice Cube's Shocking Revelation: "War of the Worlds" Remake...

Curtis Jones to Miss...

Curtis Jones Out, But Liverpool Avoid Major Injury Concerns...

Los Angeles County child...

Los Angeles County Child Dies from Measles Complications A tragic...

NASA Says Mars Rover...

NASA's Perseverance Rover Finds Potential Biosignature on Mars In a...

Bettors Bet Big On...

Bettors Bet Big on Trump Pardoning Binance Founder Changpeng...

6 Best Long-Running Sci-Fi...

6 Best Long-Running Sci-Fi TV Series to Binge-Watch Science fiction...

Ice Cube Says Critical Disaster Shot Without Director Or Actors Present

Ice Cube's Shocking Revelation: "War of the Worlds" Remake Filmed Without Crew? The 2025 remake of H.G. Wells's War of the Worlds has been met...

Curtis Jones to Miss Burnley But No Other Post-Internationals Injury Concerns

Curtis Jones Out, But Liverpool Avoid Major Injury Concerns After International Break Liverpool fans can breathe a sigh of relief. While Curtis Jones will...

Los Angeles County child dies from complications caused by measles infection, public health officials say

Los Angeles County Child Dies from Measles Complications A tragic incident in Los Angeles County highlights the severe and potentially fatal consequences of measles infection,...

NASA Says Mars Rover Discovered Potential Biosignature Last Year

NASA's Perseverance Rover Finds Potential Biosignature on Mars In a groundbreaking announcement, NASA has revealed that its Perseverance rover discovered a potential biosignature in a...

Bettors Bet Big On Trump Pardoning Binance Founder Changpeng Zhao

Bettors Bet Big on Trump Pardoning Binance Founder Changpeng Zhao A surprising prediction market trend has emerged, suggesting a significant possibility of a presidential pardon...

6 Best Long-Running Sci-Fi TV Series to Binge-Watch

6 Best Long-Running Sci-Fi TV Series to Binge-Watch Science fiction television has given us some of the most imaginative and enduring stories in recent years....

Judge sentences Australian woman to life in prison for fatal mushroom poisoning of in-laws

Australian Woman Receives Life Sentence for Fatal Mushroom Poisoning Erin Patterson, an Australian woman, has been sentenced to life in prison for the fatal mushroom...

Chainsaw Man – The Movie: Reze Arc Anime Announces Insert Song Artist

Chainsaw Man Movie's Reze Arc: Maximum the Hormone Returns! Fans of Chainsaw Man are in for a treat! The upcoming anime movie focusing on...

You can still save 20% off Steelcase office chairs and desks during this extended Labor Day sale

Score Big Savings on Steelcase Furniture: Extended Labor Day Sale! Labor Day may be over, but the deals aren't! Steelcase, a renowned name in...