A. Maxim and Biology

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx = 1e2 + 10;
char s[mx];
int main(){
int n,ans = 1e9;
scanf("%d%s",&n,s);
for(int i=0;i<=n-4;i++){
int a = abs(s[i]-'A');
a = min(a,26-a);
int b = abs(s[i+1]-'C');
b = min(b,26-b);
int c = abs(s[i+2]-'T');
c = min(c,26-c);
int d = abs(s[i+3]-'G');
d = min(d,26-d);
ans = min(ans,a+b+c+d);
}
printf("%d\n",ans);
return 0;
}

B. Dima and a Bad XOR

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx = 5e2 + 10;
int n,m,a[mx][mx],ans[mx];
bool vis[mx];
int find(int x,int d,bool f){
for(int i=1;i<=m;i++){
bool ok = a[x][i]&(1<<d);
if(f==ok) return i;
}
}
void solve(){
for(int i=0;i<10;i++){
int d = 0;bool w = 0;
memset(vis,0,sizeof(vis));
for(int j=1;j<=n;j++){
int c = 0;
for(int k=1;k<=m;k++){
if(a[j][k]&(1<<i))
c++;
}
if(c==m) d++;
if(c&&c!=m) w = vis[j] = 1;
}
if((d&1)||w){
puts("TAK");
bool f = !(d&1);
for(int j=1;j<=n;j++){
if(!vis[j]) printf("1 ");
else printf("%d ",find(j,i,f)),f=0;
}
return;
}
}
puts("NIE");
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++)
scanf("%d",a[i]+j);
}
solve();
return 0;
}

C. Problem for Nazar

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx = 5e2 + 10;
const int mod = 1e9 + 7;
ll l,r,siz[2];
bool vis[mx];
void add(ll &ans,ll len,ll c,int b){
b ^= 1;
len %=mod,c %= mod;
ans += ((2*c-(1<<b))*len%mod+len*len%mod);
ans %= mod;
}
int main(){
scanf("%lld%lld",&l,&r);
int bit = 0;
ll p = 0,c,len,ans = 0;
while(p+(1ll<<bit)<l){
p += (1ll<<bit);
siz[bit&1] += (1ll<<bit);
bit++;
}
if(p+(1ll<<bit)>=r){
len = (r-l+1);
c = (siz[bit&1]+l-p);
printf("%lld\n",ans%mod);
return 0;
}
len = (1ll<<bit)-(l-p)+1;
c = (siz[bit&1]+l-p);
p += (1ll<<bit),siz[bit&1] += (1ll<<bit),bit++;
while(p+(1ll<<bit)<r){
p += (1ll<<bit);
siz[bit&1] += (1ll<<bit);
bit++;
}
len = r - p;c = siz[bit&1] + 1;
printf("%lld\n",ans);
return 0;
}

D. Stas and the Queue at the Buffet

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx = 1e5 + 10;
const int mod = 1e9 + 7;
int n,c[mx];
int main(){
scanf("%d",&n);
ll ans = 0;
int a,b;
for(int i=1;i<=n;i++){
scanf("%d%d",&a,&b);
ans += 1ll*b*n - a;
c[i] = a - b;
}
sort(c+1,c+1+n);
for(int i=1;i<=n;i++) ans += 1ll*c[i]*(n-i+1);
printf("%lld\n",ans);
return 0;
}

E. Number of Components

1.i-1和i+1被留下，对于结果-> +1-2 = -1；

2.i-1被留下，对于结果 +1-1 = 0

3.i+1被留下，对于结果 +1-1 = 0

3.i+1和i-1都没有被留下，对于结果 +1。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx = 1e5 + 10;
const int mod = 1e9 + 7;
int n,a[mx];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",a+i);
}
int c = 1;
for(int i=2;i<=n;i++){
if(a[i]!=a[c]) a[++c] = a[i];
}
a[c+1] = a[0] = n+1;
ll ans = 0;
for(int i=1;i<=c;i++){
ans += 1ll*a[i]*(n-a[i]+1);
if(a[i-1]<a[i]){
ans -= 1ll*a[i-1]*(n-a[i]+1);
}
if(a[i+1]<a[i]){
ans -= 1ll*a[i+1]*(n-a[i]+1);
}
}
printf("%lld\n",ans);
return 0;
}

F. Sonya and Informatics

,

,

*log(K))

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx = 1e2 + 10;
const int mod = 1e9 + 7;
const int N = 50;
int n,a[mx],K;
struct node
{
int mat[55][55];
}base;
node matr(node &p1,node &p2)
{
node ret;
memset(&ret,0,sizeof(node));
for(int k=0;k<=N;k++){
for(int i=0;i<=N;i++){
if(p1.mat[i][k]);
for(int j=0;j<=N;j++){
ret.mat[i][j] += 1ll*p1.mat[i][k]*p2.mat[k][j]%mod;
ret.mat[i][j] %= mod;
}
}
}
return ret;
}
node mpow(int y,int p){
node ans;
memset(&ans,0,sizeof(node));
ans.mat[0][p] = 1;
while(y){
if(y&1) ans = matr(ans,base);
base = matr(base,base);
y >>= 1;
}
return ans;
}
ll qpow(ll x,ll y){
ll ans = 1;
while(y){
if(y&1) ans = ans*x%mod;
y >>= 1;
x = x*x%mod;
}
return ans;
}
int main(){
scanf("%d%d",&n,&K);
int c = 0,d = 0,b = 0;
for(int i=1;i<=n;i++){
scanf("%d",a+i);
if(a[i]) d++; else c++;
}
for(int i=1;i<=c;i++) if(a[i]) b++;
base.mat[0][0] = c*(c-1) / 2 + d*(d-1) / 2;
base.mat[1][0] = 1;
for(int i=1;i<=min(c,d);i++){
base.mat[i-1][i] = (c-i+1)*(d-i+1);
base.mat[i][i] = c*(c-1) / 2 + d*(d-1) / 2;
base.mat[i][i] += (c-i)*i + (d-i)*i;
base.mat[i+1][i] = (i+1)*(i+1);
}
ll ans = mpow(K,b).mat[0][0];
printf("%lld\n",ans*qpow(qpow(n*(n-1)/2,K),mod-2)%mod);
return 0;
}