Tutorial
Tutorial is loading...
Solution in C++
#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--)
{
int cnt1,cnt2;
cin>>cnt1>>cnt2;
if(cnt1%2)
{
cout<<"NO"<<endl;
}
else
{
if(cnt2%2==0)
{
cout<<"YES"<<endl;
}
else
{
if(cnt1==0)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
}
}
}
}
}
Solution in Python
t=int(input())
for _ in range(t):
a,b=map(int,input().split())
if a%2==1:
print("NO")
continue
if a==0 and b%2==1:
print("NO")
continue
print("YES")
Tutorial
Tutorial is loading...
Solution in C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
string s;
cin>>s;
int id=0;
while(id<n&&s[id]=='1')
{
id++;
}
if(id==n)
{
if(n==4)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
else
{
if((id-1)*(id-1)==n)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
}
return 0;
}
Solution in Python
for _ in range(int(input())):
n=int(input())
s=input()
i=0
while i<n and s[i]=='1':
i+=1
if i==n:
if n==4:
print("Yes")
else:
print("No")
continue
i-=1
if i*i==n:
print("Yes")
else:
print("No")
2008C - Самый длинный хороший массив
Tutorial
Tutorial is loading...
Solution in C++
#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--)
{
long long a,b;
cin>>a>>b;
b-=a;
long long l=2,r=1000000000;
while(l<r)
{
long long m=(l+r)/2;
if(m*(m-1)/2<=b)
{
l=m+1;
}
else
{
r=m;
}
}
cout<<l-1<<endl;
}
}
Solution in Python
for _ in range(int(input())):
a, b = map(int, input().split())
i = 0
while a + i <= b:
a += i
i += 1
print(i)
Tutorial
Tutorial is loading...
Solution in C++
#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--)
{
long long n;
cin>>n;
long long p[n+1]={0},b[n+1]={0};
int us[n+1]={0};
for(int i=1;i<=n;i++)
{
cin>>p[i];
}
string s;
cin >> s;
for(int i=1;i<=n;i++)
{
if(us[i])continue;
int sz=0;
while(!us[i])
{
us[i]=1;
sz += s[i - 1] == '0';
i=p[i];
}
while(us[i]!=2)
{
b[i]=sz;
us[i]=2;
i=p[i];
}
}
for(int i=1;i<=n;i++)
{
cout<<b[i]<<" ";
}
cout<<endl;
}
}
Solution in Python
t = int(input())
for _ in range(t):
n = int(input())
b = [0] * (n + 1)
us = [0] * (n + 1)
p = [k-1 for k in map(int, input().split())]
s = input()
for i in range(0, n):
if us[i]:
continue
sz = 0
while not us[i]:
us[i] = 1
sz += s[i] == '0'
i = p[i]
while us[i] != 2:
b[i] = sz
us[i] = 2
i = p[i]
print(" ".join(map(str, b[:-1])))
Tutorial
Tutorial is loading...
Solution in C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
string s;
cin>>s;
int res=s.size();
if(n%2==0)
{
vector<int>v[2]={vector<int>(26),vector<int>(26)};
for(int i=0;i<n;i++)
{
v[i%2][s[i]-'a']++;
}
for(int i=0;i<2;i++)
{
int mx=0;
for(int j=0;j<26;j++)
{
mx=max(mx,v[i][j]);
}
res-=mx;
}
cout<<res<<endl;
}
else
{
vector<int>pref[2]={vector<int>(26),vector<int>(26)};
vector<int>suf[2]={vector<int>(26),vector<int>(26)};
for(int i=n-1;i>=0;i--)
{
suf[i%2][s[i]-'a']++;
}
for(int i=0;i<n;i++)
{
suf[i%2][s[i]-'a']--;
int ans=n;
for(int k=0;k<2;k++)
{
int mx=0;
for(int j=0;j<26;j++)
{
mx=max(mx,suf[1-k][j]+pref[k][j]);
}
ans-=mx;
}
res=min(res,ans);
pref[i%2][s[i]-'a']++;
}
cout<<res<<endl;
}
}
}
Solution in Python
t = int(input())
for _ in range(t):
n = int(input())
s = input()
res = len(s)
if n % 2 == 0:
v = [[0] * 26 for _ in range(2)]
for i in range(n):
v[i % 2][ord(s[i]) - ord('a')] += 1
for i in range(2):
mx = max(v[i])
res -= mx
print(res)
else:
pref = [[0] * 26 for _ in range(2)]
suf = [[0] * 26 for _ in range(2)]
for i in range(n - 1, -1, -1):
suf[i % 2][ord(s[i]) - ord('a')] += 1
for i in range(n):
suf[i % 2][ord(s[i]) - ord('a')] -= 1
ans = n
for k in range(2):
mx = 0
for j in range(26):
mx = max(mx, suf[1 - k][j] + pref[k][j])
ans -= mx
res = min(res, ans)
pref[i % 2][ord(s[i]) - ord('a')] += 1
print(res)
Tutorial
Tutorial is loading...
Solution in C++
#include <bits/stdc++.h>
using namespace std;
constexpr int mod=1e9+7;
long long binpow(long long a,long long b)
{
if(b==0)
{
return 1;
}
if(b%2)
{
return (a*binpow(a,b-1))%mod;
}
return binpow((a*a)%mod,b/2);
}
int main(){
int t;
cin>>t;
while(t--)
{
long long n;
cin>>n;
long long a[n],sum=0,sumsq=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
sum+=a[i];sum%=mod;
sumsq+=a[i]*a[i];
sumsq%=mod;
}
sum*=sum;sum%=mod;
sum=(sum-sumsq+mod)%mod;
sum=(sum*binpow(2,mod-2))%mod;
long long cnt=n*(n-1)/2;cnt%=mod;
cout<<(sum%mod)*binpow(cnt,mod-2)%mod<<endl;
}
}
Solution in Python
import sys; input = sys.stdin.readline
for i in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ans = 0
s = 0
mod = int(1e9 + 7)
for i in range(n): s += a[i]
s %= mod
for i in range(n):
s -= a[i]
ans = (ans + a[i] * s) % mod
ans = (ans * pow(n * (n - 1) // 2, mod - 2, mod)) % mod
print(ans)
Tutorial
Tutorial is loading...
Solution in C++
#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--)
{
int n,k;
cin>>n>>k;
long long a[n+1],g=0,mx=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
g=__gcd(g,a[i]);
mx=max(mx,a[i]);
}
if(g==0)
{
cout<<k<<endl;
continue;
}
sort(a,a+n);
int q=-g;
if(n!=1)
{
for(int i=0;i<n;i++)
{
q+=g;
a[i]=q;
}
}
a[n]=1e16;
long long lst=-1;
for(int i=0;i<=n;i++)
{
if(k<=a[i]-lst-1)
{
break;
}
k-=max(a[i]-lst-1,0ll);
lst=a[i];
}
cout<<lst+k<<endl;
}
}
Solution in Python
import math
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
a = list(map(int, input().split()))
g = 0
mx = 0
for i in range(n):
g = math.gcd(g, a[i])
mx = max(mx, a[i])
if g == 0:
print(k)
continue
a.sort()
q = -g
if n != 1:
for i in range(n):
q += g
a[i] = q
a.append(10**16)
lst = -1
for i in range(n + 1):
if k <= a[i] - lst - 1:
break
k -= max(a[i] - lst - 1, 0)
lst = a[i]
print(lst + k)
Tutorial
Tutorial is loading...
Solution in C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t=1;
cin>>t;
for(int i=1;i<=t;i++)
{
int n,m;
cin>>n>>m;
vector<int>a(n);
vector<int>c(n+1,0ll);
for(int i=0;i<n;i++)
{
cin>>a[i];
c[a[i]]++;
}
for(int i=1;i<=n;i++)
{
c[i]+=c[i-1];
}
int res[n+1]={0};
for(int x=1;x<=n;x++)
{
int l=0,r=x;
while(l<r)
{
int mid=(l+r)/2;
int cnt=c[mid];
for(int k=1;k*x<=n;k++)
{
cnt+=c[min(k*x+mid,n)]-c[k*x-1];
}
if(cnt-1>=n/2)
{
r=mid;
}
else
{
l=mid+1;
}
}
res[x]=l;
}
while(m--)
{
int x;
cin>>x;
cout<<res[x]<<" ";
}
cout<<endl;
}
}
Solution in Python
for _ in range(int(input())):
n, m = map(int, input().split())
a = list(map(int, input().split()))
c = [0] * (n + 1)
for i in range(n):
c[a[i]] += 1
for i in range(1, n + 1):
c[i] += c[i - 1]
res = [0] * (n + 1)
for x in range(1, n + 1):
l, r = 0, x
while l < r:
mid = (l + r) // 2
cnt = c[mid]
for k in range(1, n // x + 1):
cnt += c[min(k * x + mid, n)] - c[k * x - 1]
if cnt - 1 >= n // 2:
r = mid
else:
l = mid + 1
res[x] = l
for _ in range(m):
x = int(input())
print(res[x])