Tutorial is loading...
Set by : vntshh
Setter's solution
#include<bits/stdc++.h>
#define rep(i,start,lim) for(lld i=start;i<lim;i++)
#define repd(i,start,lim) for(lld i=start;i>=lim;i--)
#define scan(x) scanf("%lld",&x)
#define print(x) printf("%lld ",x)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define br printf("\n")
#define sz(a) lld((a).size())
#define YES printf("YES\n")
#define NO printf("NO\n")
#define all(c) (c).begin(),(c).end()
#define INF 1011111111
#define LLINF 1000111000111000111LL
#define EPS (double)1e-10
#define MOD 1000000007
#define PI 3.14159265358979323
using namespace std;
typedef long double ldb;
typedef long long lld;
lld powm(lld base,lld exp,lld mod=MOD) {lld ans=1;while(exp){if(exp&1) ans=(ans*base)%mod;exp>>=1,base=(base*base)%mod;}return ans;}
lld ctl(char x,char an='a') {return (lld)(x-an);}
char ltc(lld x,char an='a') {return (char)(x+an);}
#define bit(x,j) ((x>>j)&1)
typedef vector<lld> vlld;
typedef pair<lld,lld> plld;
typedef map<lld,lld> mlld;
typedef set<lld> slld;
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mxm(a,b) a = max(a,b)
#define mnm(a,b) a = min(a,b)
#define endl '\n'
#define fre freopen("1.in","r",stdin); freopen("1.out","w",stdout);
#define N 1000005
int main()
{
//sync;
string s;
map<char,lld> m;
cin>>s;
lld k = sz(s);
string tmp = "";
tmp += s[0];
rep(i,1,k) if(s[i]!=s[i-1]) tmp+=s[i];
rep(i,0,k) m[s[i]]++;
if(tmp != "abc") return 0*NO;
if(m['c']!=m['a'] and m['c']!=m['b']) return 0*NO;
if(m['a']>=1 and m['b']>=1) return 0*YES;
else return 0*NO;
return 0;
}
Tutorial is loading...
Set by : AakashHanda
Setter's solution
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
priority_queue<ll> pq;
int main(){
int n, k1, k2, k;
cin>>n>>k1>>k2;
k = k1+k2;
vector<ll> a(n), b(n), arr(n);
for(int i=0 ; i<n ; ++i)
cin>>a[i];
for(int i=0 ; i<n ; ++i){
cin>>b[i];
arr[i] = abs(a[i]-b[i]);
pq.push(arr[i]);
}
while(k>0){
ll curr = pq.top();
pq.pop();
pq.push(abs(curr-1));
k--;
}
ll ans = 0;
while(!pq.empty()){
ll curr = pq.top();
ans += (curr*curr);
pq.pop();
}
cout<<ans;
}
Tutorial is loading...
Set by : 7dan
Setter's solution
#include<bits/stdc++.h>
#define rep(i,start,lim) for(lld i=start;i<lim;i++)
#define repd(i,start,lim) for(lld i=start;i>=lim;i--)
#define scan(x) scanf("%lld",&x)
#define print(x) printf("%lld ",x)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define br printf("\n")
#define sz(a) lld((a).size())
#define YES printf("YES\n")
#define NO printf("NO\n")
#define all(c) (c).begin(),(c).end()
#define INF 1011111111
#define LLINF 1000111000111000111LL
#define EPS (double)1e-10
#define MOD 1000000007
#define PI 3.14159265358979323
using namespace std;
typedef long double ldb;
typedef long long lld;
lld powm(lld base,lld exp,lld mod=MOD) {lld ans=1;while(exp){if(exp&1) ans=(ans*base)%mod;exp>>=1,base=(base*base)%mod;}return ans;}
lld ctl(char x,char an='a') {return (lld)(x-an);}
char ltc(lld x,char an='a') {return (char)(x+an);}
#define bit(x,j) ((x>>j)&1)
typedef vector<lld> vlld;
typedef pair<lld,lld> plld;
typedef map<lld,lld> mlld;
typedef set<lld> slld;
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mxm(a,b) a = max(a,b)
#define mnm(a,b) a = min(a,b)
#define endl '\n'
#define fre freopen("1.in","r",stdin); freopen("1.out","w",stdout);
#define N 1000005
int main()
{
//sync;
lld x,d;
vector<lld> ans;
cin>>x>>d;
lld num = 1, cnt = 0;
repd(i,60,1) if(bit(x,i)) {
rep(j,0,i) ans.pb(num);
cnt++;
num+=(d+1);
}
while(cnt--) ans.pb(num),num+=(d+1);
if(x%2) ans.pb(num);
cout<<sz(ans)<<endl;
for(auto i:ans) cout<<i<<" ";
return 0;
}
Tutorial is loading...
Set by : Vicennial
Setter's solution
//Gvs Akhil (Vicennial)
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define ld(a) while(a--)
#define tci(v,i) for(auto i=v.begin();i!=v.end();i++)
#define tcf(v,i) for(auto i : v)
#define all(v) v.begin(),v.end()
#define rep(i,start,lim) for(long long (i)=(start);i<(lim);i++)
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define osit ostream_iterator
#define INF 0x3f3f3f3f
#define LLINF 1000111000111000111LL
#define PI 3.14159265358979323
#define endl '\n'
#define trace1(x) cerr<<#x<<": "<<x<<endl
#define trace2(x, y) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z) cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
const int N=1000006;
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
typedef vector<long long> vll;
typedef vector<vll> vvll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef tuple<int,int,int> iii;
typedef set<int> si;
typedef complex<double> pnt;
typedef vector<pnt> vpnt;
typedef priority_queue<ii,vii,greater<ii> > spq;
const ll MOD=1000000007LL;
template<typename T> T gcd(T a,T b){if(a==0) return b; return gcd(b%a,a);}
template<typename T> T power(T x,T y,ll m=MOD){T ans=1;while(y>0){if(y&1LL) ans=(ans*x)%m;y>>=1LL;x=(x*x)%m;}return ans%m;}
int shift[66];
inline int getlev(int x){
int z=__builtin_clzll(x);
return 64-z;
}
inline int getmod(int x){
return (1LL<<(x-1));
}
void addshift(int x,int k){
int m=getmod(x);
if(k>=m) k%=m;
shift[x]+=k;
if(shift[x]>=m) shift[x]-=m;
if(shift[x]<0) shift[x]+=m;
}
int getorig(int V,int l){
int fval=(1LL<<(l-1)); // 2^(L-1)
int mod=getmod(l);
int P= V-fval;
int Z= fval+((P-shift[l])%mod + mod)%mod;
return Z;
}
int32_t main(){
sync;
int q; cin>>q;
while(q--){
int t,x,k;
cin>>t>>x;
int l=getlev(x);
if(t==1){
cin>>k; k%=getmod(l);
addshift(l,k);
}
else if(t==2){// propogating
cin>>k;
k%=getmod(l);
if(k<0) k+=getmod(l);
int curr=1;
for(int i=l;i<=60;i++){
addshift(i,k*curr);
curr<<=1;
}
}
else{ // printing
int P= x - (1LL<<(l-1)); // Finding index in array
int V= (1LL<<(l-1)) + ((P+shift[l])%(1LL<<(l-1)) + (1LL<<(l-1)) )%(1ll<<(l-1)); //Finding original value at the new index of X after performing the shift.
for(int i=l;i>=1;i--){
int Z = getorig(V,i); // Finding the current value
cout<<Z<<" "; // printing the current value
V>>=1; // dividing the original value by 2 to get its parent value
}
cout<<endl;
}
}
}
Tutorial is loading...
Set by : rohitranjan017
Setter's solution
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=1000005,mod=1e9+7;
vector<int> adj[M];
int par[M],lev[M];
ll v[M];
ll e[M],o[M];
ll ans,res,n;
ll powmod(ll base, ll exponent)
{
//cout<<base<<" "<<exponent<<endl;
ll ans=1;
while(exponent)
{
while(exponent%2==0)
{
base=(base*base)%mod;
exponent/=2;
}
exponent--;
ans=(ans*base)%mod;
}
return ans;
}
void dfs(int x,int depth)
{
//o[x]++;
lev[x]=depth;
int cno=0;
for(int i=0;i<adj[x].size();i++)
{
int c=adj[x][i];
if(c!=par[x])
{
par[c]=x;
dfs(c,depth+1);
if(cno>0)
{
ans=( (ans+ 2*( o[x]*e[c]%mod - e[x]*o[c]%mod + mod)%mod*v[x]%mod)+mod )%mod;
}
cno++;
o[x]+=e[c];
e[x]+=o[c];
}
}
o[x]++;
}
int main()
{
//freopen("o.out","r",stdin);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>v[i];
}
for(int i=1;i<n;i++)
{
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1,0);
//cout<<ans<<endl;
for(int i=1;i<=n;i++)
{
//cout<<"###"<<i<<endl;
if(lev[i]%2)
{
ans=((ans+2*(o[i]*(e[1]-o[i]+1)%mod - e[i]*(o[1]-e[i])%mod +mod)%mod*v[i]%mod)%mod+mod )%mod;
}
else
{
ans=((ans+2*(o[i]*(o[1]-o[i]+1)%mod - e[i]*(e[1]-e[i])%mod +mod)%mod*v[i]%mod)%mod+mod )%mod;
}
//cout<<ans<<endl;
ans=((ans-v[i])%mod+mod)%mod;
// cout<<i<<" "<<ans<<endl;
//cout<<ans<<endl;
}
cout<<ans<<endl;
}
Tutorial is loading...
Set by : kr_abhinav
Setter's solution
#include <bits/stdc++.h>
using namespace std;
vector<map<int,int> > s;
int getedgelen(int a, int w)
{
auto it = s[a].lower_bound(w);
if(it == s[a].begin()) return 1;
it--;
return (it->second)+1;
}
int32_t main() {
ios::sync_with_stdio(false);cin.tie(0);
int n,m,a,b,w;
cin>>n>>m;
s.resize(n+1);
int ans = 0;
while(m--)
{
cin>>a>>b>>w;
int val = getedgelen(a,w);
if(getedgelen(b,w+1) > val)
continue;
s[b][w] = max(s[b][w],val);
auto it = s[b].upper_bound(w);
while(!(it==s[b].end() || it->second > val))
{
it = s[b].erase(it);
}
ans = max(ans,val);
}
cout<<ans;
return 0;
}
Tutorial is loading...
Set by : apoorv_kulsh
Setter's solution
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
const int mod = 998244353;
const int root = 15311432;
const int root_1 = 469870224;
const int root_pw = 1 << 23;
const int N = 400004;
vector<int> v[N];
ll modInv(ll a, ll mod = mod){
ll x0 = 0, x1 = 1, r0 = mod, r1 = a;
while(r1){
ll q = r0 / r1;
x0 -= q * x1; swap(x0, x1);
r0 -= q * r1; swap(r0, r1);
}
return x0 < 0 ? x0 + mod : x0;
}
void fft (vector<int> &a, bool inv) {
int n = (int) a.size();
for (int i=1, j=0; i<n; ++i) {
int bit = n >> 1;
for (; j>=bit; bit>>=1)
j -= bit;
j += bit;
if (i < j)
swap (a[i], a[j]);
}
for (int len=2; len<=n; len<<=1) {
int wlen = inv ? root_1 : root;
for (int i=len; i<root_pw; i<<=1)
wlen = int (wlen * 1ll * wlen % mod);
for (int i=0; i<n; i+=len) {
int w = 1;
for (int j=0; j<len/2; ++j) {
int u = a[i+j], v = int (a[i+j+len/2] * 1ll * w % mod);
a[i+j] = u+v < mod ? u+v : u+v-mod;
a[i+j+len/2] = u-v >= 0 ? u-v : u-v+mod;
w = int (w * 1ll * wlen % mod);
}
}
}
if(inv) {
int nrev = modInv(n, mod);
for (int i=0; i<n; ++i)
a[i] = int (a[i] * 1ll * nrev % mod);
}
}
void pro(const vector<int> &a, const vector<int> &b, vector<int> &res) {
vector<int> fa(a.begin(), a.end()), fb(b.begin(), b.end());
int n = 1;
while (n < (int) max(a.size(), b.size())) n <<= 1;
n <<= 1;
fa.resize (n), fb.resize (n);
fft(fa, false), fft (fb, false);
for (int i = 0; i < n; ++i)
fa[i] = 1LL * fa[i] * fb[i] % mod;
fft (fa, true);
res = fa;
}
int S(int n, int r) {
int nn = 1;
while(nn < n) nn <<= 1;
for(int i = 0; i < n; ++i) {
v[i].push_back(i);
v[i].push_back(1);
}
for(int i = n; i < nn; ++i) {
v[i].push_back(1);
}
for(int j = nn; j > 1; j >>= 1){
int hn = j >> 1;
for(int i = 0; i < hn; ++i){
pro(v[i], v[i + hn], v[i]);
}
}
return v[0][r];
}
int fac[N], ifac[N], inv[N];
void prencr(){
fac[0] = ifac[0] = inv[1] = 1;
for(int i = 2; i < N; ++i)
inv[i] = mod - 1LL * (mod / i) * inv[mod % i] % mod;
for(int i = 1; i < N; ++i){
fac[i] = 1LL * i * fac[i - 1] % mod;
ifac[i] = 1LL * inv[i] * ifac[i - 1] % mod;
}
}
int C(int n, int r){
return (r >= 0 && n >= r) ? (1LL * fac[n] * ifac[n - r] % mod * ifac[r] % mod) : 0;
}
int main(){
prencr();
int n, p, q;
cin >> n >> p >> q;
ll ans = C(p + q - 2, p - 1);
ans *= S(n - 1, p + q - 2);
ans %= mod;
cout << ans;
}
Tutorial is loading...
Set by : arnabsamanta
Setter's solution
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
const int N = 400004;
int n;
vector<int> e[N];
int sz[N], par[N], sp[30][N];
int V[N], L[N], CS[N];
bool mark[N];
struct tri {
long long A, B;
int C;
};
map<int, tri> nmp[N];
long long D[N]; // Sum of Distances
long long S[N]; // Sum of Depth
int T[N]; // Count
//======================================PRE-PROCESS=========================================
void getLevel(int u, int p = -1){
sp[0][u] = p;
for(auto x : e[u]) if(x != p) {
L[x] = 1 + L[u];
getLevel(x, u);
}
}
void prepAncestor(){
for(int i = 1; (1 << i) <= n; ++i) {
for(int j = 1; j <= n; ++j) {
sp[i][j] = sp[i - 1][sp[i - 1][j]];
}
}
}
int dist(int u, int v){
if(L[u] < L[v]) swap(u, v);
int lgn = 0;
while((1 << lgn) < n) lgn++;
int ans = 0;
for(int i = lgn; i >= 0; --i){
if(L[u] - (1 << i) >= L[v]){
u = sp[i][u];
ans += (1 << i);
}
}
if(u == v) return ans;
for(int i = lgn; i >= 0; --i){
if(sp[i][u] != sp[i][v]){
ans += 1 << (i + 1);
u = sp[i][u];
v = sp[i][v];
}
}
return ans + 2;
}
//=================================CENTROID DECOMPOSITION===================================
void getSize(int u, int p = -1) {
sz[u] = 1;
for(auto v : e[u]) if(v != p && !mark[v]) {
getSize(v, u);
sz[u] += sz[v];
}
}
int getCentroid(int u, int p, int lim) {
for(auto v : e[u]) if(v != p && !mark[v] && sz[v] > lim) {
return getCentroid(v, u, lim);
}
return u;
}
void decompose(int u, int p = -1) {
getSize(u);
int cen = getCentroid(u, -1, sz[u] / 2);
par[cen] = p == -1 ? cen : p;
mark[cen] = true;
for(auto v : e[cen]) if(v != p && !mark[v]) {
decompose(v, cen);
}
}
//==========================================QUERY-HANDLER===============================================
void updAns(int u, int add) {
int Z = V[u];
tri tY = nmp[u][Z];
ll ret = tY.A;
int y = u, x = par[u];
while(y != x) {
tri tX = nmp[x][Z];
ret += 1LL * (tX.C - tY.C) * dist(x, u) + (tX.A - tY.B);
y = x;
tY = tX;
x = par[x];
}
ret <<= 1;
int mul = add ? 1 : -1;
D[Z] += mul * ret;
S[Z] += mul * L[u];
T[Z] += mul;
}
void updTree(int u, bool add) {
int Z = V[u];
int mul = add ? 1 : -1;
for(int x = u; ; x = par[x]) {
tri &tX = nmp[x][Z];
tX.C += mul;
if(!tX.C) {
nmp[x].erase(Z);
} else {
tX.A += mul * dist(u, x);
tX.B += mul * dist(u, par[x]);
}
if(x == par[x]) break;
}
}
void upd(int u, bool add) {
if(add) {
updTree(u, add);
updAns(u, add);
} else {
updAns(u, add);
updTree(u, add);
}
}
//=========================================MAIN============================================
int main(){
int m, q, ac;
scanf("%d %d %d %d", &n, &m, &q, &ac);
for(int i = 1; i <= n; ++i) {
scanf("%d", &V[i]);
}
for(int i = 2; i <= n; ++i) {
int v;
scanf("%d", &v);
e[v].pb(i);
e[i].pb(v);
}
for(int i = 1; i <= m; ++i) {
scanf("%d", &CS[i]);
}
L[1] = 1;
getLevel(1);
prepAncestor();
decompose(1);
for(int i = 1; i <= n; ++i) {
upd(i, true);
}
while(q--) {
int t;
scanf("%d", &t);
if(t == 1) {
int u, v;
scanf("%d %d", &u, &v);
upd(u, false);
V[u] = v;
upd(u, true);
}
else if(t == 2) {
int v;
scanf("%d", &v);
long double ans = S[v] * T[v] - D[v] / 2;
ans *= 1LL * CS[v] * CS[v];
ans += 1LL * n * ac * ac;
long long anst = 2LL * CS[v] * ac * S[v];
ans -= anst;
cout << fixed << setprecision(12) << ans / n << "\n";
}
}
}
Do give your feedback here : https://goo.gl/forms/xbsdMxnkA3XsG4092. Would love to hear your feedback, since that would help us get better!