Tutorial is loading...
Author's code
#include "bits/stdc++.h"
using namespace std;
// Checking if a character is vowel
bool isVowel(char a)
{
if(a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return true;
return false;
}
int main()
{
string S, T;
cin >> S;
cin >> T;
// Answer is no if size of strings is differents
if(S.size() != T.size())
{
cout << "No\n";
return 0;
}
int flag = 1;
// Checking the condition on all characters one by one
for(int i = 0; i < S.size(); ++i)
{
if((isVowel(S[i]) && isVowel(T[i])) || (isVowel(S[i]) == false && isVowel(T[i]) == false))
{
continue;
}
flag = 0;
break;
}
if(flag)
cout << "Yes\n";
else
cout << "No\n";
return 0;
}
Tutorial is loading...
Author's code
#include <bits/stdc++.h>
using namespace std;
const long long N = 100010;
long long a[N];
int main()
{
long long n, k, m, i, sum=0, tp;
long double mx;
cin>>n>>k>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum += a[i];
}
// Sorting the array
sort(a+1, a+n+1);
// Checking for the case where none of the avengers is removed
mx = (long double)(sum+min(m, n*k))/(long double)(n);
for(int i=1;i<=min(n-1, m);i++)
{
sum -= a[i];
tp = sum + min(m-i, (n-i)*k);
mx = max(mx, (long double)(tp)/(long double)(n-i));
}
cout<<fixed<<setprecision(20)<<mx<<endl;
return 0;
}
Tutorial is loading...
Author's code
#include <bits/stdc++.h>
using namespace std;
vector<long long> avengers;
long long n,k,A,B;
// rec(l,r) returns minimum power needed to destroy l to r (inclusive)
long long rec(long long l, long long r)
{
long long i=lower_bound(avengers.begin(),avengers.end(),l)-avengers.begin();
long long j=upper_bound(avengers.begin(),avengers.end(),r)-avengers.begin();
j--;
// calculating positions of l and r in avengers array to calculate x
long long x=j-i+1;
long long power_consumed;
// if there is no avenger in that subarray
if(x==0)
power_consumed=A;
else
{
power_consumed=B*x*(r-l+1);
// power consumed for operation 2. (r-l+1 is length of subarray)
}
// if l is equal to r or if there is no avenger in the subarray, then do not go into recursion further
if(l==r || x==0)
return power_consumed;
long long mid=(l+r)/2;
// taking minimum of two operations.
long long minPower=min(power_consumed, rec(l,mid)+rec(mid+1,r));
return minPower;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>k>>A>>B;
int i;
for(i=0;i<k;i++)
{
int val;
cin>>val;
avengers.push_back(val);
}
sort(avengers.begin(),avengers.end());
long long x = (long long)1<<n;
cout<<rec(1,x)<<endl;
return 0;
}
Tutorial is loading...
Author's code
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fr(i,n) for(i=0;i<n;i++)
#define rep(i,st,en) for(i=st;i<=en;i++)
typedef long long ll;
typedef pair<int,int> pii;
const int N = 100010;
ll mod = 1e9+7;
ll fmod(ll b,ll exp){
ll res =1;
while(exp){if(exp&1ll)res=(res*b)%mod;
b =(b*b)%mod;exp/=2ll;
}
return res;
}
ll buc[101];
ll fac[N],inv[N];
ll dp[N],temp_dp[N];
ll ans[55][55];
string s,s1,s2;
int find(char c)
{
if(c>='A' && c<='Z')return (int)(c-'A'+26);
else return (c-'a');
}
inline void add(ll &a,ll b)
{
a+=b;
if(a>=mod)a-=mod;
}
inline void sub(ll &a,ll b)
{
a-=b;
if(a<0)a+=mod;
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);cin.tie(NULL);
int t=1,n,i,j,m,q;
cin>>s;
n = s.length();
//Store the frequencies in buc
for(int i=0;i<n;i++)buc[find(s[i])]++;
//Compte factorial and inverse factorials modulo mod
fac[0]=1;
rep(i,1,n)fac[i]=(fac[i-1]*1ll*i)%mod;
inv[n] = fmod(fac[n],mod-2);
for(int i=n-1;i>=0;i--)inv[i]=(inv[i+1]*1ll*(i+1))%mod;
//Compute n/2! * n/2! divided by factorials of frequencies of all
ll num = (fac[n/2]*fac[n/2])%mod;
for(int i=0;i<52;i++)num = (num*inv[buc[i]])%mod;
//DP for subset sum
dp[0]=1;
for(int i=0;i<52;i++)
{
if(!buc[i])continue;
for(int j=n;j>=buc[i];j--)
add(dp[j],dp[j-buc[i]]);
}
fr(i,52)ans[i][i]=dp[n/2];
fr(i,52)
{
if(!buc[i])continue;
//Temporrily store dp using all characters in an array
for(int k=0;k<=n;k++)
temp_dp[k]= dp[k];
//Remove all ways consisting of the ith character from temp array
for(int k=buc[i];k<=n;k++)
sub(temp_dp[k],temp_dp[k-buc[i]]);
for(int j=i+1;j<52;j++)
{
if(!buc[j])continue;
//Now remove the jth character from the temp
for(int k=buc[j];k<=n;k++)
sub(temp_dp[k],temp_dp[k-buc[j]]);
// Answer will be twice since ith and jth can be in 1st or 2nd half
ans[i][j]= (2ll*temp_dp[n/2])%mod;
ans[j][i]= ans[i][j]; //Symmetric
//Restore condition by adding ways using jth to reset temp to without i
for(int k=n;k>=buc[j];k--)
add(temp_dp[k],temp_dp[k-buc[j]]);
}
}
cin>>q;
int x,y;
while(q--)
{
cin>>x>>y;
int l = find(s[x-1]);
int r = find(s[y-1]);
//Use precomputed num and ans, for all queries
cout<<(num*ans[l][r])%mod<<"\n";
}
return 0;
}
Tutorial is loading...
Author's code
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define rep(i,st,en) for(i=st;i<=en;i++)
typedef long long ll;
typedef pair<int,int> pii;
const int N = 200010;
const int LN = 20;
ll mod = 1e9+7;
ll fmod(ll b,ll exp){
ll res =1;
while(exp){if(exp&1ll)res=(res*b)%mod;
b =(b*b)%mod;exp/=2ll;
}
return res;
}
int ver[2*N]; // gives the id of the vertex in the euler array
ll bit[2*N];
int st[N],en[N],A[N]; // st and en are the start and end time of node i
int f[N];
ll dp[N];
int par[N][LN];
int mark[N],lev[N];
vector<int> adj[N];
int tim = 0,n;
/* Normal DFS and LCA functions */
void dfs(int i,int p=-1)
{
if (p+1)
{
lev[i]=lev[p]+1;
par[i][0]=p;
for(int j=1;j<LN;j++)
if (par[i][j-1]+1)
par[i][j]=par[par[i][j-1]][j-1];
}
st[i]=(++tim);
ver[tim]= i;
for(auto ve:adj[i])
if(p-ve)
dfs(ve,i);
en[i]=(++tim);
ver[tim]= i;
}
int lca(int u,int v)
{
if (lev[u]<lev[v])
swap(u,v);
for(int j=LN-1;j>=0;j--)
if (par[u][j]+1 && lev[par[u][j]]>=lev[v])
u=par[u][j];
if (u==v)
return u;
for(int j=LN-1;j>=0;j--)
{
if (par[u][j]+1 && par[u][j]!=par[v][j])
{
u=par[u][j];
v=par[v][j];
}
}
return par[u][0];
}
/* Helper functions */
inline ll add(ll a,ll b)
{
a+=b;
if(a>=mod)a-=mod;
return a;
}
void upd(int ind,int val)
{
for(int i=ind;i<=2*n;i+=(i&-i))
bit[i]+=val;
}
ll query(int ind)
{
ll res = 0;
for(int i=ind;i>0;i-=(i&-i))
res+=bit[i];
return res; // returns the number of marked nodes from 1 to ind inclusive.
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);cin.tie(NULL);
int t=1,i,j,m,q,u,v,w,typ,e,k,x;
memset(par,-1,sizeof(par));
cin>>n>>q;
rep(i,1,n-1)
{
cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1,-1);
int root;
while(q--)
{
cin>>k>>x>>root;
rep(i,1,k){
cin>>A[i];
mark[A[i]]=1; //mark nodes given in the set
upd(st[A[i]],1); // range update by updating at start and end time in bit
upd(en[A[i]]+1,-1);
}
int ans_root = query(st[root]);
rep(i,1,k){
int lp = lca(A[i],root);
//query function is inclusive, hence subtract 1 to remove current node from answer
f[i]= query(st[A[i]])+ans_root-2*query(st[lp])+(mark[lp]==1)-1;
}
//Sorting level-wise using f[i] values
sort(f+1,f+k+1);
rep(i,1,k){
upd(st[A[i]],-1); // reverse the updates to nullify effect
upd(en[A[i]]+1,1);
mark[A[i]]=0; // unmark nodes in the set
}
rep(i,0,x)dp[i]=0;
dp[0]=1;
//If number of groups less than maximum height no way possible
int fl = 0;
rep(i,1,k)if(f[i]>=x)fl=1;
if(fl)cout<<"0\n";
else{
dp[0]=1;
//For every value of f[i], update dp. Note loop for j will be reverse.
rep(i,1,k){
for(int j=min(i,x);j>=0;j--)
{
if(j<=f[i])dp[j]=0; //no way possible since less than height
else dp[j]= add(((dp[j]*1ll*(j-f[i]))%mod),dp[j-1]);
}
}
ll ans = 0;
// AT MOST x in the question
rep(i,1,x){
ans = add(ans,dp[i]);
}
cout<<ans<<"\n";
}
}
return 0;
}