solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
enum Type{
Zero,
One,
Tate,
Yoko,
Naname,
Three,
Four,
};
int H,N;
int ans;
int loc_sum[3][2][2];
bool table[20][2][2],fall[3][2][2][2];
Type type[3];
void copyTable(bool to[20][2][2],bool from[20][2][2]){
for(int h = 0; h < 20; h++){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++)to[h][row][col] = from[h][row][col];
}
}
}
void addBlock(bool to[20][2][2],bool add[2][2][2],int base_h){
for(int h = 0; h < 2; h++){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++){
if(add[h][row][col]){
to[base_h+h][row][col] = add[h][row][col];
}
}
}
}
}
int deleteBlock(bool tmp_table[20][2][2],int base_h){
int ret = 0;
int current_h = base_h,tmp_count;
for(int i = 0; i < 2; i++){
tmp_count = 0;
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++){
if(tmp_table[current_h][row][col])tmp_count++;
}
}
if(tmp_count == 4){ //1段下へ平行シフトする
ret++;
for(int h = current_h+1; h < 20; h++){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++){
tmp_table[h-1][row][col] = tmp_table[h][row][col];
tmp_table[h][row][col] = false;
}
}
}
//current_hはそのまま
}else{
current_h++; //次の高さへ
}
}
return ret;
}
void recursive(int fall_id,int num_delete,bool tmp_table[20][2][2]){
if(fall_id == N){
ans = max(ans,num_delete);
return;
}
bool work[2][2][2];
bool next_tmp_table[20][2][2];
int base_h; //落下ブロックの底面となる高さ
bool FLG;
int next_delete;
//fall_id番目のブロックの落下計算
switch(type[fall_id]){
case Zero:
recursive(fall_id+1,num_delete,tmp_table);
break;
case One:
//4箇所平行移動する
for(int to_row = 0; to_row < 2; to_row++){
for(int to_col = 0; to_col < 2; to_col++){
for(int h = 0; h < 2; h++){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++){
work[h][row][col] = false;
}
}
}
for(int h = 0; h < 2; h++){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++){
if(fall[fall_id][h][row][col]){
work[h][to_row][to_col] = true; //平行移動
}
}
}
}
copyTable(next_tmp_table,tmp_table);
//落下処理
FLG = false;
for(int h = 19; h >= 0; h--){ //落下ブロックの底面となる高さのループ
if(h > 0){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++){
if((work[0][row][col] == true && next_tmp_table[h-1][row][col] == true) ||
(work[1][row][col] == true && next_tmp_table[h][row][col] == true)){ //底面がブロックに接した場合
FLG = true;
break;
}
}
if(FLG)break;
}
if(FLG){
//全体に落下ブロックを書き足す
base_h = h;
addBlock(next_tmp_table,work,base_h);
break;
}
}else{ //h == 0
base_h = 0;
addBlock(next_tmp_table,work,base_h);
break;
}
}
//消滅&落下処理
next_delete = num_delete;
next_delete += deleteBlock(next_tmp_table,base_h);
//次へ
recursive(fall_id+1,next_delete,next_tmp_table);
}
}
break;
case Tate:
//2箇所平行移動
for(int to_col = 0; to_col < 2; to_col++){
for(int h = 0; h < 2; h++){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++){
work[h][row][col] = false;
}
}
}
for(int h = 0; h < 2; h++){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++){
if(fall[fall_id][h][row][col]){
work[h][row][to_col] = true; //平行移動
}
}
}
}
copyTable(next_tmp_table,tmp_table);
//落下処理
FLG = false;
for(int h = 19; h >= 0; h--){ //落下ブロックの底面となる高さのループ
if(h > 0){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++){
if((work[0][row][col] == true && next_tmp_table[h-1][row][col] == true) ||
(work[1][row][col] == true && next_tmp_table[h][row][col] == true)){ //底面がブロックに接した場合
FLG = true;
break;
}
}
if(FLG)break;
}
if(FLG){
//全体に落下ブロックを書き足す
base_h = h;
addBlock(next_tmp_table,work,base_h);
break;
}
}else{ //h == 0
base_h = 0;
addBlock(next_tmp_table,work,base_h);
break;
}
}
//消滅&落下処理
next_delete = num_delete;
next_delete += deleteBlock(next_tmp_table,base_h);
//次へ
recursive(fall_id+1,next_delete,next_tmp_table);
}
break;
case Yoko:
//2箇所平行移動
for(int to_row = 0; to_row < 2; to_row++){
for(int h = 0; h < 2; h++){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++){
work[h][row][col] = false;
}
}
}
for(int h = 0; h < 2; h++){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++){
if(fall[fall_id][h][row][col]){
work[h][to_row][col] = true; //平行移動
}
}
}
}
copyTable(next_tmp_table,tmp_table);
//落下処理
FLG = false;
for(int h = 19; h >= 0; h--){ //落下ブロックの底面となる高さのループ
if(h > 0){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++){
if((work[0][row][col] == true && next_tmp_table[h-1][row][col] == true) ||
(work[1][row][col] == true && next_tmp_table[h][row][col] == true)){ //底面がブロックに接した場合
FLG = true;
break;
}
}
if(FLG)break;
}
if(FLG){
//全体に落下ブロックを書き足す
base_h = h;
addBlock(next_tmp_table,work,base_h);
break;
}
}else{ //h == 0
base_h = 0;
addBlock(next_tmp_table,work,base_h);
break;
}
}
//消滅&落下処理
next_delete = num_delete;
next_delete += deleteBlock(next_tmp_table,base_h);
//次へ
recursive(fall_id+1,next_delete,next_tmp_table);
}
break;
case Naname:
case Three:
case Four:
copyTable(next_tmp_table,tmp_table);
//落下処理
FLG = false;
for(int h = 19; h >= 0; h--){ //落下ブロックの底面となる高さのループ
if(h > 0){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++){
if((fall[fall_id][0][row][col] == true && next_tmp_table[h-1][row][col] == true) ||
(fall[fall_id][1][row][col] == true && next_tmp_table[h][row][col] == true)){ //底面がブロックに接した場合
FLG = true;
break;
}
}
if(FLG)break;
}
if(FLG){
//全体に落下ブロックを書き足す
base_h = h;
addBlock(next_tmp_table,fall[fall_id],base_h);
break;
}
}else{ //h == 0
base_h = 0;
addBlock(next_tmp_table,fall[fall_id],base_h);
break;
}
}
//消滅&落下処理
next_delete = num_delete;
next_delete += deleteBlock(next_tmp_table,base_h);
//次へ
recursive(fall_id+1,next_delete,next_tmp_table);
break;
}
}
void func(){
for(int h = 0; h < 20; h++){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++)table[h][row][col] = false;
}
}
char buf[3];
//初期データの取得
for(int h = 0; h < H; h++){
for(int row = 1; row >= 0; row--){
scanf("%s",buf);
for(int col = 0; col < 2; col++){
if(buf[col] == '#'){
table[h][row][col] = true;
}
}
}
}
for(int id = 0; id < 3; id++){
for(int h = 0; h < 2; h++){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++)fall[id][h][row][col] = false;
}
}
}
for(int id = 0; id < 3; id++){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++){
loc_sum[id][row][col] = 0; //鉛直方向から見下ろした際の、ブロックのある場所の数
}
}
}
int num,bottom_count;
for(int id = 0; id < N; id++){ //塊のループ
bottom_count = 0; //高さ0のブロックの数
for(int h = 0; h < 2; h++){
for(int row = 1; row >= 0; row--){
scanf("%s",buf);
for(int col = 0; col < 2; col++){
if(buf[col] == '#'){
fall[id][h][row][col] = true;
loc_sum[id][row][col]++;
if(h == 0)bottom_count++;
}
}
}
}
if(bottom_count == 0){ //高さ0にブロックがない場合は、下に平行移動させておく
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++){
fall[id][0][row][col] = fall[id][1][row][col];
fall[id][1][row][col] = false;
}
}
}
//ブロック配置のタイプを決定する
num = 0;
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++){
if(loc_sum[id][row][col] > 0)num++;
}
}
switch(num){
case 0:
type[id] = Zero;
break;
case 1:
type[id] = One;
break;
case 2:
if((loc_sum[id][0][0] > 0 && loc_sum[id][0][1] > 0) || (loc_sum[id][1][0] > 0 && loc_sum[id][1][1] > 0)){
type[id] = Yoko;
}else if((loc_sum[id][0][0] > 0 && loc_sum[id][1][0] > 0) || (loc_sum[id][0][1] > 0 && loc_sum[id][1][1] > 0)){
type[id] = Tate;
}else{
type[id] = Naname;
}
break;
case 3:
type[id] = Three;
break;
case 4:
type[id] = Four;
break;
}
}
ans = 0;
bool first_tmp[20][2][2];
for(int h = 0; h < 20; h++){
for(int row = 0; row < 2; row++){
for(int col = 0; col < 2; col++)first_tmp[h][row][col] = table[h][row][col];
}
}
recursive(0,0,first_tmp);
printf("%d\n",ans);
}
int main(){
while(true){
scanf("%d %d",&H,&N);
if(H == 0 && N == 0)break;
func();
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double pi = acos(-1.0);
const long long oo = 1LL << 60;
const int N = 310;
double res;
double p[N], dp[N][N][N];
int n, a[N];
int main(void) {
int l, k, i, j;
scanf("%d%d%d", &n, &l, &k);
for (int i = 1; i <= n; i++) scanf("%lf", p + i), p[i] /= 100.0;
for (int i = 1; i <= n; i++) scanf("%d", a + i), a[i]++;
if (k < n)
dp[0][0][k] = 1;
else
dp[0][0][n] = 1;
for (i = 0; i < n; i++)
for (j = 0; j <= i; j++)
for (k = 0; k <= n; k++) {
dp[i + 1][j + 1][min(n, k + a[i + 1])] += dp[i][j][k] * p[i + 1];
dp[i + 1][j][k] += (1 - p[i + 1]) * dp[i][j][k];
}
for (int i = l; i <= n; i++)
for (int j = i; j <= n; j++) res += dp[n][i][j];
printf("%.10lf\n", res);
return 0;
}
| 2 |
#include <bits/stdc++.h>
int cnt = 0;
int ans[1007], a[1007];
int n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = n; i; i--) a[i] -= a[i - 1];
for (int i = 1; i <= n; i++) {
bool flag = true;
for (int j = i + 1; j <= n; j++)
if (a[j] != a[j - i]) flag = false;
if (flag) ans[++cnt] = i;
}
printf("%d\n", cnt);
for (int i = 1; i <= cnt; i++) printf("%d ", ans[i]);
printf("\n");
return 0;
}
| 2 |
#include "bits/stdc++.h"
using namespace std;
const int N=2e5+20;
int n,a[N];
long long ans;
map <int,int> m;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
ans+=m[a[i]-i];
m[-a[i]-i]++;
}
cout<<ans;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
double rd_() {
string s;
cin >> s;
int k = s.size();
int p = find(begin(s), end(s), '.') - begin(s);
if (p == k) {
long long x = 0;
for (int i = 0; i < k; i++) {
x = 10 * x + s[i] - 48;
}
return x;
} else {
long long x = 0, f = 0, g = 1;
for (int i = 0; i < p; i++) {
x = 10 * x + s[i] - 48;
}
for (int i = p + 1; i < k; i++) {
f = 10 * f + s[i] - 48;
g = 10 * g;
}
return double(x * g + f) / g;
}
}
double rd() {
double x;
scanf("%lf", &x);
return x;
}
struct pt {
double x, y;
};
struct hcirc {
double x, y, r;
};
int n;
vector<pt> a[1000005];
double povrs[1000005];
hcirc hc[1000005];
inline double sq(double x) { return x * x; }
int main() {
ios::sync_with_stdio(!cin.tie(0));
rd();
rd();
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int c;
scanf("%d", &c);
a[i].resize(c);
for (auto& [x, y] : a[i]) {
x = rd();
y = rd();
}
double xlo = 1e18, xhi = -1e18;
double ylo = 1e18, yhi = -1e18;
for (int j = 0; j < c; j++) {
int k = j == c - 1 ? 0 : j + 1;
auto [u, v] = a[i][j];
auto [p, q] = a[i][k];
povrs[i] += u * q - p * v;
xlo = min(xlo, u);
ylo = min(ylo, v);
xhi = max(xhi, u);
yhi = max(yhi, v);
}
hc[i] = {(xlo + xhi) / 2, (ylo + yhi) / 2,
sqrt(sq(xhi - xlo) + sq(yhi - ylo)) / 2};
}
int q;
scanf("%d", &q);
while (q--) {
double x, y, r;
x = rd();
y = rd();
r = rd();
double mv = r * 0.1273123;
basic_string<int> sol;
double s = 0;
for (int i = 0; i < n; i++) {
int c = a[i].size();
bool touch = false;
int h = 0;
if (sq(hc[i].x - x) + sq(hc[i].y - y) > sq(r + hc[i].r)) {
continue;
}
for (int j = 0; j < c; j++) {
int k = j == c - 1 ? 0 : j + 1;
auto [p, q] = a[i][j];
auto [u, v] = a[i][k];
p -= x;
q -= y;
u -= x;
v -= y;
double sqd = sq(p - u) + sq(q - v);
double det = q * u - p * v;
double mul = det / sqd;
double hx = (q - v) * mul;
double hy = (u - p) * mul;
double r2 = sq(r);
if ((hx - p) * (hx - u) + (hy - q) * (hy - v) < 0) {
touch |= sq(hx) + sq(hy) <= r2;
}
touch |= sq(p) + sq(q) <= r2;
if (touch) break;
p += mv;
u += mv;
if (p * u < 0 && (p - u) * (q * u - p * v) < 0) h++;
}
if (touch || h % 2) {
sol += i;
s += povrs[i];
}
}
printf("%lf %d\n", s / 2, (int)sol.size());
for (int x : sol) printf(" %d", x);
printf("\n");
}
}
| 5 |
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
for(int c=1,w; cin>>w,w; c++){
int n,tr[1001][2] = {0};
cin >>n;
for(int i=1; i<=n; i++){
scanf("%d,%d",&tr[i][0],&tr[i][1]);
}
int dp[1001][1001] = {0};
for(int i=1; i<=n; i++){
for(int j=0; j<=w; j++){
if(j>=tr[i][1]){dp[j][i] = max(dp[j][i-1],dp[j-tr[i][1]][i-1]+tr[i][0]);}
else{dp[j][i] = dp[j][i-1];}
}
}
int pr = dp[w][n], val;
for(int j=0; j<=w; j++){
if(pr == dp[j][n]){val = j;break;}
}
cout <<"Case "<<c<<":"<<endl;
cout <<pr<<endl<<val<<endl;
}
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
#define INF 1000000000
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
typedef long long LL;
LL N;
LL X;
LL x[200011];
LL s[200011];
LL t[200011];
int main(){
cin>>N>>X;
REP(i,N){
cin>>x[i];
}
s[0]=0;
REP(i,N){
s[i+1]=s[i]+x[i];
}
t[0]=5;
t[1]=5;
for(int i=2;i<N+1;i++){
t[i]=t[i-1]+2;
}
LL ans=1e17;
REP(i,N){
LL y=i+1;
LL c=0;
LL now=0;
for(int j=N;j>0;j-=y){
LL aa=max(j-y,LL(0));
now+=(t[c]*(s[j]-s[aa]));
//cout<<"miii"<<now<<endl;
c++;
if(now>ans)break;
}
if(now>ans)continue;
now+=X*(y+N);
//cout<<now<<" aaa"<<endl;
ans=min(ans,now);
}
cout<<ans<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
long long shouldendl = 1;
long long INF = (long long)(1e9 + 7);
long long MOD = (long long)(1e9 + 7);
using namespace std;
long long modInverse(long long a) {
long long m = MOD, m0 = m, y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long q = a / m, t = m;
m = a % m, a = t, t = y;
y = x - q * y, x = t;
}
if (x < 0) x += m0;
return x;
}
long long add(long long a, long long b) {
return ((a % MOD) + (b % MOD)) % MOD;
}
long long mul(long long a, long long b) {
return ((a % MOD) * (b % MOD)) % MOD;
}
long long sub(long long a, long long b) {
long long ans = ((a % MOD - b % MOD)) % MOD;
if (ans < 0) ans += MOD;
return ans;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
void MAIN(long long i, long long t);
int get(int f = 2) {
if (f != 2) return 1;
long long t;
cin >> t;
;
return t;
}
template <class T>
int logd(T name) {
cout << name << endl;
return 0;
}
void test(int t) {
for (long long i = 1; i <= t; i++) {
MAIN(i, t);
if (shouldendl) cout << "\n";
}
}
long long power(long long x, long long y) {
if (y == 0) return 1;
long long res = 1;
x = x % MOD;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % MOD;
y = y >> 1;
x = (x * x) % MOD;
}
return res;
}
template <typename T, typename U>
std::pair<T, U> operator+(const std::pair<T, U> &l, const std::pair<T, U> &r) {
return {l.first + r.first, l.second + r.second};
}
long long popcnt(long long x) { return __builtin_popcountll(x); }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
template <class T>
bool input(vector<T> &v, long long n = -1) {
if (n == -1) assert(v.size() != 0);
if (n == -1) n = v.size();
for (auto &i : v) cin >> i;
return 0;
}
template <class T>
bool print(vector<T> v, long long l = 0, long long r = -1) {
if (r == -1) r = v.size();
for (int i = l; i < r; i++) cout << v[i] << " ";
return 0;
}
vector<int> edges[200005];
vector<int> a(200005);
int dp[200005][2];
map<int, bool> visited;
long long n;
void dfs(long long u) {
visited[u] = true;
dp[u][0] = 1 - a[u];
dp[u][1] = a[u];
for (auto v : edges[u]) {
if (visited[v]) continue;
dfs(v);
int zero = dp[u][0];
int one = dp[u][1];
dp[u][0] = mul(zero, dp[v][1]);
dp[u][1] = mul(one, dp[v][1]);
dp[u][0] += mul(zero, dp[v][0]);
dp[u][1] += add(mul(zero, dp[v][1]), mul(one, dp[v][0]));
dp[u][0] %= MOD;
dp[u][1] %= MOD;
}
}
void MAIN(long long current_test_case, long long total_test_cases) {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u = i + 1, v;
cin >> v;
edges[u].push_back(v);
edges[v].push_back(u);
}
for (int i = 0; i < n; i++) cin >> a[i];
dfs(0);
cout << dp[0][1];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
shouldendl = 1;
test(get(1));
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<int> unknown;
int sum;
int m;
int solve(int even, int odd){
int res = sum;
for(int i = 0; i < even; i++){
int v = 2 * unknown[i];
int adding = 0;
if(v >= 10){
adding += v%10;
v /= 10;
adding += v%10;
res += adding;
}else{
res+=v;
}
res %= 10;
}
for(int i = even; i < odd+even; i++){
res += unknown[i];
res %= 10;
}
return res%10 == 0;
}
int main(void){
int n;
cin >> n;
int odd = 0, even = 0;
string str;
cin >> str;
sum = 0;
for(int i = 1; i <= n; i++){
char c = str[n-i];
if(c == '*'){
if(i % 2 == 0) even++;
if(i % 2 == 1) odd++;
}else{
int v = c - '0';
if(i % 2 == 1)
sum += v;
else{
v *= 2;
if(v >= 10){
sum += v%10;
v /= 10;
sum += v%10;
}else{
sum += v;
}
}
sum %= 10;
}
}
unknown = vector<int>(even + odd);
cin >> m;
vector<int> a(m);
for(int i = 0; i < m; i++) cin >> a[i];
int cnt = even + odd;
int ans = 0;
for(int i1 = 0; i1 < m; i1++){
unknown[0] = a[i1];
if(cnt == 1){
ans += solve( even, odd);
continue;
}
for(int i2 = 0; i2 < m; i2++){
unknown[1] = a[i2];
if(cnt == 2){
ans += solve( even, odd);
continue;
}
for(int i3 = 0; i3 < m; i3++){
unknown[2] = a[i3];
if(cnt == 3){
ans += solve( even, odd);
continue;
}
for(int i4 = 0; i4 < m; i4++){
unknown[3] = a[i4];
if(cnt == 4){
ans += solve( even, odd);
continue;
}
for(int i5 = 0; i5 < m; i5++){
unknown[4] = a[i5];
if(cnt == 5){
ans += solve( even, odd);
continue;
}
for(int i6 = 0; i6 < m; i6++){
unknown[5] = a[i6];
if(cnt == 6){
ans += solve( even, odd);
continue;
}
for(int i7 = 0; i7 < m; i7++){
unknown[6] = a[i7];
ans += solve( even, odd);
}
}
}
}
}
}
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
int T = 1000000;
double C = (1 - (double)a / b) * (1 - (double)c / d), ans = (double)a / b;
while (T--) {
ans += ans * C;
C *= C;
}
printf("%.10lf", (double)ans);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, row, col, k;
string abc = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890";
cin >> t;
while (t--) {
cin >> row >> col >> k;
string s[row];
int n = 0;
for (int i = 0; i < row; i++) {
cin >> s[i];
n += count(s[i].begin(), s[i].end(), 'R');
}
int a = n / k, b = n % k;
int r = 0, c = 0, dif = 1, j = 0;
for (int i = 0; i < k; i++) {
if (i == k - b) a++;
int m = 0;
while (m < a) {
if (s[r][c] == 'R') m++;
s[r][c] = abc[j];
c += dif;
if (c == col) {
cout << s[r] << endl;
r++;
c--;
dif = -1;
}
if (c == -1) {
cout << s[r] << endl;
r++;
c++;
dif = 1;
}
}
j++;
}
j--;
while (r < row) {
s[r][c] = abc[j];
c += dif;
if (c == col) {
cout << s[r] << endl;
r++;
c--;
dif = -1;
}
if (c == -1) {
cout << s[r] << endl;
r++;
c++;
dif = 1;
}
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> luck;
void back(long long num, int pos) {
if (pos == 10) {
return;
}
luck.push_back(num * 10LL + 4LL);
back(num * 10LL + 4LL, pos + 1);
luck.push_back(num * 10LL + 7LL);
back(num * 10LL + 7LL, pos + 1);
}
long long calc(long long l1, long long r1, long long l2, long long r2) {
if (r1 < l2) return 0;
if (r2 < l1) return 0;
if (l1 < l2) {
return (min(r1, r2) + 1 - l2);
} else {
return (min(r1, r2) + 1 - l1);
}
}
int main() {
back(0, 0);
sort(luck.begin(), luck.end());
long long pl, pr, vl, vr, k;
cin >> pl >> pr >> vl >> vr >> k;
long long tot = (pr - pl + 1) * (vr - vl + 1);
long long way = 0;
for (int i = 0; i < luck.size() - 1; i++) {
int j = i + k - 1;
if (j + 1 >= luck.size()) break;
if (i != 0) {
long long p1 = calc(luck[i - 1] + 1, luck[i], pl, pr);
long long p2 = calc(luck[j], luck[j + 1] - 1, vl, vr);
way = way + p1 * p2;
p1 = calc(luck[i - 1] + 1, luck[i], vl, vr);
p2 = calc(luck[j], luck[j + 1] - 1, pl, pr);
way = way + p1 * p2;
if (k == 1 && luck[i] >= vl && luck[i] <= vr && luck[i] >= pl &&
luck[i] <= pr)
way--;
} else {
long long p1 = calc(1, luck[i], pl, pr);
long long p2 = calc(luck[j], luck[j + 1] - 1, vl, vr);
way = way + p1 * p2;
p1 = calc(1, luck[i], vl, vr);
p2 = calc(luck[j], luck[j + 1] - 1, pl, pr);
way = way + p1 * p2;
if (k == 1 && luck[i] >= vl && luck[i] <= vr && luck[i] >= pl &&
luck[i] <= pr)
way--;
}
}
double a1 = way;
double b1 = tot;
a1 = a1 / b1;
printf("%.10lf\n", a1);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, y, large = 2000000000, small = -2000000000;
string s1, s2;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s1 >> x >> s2;
if ((s1 == ">" && s2 == "Y") || (s1 == "<=" && s2 == "N")) {
if (x >= small) {
small = x + 1;
}
} else if ((s1 == "<" && s2 == "Y") || (s1 == ">=" && s2 == "N")) {
if (x <= large) {
large = x - 1;
}
} else if ((s1 == ">=" && s2 == "Y") || (s1 == "<" && s2 == "N")) {
if (x >= small) {
small = x;
}
} else {
if (x <= large) {
large = x;
}
}
}
if (small > large) {
cout << "Impossible";
} else {
cout << small;
}
return (0);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a;
long long n, w[26], ans = 0, value = 0;
cin >> a >> n;
for (int i = 0; i < 26; i++) {
cin >> w[i];
value = max(value, w[i]);
}
for (int i = 0; i < a.size() + n; i++) {
if (i < a.size())
ans += w[a[i] - 'a'] * (i + 1);
else
ans += value * (i + 1);
}
cout << ans;
return 0;
}
| 2 |
// {{{ Boilerplate Code <--------------------------------------------------
// vim:filetype=cpp:foldmethod=marker:foldmarker={{{,}}}
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define ALL(A) (A).begin(), (A).end()
using namespace std;
// }}}
int main(){
int n;
vector <double> d;
cin>>n;
FOR(i,0,n){
int tmp;
cin>>tmp;
d.push_back(tmp);
}
vector <double> pos;
double ret = 0.0;
FOR(i,0,n){
double curpos = 0.0;
curpos=max(curpos,d[i]);
FOR(j,0,i){
double e1=d[i]-d[j];
double e2=d[i]+d[j];
double e3=sqrt(e2*e2-e1*e1);
curpos=max(curpos,pos[j]+e3);
}
pos.push_back(curpos);
ret=max(ret,curpos+d[i]);
}
cout<<setprecision(15)<<ret<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
const long long INF = 1LL * inf * inf;
const double eps = 1e-9;
const int md = (int)1e9 + 7;
const double EPS = 1e-7;
const long long maxll = numeric_limits<long long>::max();
const double PI = acos(-1.0);
template <typename name>
inline name sqr(name x) {
return x * x;
}
inline long long multmod(long long a, long long b, long long md) {
if (!a) return 0;
if (a % 2 == 1)
return (multmod(a - 1, b, md) + b) % md;
else
return sqr<long long>(multmod(a / 2, b, md)) % md;
}
template <typename name>
inline name bpow(name base, long long exp) {
name res = base;
while (exp) {
if (exp % 2 == 1) {
res = res * base;
}
base = base * base;
exp /= 2;
}
return res;
}
int n, x, m;
double p[2000];
struct matr {
double a[1 << 7][1 << 7];
} mat, qq;
const matr& operator*(const matr& x, const matr& y) {
for (int i = 0; i < (1 << 7); i++)
for (int j = 0; j < (1 << 7); j++) qq.a[i][j] = 0;
for (int i = 0; i < (1 << 7); i++)
for (int j = 0; j < (1 << 7); j++)
for (int k = 0; k < (1 << 7); k++) qq.a[i][j] += x.a[i][k] * y.a[k][j];
return qq;
}
int main() {
ios_base ::sync_with_stdio(0);
cin >> n >> x;
for (int i = 0; i <= x; i++) cin >> p[i];
for (int mask1 = 0; mask1 < (1 << 7); mask1++)
for (int mask2 = 0; mask2 < (1 << 7); mask2++)
mat.a[mask1][mask2] = p[mask1 ^ mask2];
mat = bpow(mat, n - 1);
cout << fixed << setprecision(10) << 1 - mat.a[0][0];
return 0;
}
| 4 |
#include <iostream>
using namespace std;
int main()
{
int A[30] = {};
int P[1000000] = {};
int i = 0, k;
while (cin >> k) A[i++] = k;
int c = 0, j;
for (int k = 2; k < 1000000; ++k){
if (P[k] == 0){
P[k] = ++c;
for (j = 2; j * k < 1000000; ++j)
P[j * k] = -1;
}
else if (P[k] == -1)
P[k] = c;
}
for (j = 0; j < i; ++j)
cout << P[A[j]] << "\n";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
int main() {
char a[15][15] = {0};
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
cin >> a[i][j];
}
}
int n, m;
cin >> n >> m;
n--;
m--;
int p = n % 3, q = m % 3;
int num = 0;
for (int i = p * 3; i < p * 3 + 3; i++) {
for (int j = q * 3; j < q * 3 + 3; j++) {
if (a[i][j] == '.') {
num++;
a[i][j] = '!';
}
}
}
if (num == 0) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (a[i][j] == '.') a[i][j] = '!';
}
}
}
for (int i = 0; i < 9; i++) {
if (i && i % 3 == 0) cout << endl;
for (int j = 0; j < 9; j++) {
if (j && j % 3 == 0) cout << ' ';
cout << a[i][j];
}
cout << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define xx first
#define yy second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz(x) x.size()
#define pii pair<int,int>
#define en '\n'
#define sp ' '
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ar array
using namespace std;
//const ll mod = 998244353;
const ll mod = 1e9+7;
const int N = 1e5+5e4 + 2;
const int M = 4;
const int inf = 2e9+2;
const ll linf = 8e18+2;
ll add(ll a,ll b){a+=b;return a>=mod?a-mod:a;}
ll mul(ll a,ll b){return (ll)a*b%mod;}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
vector<int>n(4);
vector<vector<int>>data(4);
for(int i=0;i<4;i++)cin>>n[i];
for(int i=0;i<4;i++){
data[i].resize(n[i]);
for(int j=0;j<n[i];j++){
cin>>data[i][j];
}
}
vector<vector<vector<int>>>a(3);
for(int i=0;i<3;i++){
a[i].resize(n[i+1]);
int m;
cin>>m;
for(int j=0,x,y;j<m;j++){
cin>>x>>y;
x--,y--;
a[i][y].pb(x);
}
}
vector<vector<int>>dp(4);
dp[0]=data[0];
for(int i=0;i<3;i++){
dp[i+1].resize(n[i+1],inf);
multiset<int>s;
for(int j=0;j<n[i];j++)s.insert(dp[i][j]);
for(int j=0;j<n[i+1];j++){
for(auto k : a[i][j])s.erase(s.find(dp[i][k]));
if(!s.empty())dp[i+1][j]=*s.begin()+data[i+1][j];
for(auto k : a[i][j])s.insert(dp[i][k]);
}
}
int ans = *min_element(all(dp[3]));
if(ans>1e9)cout<<"-1\n";
else cout<<ans<<en;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int beki(int a, int b) {
int x = 1;
while (b != 0) {
if (b & 1) {
x = ((((long long)x * a + 998244353)) % 998244353);
}
a = ((((long long)a * a + 998244353)) % 998244353);
b >>= 1;
}
return x;
}
int gyakugen(int a) { return beki(a, 998244353 - 2); }
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<vector<int>> dp(k + 1, vector<int>(k + 1, 0));
dp[0][0] = 1;
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= k; j++) {
dp[i][j] = ((((long long)dp[i][j] +
((((long long)dp[i - 1][j] * j + 998244353)) % 998244353) +
998244353)) %
998244353);
dp[i][j] =
((((long long)dp[i][j] +
((((long long)dp[i - 1][j - 1] * max(0, n - j + 1) + 998244353)) %
998244353) +
998244353)) %
998244353);
}
}
int ans = 0;
for (int i = 1; i <= k; i++) {
ans = ((((long long)ans +
((((long long)dp[k][i] * gyakugen(beki(m, i)) + 998244353)) %
998244353) +
998244353)) %
998244353);
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;
const int maxn = 5e5 + 20;
template <class T>
inline void read(T& x) {
int sign = 1;
char c = getchar();
x = 0;
while (c > '9' || c < '0') {
if (c == '-') sign = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
x *= sign;
}
using namespace std;
int n;
char c[maxn];
std::pair<int, int> a[maxn];
inline int get(std::pair<int, int>& p1, std::pair<int, int>& p2) {
int x = p1.first - p2.first, y = p1.second - p2.second;
if (x < 0 && y < 0) {
x = -x;
y = -y;
}
if (x >= 0 && y >= 0) return max(x, y);
if (x < 0) x = -x;
if (y < 0) y = -y;
return x + y;
}
inline int g(std::pair<int, int> p1) {
int ans = 0;
for (register int i = 1; i <= n; ++i) {
ans = max(ans, get(p1, a[i]));
}
return ans;
}
std::pair<int, int> f(int x) {
std::pair<int, int> ans = {inf, inf};
int l = 0, r = maxn - 1;
if (!x) ++l;
while (l <= r) {
int k1 = l + (r - l) / 3, k2 = r - (r - l) / 3;
int ans1 = g({x, k1}), ans2 = g({x, k2});
if (ans1 > ans2) {
l = k1 + 1;
if (ans2 < ans.first) {
ans = {ans2, k2};
}
} else {
r = k2 - 1;
if (ans1 < ans.first) {
ans = {ans1, k1};
}
}
}
return ans;
}
int main() {
read(n);
for (register int i = 1; i <= n; ++i) {
scanf("%s", c + 1);
int len = strlen(c + 1);
int cnt = 0;
for (int j = 1; c[j]; j++) {
cnt += (c[j] == 'N');
}
a[i] = {cnt, len - cnt};
}
int maxv = inf;
int L, R;
int l = 0, r = maxn - 1;
while (l <= r) {
int k1 = l + (r - l) / 3, k2 = r - (r - l) / 3;
std::pair<int, int> ans1 = f(k1), ans2 = f(k2);
if (ans1.first > ans2.first) {
l = k1 + 1;
if (ans2.first < maxv) {
maxv = ans2.first;
L = k2;
R = ans2.second;
}
} else {
r = k2 - 1;
if (ans1.first < maxv) {
maxv = ans1.first;
L = k1;
R = ans1.second;
}
}
}
printf("%d\n", maxv);
for (int i = 1; i <= R; i++) putchar('B');
for (int i = 1; i <= L; i++) putchar('N');
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
for (int i = 0; i <= 9; i++) {
cout << i << "\n";
fflush(stdout);
string tp;
getline(cin, tp);
if (tp == "great!" || tp == "cool" || tp == "not bad" ||
tp == "don't touch me" || tp == "don't think so") {
cout << "normal";
exit(0);
}
if (tp == "no way" || tp == "are you serious?" || tp == "don't even" ||
tp == "go die in a hole" || tp == "terrible" || tp == "worse") {
cout << "grumpy";
exit(0);
}
}
}
| 2 |
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int n,m,i,j;
while(cin >> n >> m, n && m){
int ret = 0;
vector<int> data(n+1);
vector<int> all;
for(i=0;i<n;i++)cin >> data[i];
for(i=0;i<n+1;i++)
for(j=i;j<n+1;j++)
all.push_back(data[i]+data[j]);
sort(all.begin(),all.end());
for(i=0;i<all.size();i++){
int t = m-all[i];
vector<int>::iterator it = upper_bound(all.begin(),all.end(),t);
if(it != all.begin()){
ret = max(ret,all[i] + *(it-1) );
}
}
cout << ret << endl;
}
}
| 0 |
#include <bits/stdc++.h>
#define rep(i,s,t) for(int i=s;i<=(t);++i)
using namespace std;
const int N=1e5+50;
bool vis[N];
int n,m,a[N],b[N],p[N],id[N];
long long sum[N],f[N];
vector<int> g[N];
int find(int x){ return p[x]==x?x:p[x]=find(p[x]); }
int main(int argc,char *argv[]){
scanf("%d%d",&n,&m);
rep(i,1,n){
scanf("%d%d",&a[i],&b[i]);
a[i]=max(0,a[i]-b[i]);
sum[i]=b[i];
}
rep(i,1,m){
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
rep(i,1,n)p[i]=i,id[i]=i;
sort(id+1,id+n+1,[](int x,int y){ return a[x]<a[y]; });
rep(k,1,n){
int v=id[k];
vis[v]=true;
vector<int> son;
for(int d:g[v])if(vis[d]&&find(d)!=find(v)){
sum[v]+=sum[find(d)];
son.push_back(find(d));
p[find(d)]=find(v);
}
f[v]=sum[v]+a[v];
for(int d:son){
f[v]=min(f[v],sum[v]-sum[d]+max(1ll*a[v],f[d]));
}
}
printf("%lld\n",f[id[n]]);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
set<long long> s;
unordered_map<long long, long long> mp;
long long a[101000], dp[1010000][2], n;
int main() {
long long x;
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> x;
s.insert(x);
mp[x]++;
}
n = s.size();
int k = 1;
set<long long>::iterator it;
for (it = s.begin(); it != s.end(); it++) a[k++] = *it;
long long maxi = 0;
for (int i = 1; i <= n; i++) maxi = max(a[i], maxi);
for (int i = 1; i <= maxi; i++) {
dp[i][1] = mp[i] * i + dp[i - 1][0];
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
}
cout << max(dp[maxi][1], dp[maxi][0]) << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int l[200010], r[200010];
const int MOD = 1000000007;
const int MOD2 = 1000000006;
int p[200010];
int exp(int a, int b, int c) {
int ans = 1, num = a;
while (b > 0) {
if (b & 1) {
ans = (long long)ans * num % c;
}
num = (long long)num * num % c;
b >>= 1;
}
return ans;
}
int main() {
int m;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
int x;
scanf("%d", &x);
p[x]++;
}
l[0] = 1;
for (int i = 1; i <= 200001; i++) {
l[i] = (long long)l[i - 1] * (p[i] + 1) % MOD2;
}
r[200001] = 1;
for (int i = 200000; i >= 0; i--) {
r[i] = (long long)r[i + 1] * (p[i] + 1) % MOD2;
}
int res = 1;
for (int i = 2; i <= 200000; i++) {
long long mult = (long long)l[i - 1] * r[i + 1] % MOD2;
for (int j = 0; j < p[i]; j++) {
long long tmp = (long long)mult * (p[i] - j) % MOD2;
res = (long long)res * exp(i, tmp, MOD) % MOD;
}
}
printf("%d\n", res);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool check(int x, int n, vector<int>& a) {
vector<int> temp;
int i = 0, j = x;
int cnt = 0;
bool ok = false;
while (cnt < n) {
if (ok)
temp.push_back(a[i]), i++, ok = false;
else
temp.push_back(a[j]), j++, ok = true;
cnt++;
}
cnt = 0;
return cnt == x;
}
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
sort(a.begin(), a.end());
int cnt = 0;
vector<int> ans(n);
for (int i = 1; i < n; i += 2) {
ans[i] = a[cnt++];
}
for (int i = 0; i < n; i += 2) ans[i] = a[cnt++];
cnt = 0;
for (int i = 1; i < n - 1; i++) {
if (ans[i] < ans[i - 1] && ans[i] < ans[i + 1]) cnt++;
}
cout << cnt << "\n";
for (int i = 0; i < n; i++) cout << ans[i] << " ";
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
;
int t = 1;
while (t--) {
solve();
cout << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long nop = 100;
vector<int> primes(nop, 1);
long long cnt = 0;
void sieve() {
primes[1] = primes[0] = 0;
for (long long i = 2; i * i <= nop; i++) {
if (primes[i]) {
for (long long j = i * i; j <= nop; j += i) {
primes[j] = 0;
}
}
}
}
bool isVowel(char ch) {
ch = tolower(ch);
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
long long gcd(long long a, long long b) {
if (b > a) swap(b, a);
if (b == 0) return a;
return gcd(b, a % b);
}
void extgcd(long long a, long long b, long long& x, long long& y) {
if (a == 0) {
x = a;
y = b;
return;
}
extgcd(b % a, a, x, y);
long long y1 = y, x1 = x;
x = y1 - (b / a) * x1;
y = x1;
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long long bexp(long long num, long long e) {
long long ans = 1;
while (e > 0) {
if (e & 1) {
ans *= num;
}
num *= num;
e >>= 1;
}
return ans;
}
long long mexp(long long num, long long e) {
long long ans = 1;
while (e > 0) {
if (e & 1) {
ans = (ans * num) % mod;
}
num = (num * num) % mod;
e >>= 1;
}
return ans % mod;
}
long long modinv(long long a) { return bexp(a, mod - 2); }
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
string s, t;
cin >> s >> t;
long long ans = 0;
for (long long i = 0; i < s.length(); i++) {
for (long long l = 1; l <= s.length() - i; l++) {
long long j = i + l - 1;
long long ti = 0;
for (long long k = 0; k < s.length(); k++) {
if (k == i) {
k = j;
continue;
}
if (s[k] == t[ti]) {
ti++;
}
if (ti == t.length()) break;
}
if (ti == t.length()) ans = max(ans, l);
}
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 101;
pair<int, int> l[N];
vector<int> pt;
int dp[N][3 * N];
inline void smax(int &a, int b) {
if (a < b) a = b;
}
inline int fnd(int point) {
return lower_bound(pt.begin(), pt.end(), point) - pt.begin();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> l[i].first >> l[i].second;
sort(l, l + n);
for (int i = 0; i < n; i++)
pt.push_back(l[i].first - l[i].second),
pt.push_back(l[i].first + l[i].second), pt.push_back(l[i].first);
sort(pt.begin(), pt.end());
pt.resize(distance(pt.begin(), unique(pt.begin(), pt.end())));
int m = pt.size();
for (int i = n - 1; i >= 0; i--)
for (int j = m - 1; j >= 0; j--) {
if (l[i].first <= pt[j]) {
int nex = max(pt[j], l[i].first + l[i].second);
smax(dp[i][j], dp[i + 1][fnd(nex)] + nex - pt[j]);
continue;
}
int mx = 0, seg = 0;
smax(dp[i][j], dp[i + 1][fnd(l[i].first + l[i].second)] + l[i].second);
for (int k = i; k < n; k++) {
int cur = l[k].first - l[k].second;
cur = max(cur, pt[j]);
mx = max(mx, l[k].first);
seg += max(l[k].first + l[k].second - mx, 0);
if (cur <= l[i].first)
smax(dp[i][j],
dp[k + 1][fnd(mx)] + l[k].first - cur + mx - l[k].first);
mx = max(mx, l[k].first + l[k].second);
}
smax(dp[i][j], seg);
}
cout << dp[0][0] << "\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL NS = (LL)2e5 + 4, MOD = 998244353;
LL N;
LL arr[NS];
LL val[NS];
int main(){
scanf("%lld", &N);
LL ans = 0, st = -1;
for(LL i = 1; i <= N; ++i){
scanf("%1d", arr + i);
}
int last = 0;
for(int j = 1; j <= N; ++j){
if(arr[j]) last = j;
if(N % j == 0 && N / j % 2){
LL cnt = 0;
for(int x = 1; x <= j; ++x){
cnt *= 2;
cnt += arr[x];
cnt %= MOD;
}
++cnt;
int A = (int)1e9, B = (int)1e9;
for(int x = 1; x <= j; ++x){
for(int y = x, z = arr[x]; y <= N; y += j, z ^= 1){
if(arr[y] < z){
A = min(A, y);
}
else if(arr[y] > z){
B = min(B, y);
}
}
}
cnt -= B > A; if(cnt < 0) cnt += MOD;
for(int x = 1; x * x <= j; ++x){
if(j % x == 0){
cnt -= val[x];
if(x * x < j) cnt -= val[j / x];
while(cnt < 0) cnt += MOD;
}
}
val[j] = cnt;
ans += cnt * j * 2;
ans %= MOD;
}
}
printf("%lld\n", ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long N, M;
int cnt[1000010][26];
long long S1, S2;
string str1, str2;
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int main() {
cin >> N >> M;
cin >> str1 >> str2;
S1 = str1.size();
S2 = str2.size();
long long g = gcd(S1, S2);
for (int i = 0; i < g; i++) {
for (int j = i; j < S2; j += g) {
cnt[i][str2[j] - 'a']++;
}
}
long long ans = 0;
for (int i = 0; i < S1; i++) ans += S2 / g - cnt[i % g][str1[i] - 'a'];
cout << N * gcd(S1, S2) / S2 * ans << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct point {
double x, y, z;
void read() { scanf("%lf%lf%lf", &x, &y, &z); }
};
int n;
point p[101], o, ans;
double chk2 = -1.0;
double dist(point p1, point p2) {
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y) +
(p1.z - p2.z) * (p1.z - p2.z));
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) p[i].read();
o.x = o.y = o.z = 0.00;
for (double t = 1.0; t >= 1e-11; t *= 0.9995) {
point sv = p[0];
double chk = dist(p[0], o);
for (int i = 0; i < n; i++)
if (chk < dist(p[i], o)) sv = p[i], chk = dist(p[i], o);
if (chk2 < 0 || chk2 > chk) chk2 = chk, ans = o;
o.x = o.x + t * (sv.x - o.x);
o.y = o.y + t * (sv.y - o.y);
o.z = o.z + t * (sv.z - o.z);
}
printf("%.10lf %.10lf %.10lf\n", ans.x, ans.y, ans.z);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
if (k > n)
cout << -1;
else
cout << a[n - k] << " " << 0 << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
cout << n;
for (int i = 1; i < n; ++i) {
cout << " " << i;
}
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
string s;
long long n;
long long dp[100001][2];
long long f(long long i, long long tight) {
if (i == n) return 0;
long long ans = 0;
if (dp[i][tight] != -1) return dp[i][tight];
if (tight) {
for (long long j = 0; j <= s[i] - '0'; j++)
ans = max(ans, f(i + 1, j == s[i] - '0') + j * v[i]);
} else {
for (long long j = 0; j <= 1; j++) ans = max(ans, f(i + 1, 0) + j * v[i]);
}
return dp[i][tight] = ans;
}
void solve(long long testcase) {
memset(dp, -1, sizeof dp);
cin >> n;
v.resize(n);
for (long long &x : v) cin >> x;
reverse(v.begin(), v.end());
cin >> s;
reverse(s.begin(), s.end());
cout << f(0, 1) << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long T = 1;
for (long long t = 1; t <= T; t++) solve(t);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t, n, a, b, c, i, ct, tmp;
char s[109], r[109];
cin >> t;
while (t--) {
cin >> n >> a >> b >> c;
for (i = 0; i < n; i++) {
cin >> s[i];
}
ct = 0;
for (i = 0; i < n; i++) {
if (s[i] == 'R' && b > 0) {
ct++;
b--;
r[i] = 'P';
} else if (s[i] == 'P' && c > 0) {
ct++;
c--;
r[i] = 'S';
} else if (s[i] == 'S' && a > 0) {
ct++;
a--;
r[i] = 'R';
} else {
r[i] = '.';
}
}
if ((n % 2) == 0) {
tmp = n / 2;
} else {
tmp = (n / 2) + 1;
}
if (ct < n) {
for (i = 0; i < n; i++) {
if (r[i] == '.') {
if (a > 0) {
r[i] = 'R';
a--;
} else if (b > 0) {
r[i] = 'P';
b--;
} else if (c > 0) {
r[i] = 'S';
c--;
}
}
}
}
if (ct >= tmp) {
cout << "YES"
<< "\n";
for (i = 0; i < n; i++) {
cout << r[i];
}
} else {
cout << "NO";
}
if (t != 0) {
cout << "\n";
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int n;
struct Node {
int l, r;
} a[N], pre[N], suf[N];
inline int size(const Node &x) { return x.r - x.l; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d%d", &a[i].l, &a[i].r);
pre[1] = a[1];
for (int i = 2; i <= n; ++i) {
pre[i].l = max(pre[i - 1].l, a[i].l);
pre[i].r = min(pre[i - 1].r, a[i].r);
}
suf[n] = a[n];
for (int i = n - 1; i >= 1; --i) {
suf[i].l = max(suf[i + 1].l, a[i].l);
suf[i].r = min(suf[i + 1].r, a[i].r);
}
int ans = 0;
ans = max(ans, size(suf[2]));
ans = max(ans, size(pre[n - 1]));
Node now;
for (int i = 2; i < n; ++i) {
now.l = max(pre[i - 1].l, suf[i + 1].l);
now.r = min(pre[i - 1].r, suf[i + 1].r);
ans = max(ans, size(now));
}
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, v[100010];
int main() {
scanf("%d", &n);
for (int k = 1;; k++) {
int p = ((1 << k) - 1) << (k - 1);
if (p > n) break;
v[p] = 1;
}
for (int i = n; i; i--)
if (!(n % i) && v[i]) return printf("%d", i), 0;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline long long rint() {
long long x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
inline long long gcd(long long a, long long b) {
return (b) ? gcd(b, a % b) : a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long pow(long long a, long long b, long long q) {
long long rtn = 1;
while (b) {
if (b & 1) rtn = rtn * a % q;
a = a * a % q;
b >>= 1;
}
return rtn;
}
inline long long mysqrt(long long n) {
long long x = sqrt(n);
while (x * x < n) x++;
while (x * x > n) x--;
return x;
}
char s[2005][2005];
int a[2005][2005], b[2005][2005], c[2005][2005], d[2005][2005];
void solve() {
int n = rint(), m = rint();
for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
s[0][1] = s[0][m] = s[n + 1][0] = s[n + 1][m] = '?';
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
a[i][j] = (s[i - 1][j] == s[i][j - 1] && s[i - 1][j] == s[i][j]
? min(a[i - 1][j], a[i][j - 1]) + 1
: 0);
for (int i = n; i >= 1; i--)
for (int j = 1; j <= m; j++)
b[i][j] = (s[i + 1][j] == s[i][j - 1] && s[i + 1][j] == s[i][j]
? min(b[i + 1][j], b[i][j - 1]) + 1
: 0);
for (int i = 1; i <= n; i++)
for (int j = m; j >= 1; j--)
c[i][j] = (s[i - 1][j] == s[i][j + 1] && s[i - 1][j] == s[i][j]
? min(c[i - 1][j], c[i][j + 1]) + 1
: 0);
for (int i = n; i >= 1; i--)
for (int j = m; j >= 1; j--)
d[i][j] = (s[i + 1][j] == s[i][j + 1] && s[i + 1][j] == s[i][j]
? min(d[i + 1][j], d[i][j + 1]) + 1
: 0);
long long res = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
res += min({a[i][j], b[i][j], c[i][j], d[i][j]}) + 1;
cout << res << "\n";
}
int main() { solve(); }
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 5;
string s, e;
char c;
int n, tr[4][10][10][N], t, q, l, r, pos;
map<char, int> m;
void updateOst(int q, int ost, int l, int i, int val) {
for (; i < n; i = (i | (i + 1))) tr[q][ost][l - 1][i] += val;
}
void update(int q, int pos, int val) {
for (int i = 1; i <= 10; i++) {
int tPos = pos / i;
int ost = pos % i;
updateOst(q, ost, i, tPos, val);
}
}
int sum(int q, int ost, int len, int i) {
int res = 0;
for (; i >= 0; i = (i & (i + 1)) - 1) res += tr[q][ost][len - 1][i];
return res;
}
int sum(int q, int ost, int len, int l, int r) {
if (l == 0)
return sum(q, ost, len, r);
else
return sum(q, ost, len, r) - sum(q, ost, len, l - 1);
}
int get(int q, int len, int ost, int l, int r) {
int tempL = l / len * len + ost;
if (tempL >= l)
l = tempL;
else
l = tempL + len;
int tempR = r / len * len + ost;
if (tempR <= r)
r = tempR;
else
r = tempR - len;
if (r < l) return 0;
int tl = l / len;
int tr = r / len;
return sum(q, ost, len, tl, tr);
}
int main() {
ios::sync_with_stdio(false);
m['A'] = 0;
m['T'] = 1;
m['G'] = 2;
m['C'] = 3;
cin >> s >> q;
n = s.size();
for (int i = 0; i < n; i++) update(m[s[i]], i, 1);
for (int i = 0; i < q; i++) {
cin >> t;
if (t == 1) {
cin >> pos >> c;
pos--;
update(m[c], pos, 1);
update(m[s[pos]], pos, -1);
s[pos] = c;
} else {
cin >> l >> r >> e;
l--;
r--;
int sz = e.size();
int ans = 0;
for (int j = 0; j < sz; j++) {
c = e[j];
ans += get(m[c], sz, (l + j) % sz, l, r);
}
cout << ans << "\n";
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
template <class T>
void read(T &x) {
int sgn = 1;
char ch;
x = 0;
for (ch = getchar(); (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
;
if (ch == '-') ch = getchar(), sgn = -1;
for (; '0' <= ch && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
x *= sgn;
}
template <class T>
void write(T x) {
if (x < 0)
putchar('-'), write(-x);
else if (x < 10)
putchar(x + '0');
else
write(x / 10), putchar(x % 10 + '0');
}
struct edge {
int to, nxt;
} tree[N << 1];
int n, head[N], rt, cnt = 0;
long long m, L, R;
void addedge(int u, int v) {
edge e = {v, head[u]};
tree[head[u] = cnt++] = e;
}
int sz[N], dep[N], par[N];
vector<int> child[N];
void dfs(int u, int fa) {
sz[u] = 1;
for (int i = head[u]; ~i; i = tree[i].nxt) {
int v = tree[i].to;
if (v != fa) {
par[v] = u, dep[v] = dep[u] + 1;
child[u].push_back(v), dfs(v, u);
sz[u] += sz[v];
}
}
R += min(sz[u], n - sz[u]);
if (fa && (sz[u] & 1)) L++;
}
void find_centroid(int u, int fa, int &c, int &w) {
int weight = 0;
for (int i = head[u]; ~i; i = tree[i].nxt) {
int v = tree[i].to;
if (v != fa) {
find_centroid(v, u, c, w);
weight = max(weight, sz[v]);
}
}
weight = max(weight, n - sz[u]);
if (weight < w) w = weight, c = u;
}
vector<int> vec[N];
queue<int> que;
void bfs(int s, int id) {
que.push(s);
while (!que.empty()) {
int u = que.front();
vec[id].push_back(u), que.pop();
for (int i = 0; i < child[u].size(); i++) que.push(child[u][i]);
}
}
int match[N], tot[N];
vector<int> con[N];
int main() {
read(n), read(m);
for (int i = 1; i <= n; i++) head[i] = -1;
for (int i = 1; i < n; i++) {
int u, v;
read(u), read(v);
addedge(u, v), addedge(v, u);
}
int w = n;
dep[1] = 0, dfs(1, 0);
find_centroid(1, 0, rt, w);
for (int i = 1; i <= n; i++) child[i].clear();
L = R = 0ll, dep[rt] = 0, dfs(rt, 0);
if (L <= m && m <= R && (m - L) % 2 == 0) {
puts("YES");
for (int i = 1; i <= n; i++) match[i] = 0;
for (int i = 0; i < child[rt].size(); i++) bfs(child[rt][i], i);
for (int i = 0; i < child[rt].size(); i++)
con[sz[child[rt][i]]].push_back(i);
int mx = n;
vector<int> tmp;
for (int i = 0; i < child[rt].size(); i++) tmp.push_back(i);
for (int T = n;; T -= 2) {
for (; mx > 1; mx--) {
bool flag = false;
for (int i = 0; i < con[mx].size(); i++) {
if (sz[child[rt][con[mx][i]]] == mx) flag = true;
}
if (flag) break;
}
if (mx <= 1) break;
while (sz[child[rt][con[mx].back()]] < mx) con[mx].pop_back();
int id = con[mx].back();
while (!vec[id].empty() && match[vec[id].back()]) vec[id].pop_back();
int u = vec[id].back(), v = par[u];
while (!child[v].empty() && match[child[v].back()]) child[v].pop_back();
if (child[v].back() == u) {
child[v].pop_back();
while (!child[v].empty() && match[child[v].back()]) child[v].pop_back();
child[v].push_back(u);
}
long long thershold = R - dep[u] -
(child[v].size() == 1 ? dep[v] : dep[u]) +
(child[v].size() == 1 ? 1 : 2);
if (m <= thershold) {
if (child[v].back() == u) child[v].pop_back();
while (!child[v].empty() && match[child[v].back()]) child[v].pop_back();
if (child[v].empty()) {
match[u] = v, match[v] = u;
con[sz[child[rt][id]] -= 2].push_back(id);
R -= dep[u] + dep[v], m--;
} else {
int w = child[v].back();
match[u] = w, match[w] = u;
con[sz[child[rt][id]] -= 2].push_back(id);
R -= (dep[u] << 1), m -= 2;
}
} else {
while (!sz[child[rt][tmp.back()]]) tmp.pop_back();
int id1, id2;
if (tmp.back() != id)
id1 = tmp.back(), id2 = id;
else {
tmp.pop_back();
while (!tmp.empty() && !sz[child[rt][tmp.back()]]) tmp.pop_back();
id1 = id, id2 = tmp.back();
tmp.push_back(id);
}
while (!vec[id1].empty() && match[vec[id1].back()]) vec[id1].pop_back();
while (!vec[id2].empty() && match[vec[id2].back()]) vec[id2].pop_back();
int u = vec[id1].back(), v = vec[id2].back();
match[u] = v, match[v] = u;
R -= dep[u] + dep[v], m -= dep[u] + dep[v];
con[--sz[child[rt][id1]]].push_back(id1);
con[--sz[child[rt][id2]]].push_back(id2);
}
}
tmp.clear();
for (int i = 0; i < child[rt].size(); i++) {
if (sz[child[rt][i]]) tmp.push_back(i);
}
match[rt] = child[rt][tmp[0]], match[child[rt][tmp[0]]] = rt;
for (int i = 2; i < tmp.size(); i += 2) {
match[child[rt][tmp[i]]] = child[rt][tmp[i - 1]];
match[child[rt][tmp[i - 1]]] = child[rt][tmp[i]];
}
for (int i = 1; i <= n; i++) {
if (match[i] > i) {
write(i), putchar(' ');
write(match[i]), putchar('\n');
}
}
} else
puts("NO");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
long long mod = 1000000007;
const int MX = 0x3f3f3f3f;
bool G[8][8];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
if (n <= 6) {
cout << m << endl;
return 0;
}
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
G[u][v] = G[v][u] = true;
}
int mini = INT_MAX;
for (int i = 1; i <= 7; i++) {
for (int j = 1; j <= 7; j++) {
if (j == i) continue;
int cur = 0;
for (int k = 1; k <= 7; k++) cur += G[i][k] & G[j][k];
mini = min(mini, cur);
}
}
cout << m - mini << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string str;
cin >> str;
for (int i = 0; i < n; i++) {
cout << str[n - 1];
}
cout << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, t;
cin >> s;
cin >> t;
int i, n;
n = s.length();
for (i = 0; i < n / 2; i++) {
swap(s[i], s[n - i - 1]);
}
if (s == t)
cout << "YES";
else
cout << "NO";
}
| 1 |
#include <bits/stdc++.h>
using std::cin;
using std::cout;
int main() {
int t, a, b, c, res = 0;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> a >> b >> c;
if (b > c / 2) {
res += 3 * (c / 2);
b -= c / 2;
if (a > b / 2) {
res += 3 * (b / 2);
} else {
res += 3 * a;
}
} else {
res += 3 * b;
}
cout << res << "\n";
res = 0;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
namespace IO {
const int buffer_size = 1e5 + 5;
char buf[buffer_size], *S, *T;
bool flag_EOF;
inline char read_char() {
if (S == T) T = (S = buf) + fread(buf, 1, buffer_size, stdin);
return S != T ? *(S++) : EOF;
}
inline int read_int() {
int flag = 1;
char c = read_char();
while (c < '0' || c > '9') {
if (c == EOF) {
flag_EOF = true;
return 0;
}
flag = (c == '-' ? -1 : 1);
c = read_char();
}
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + (c ^ 48);
c = read_char();
}
return x * flag;
}
char st[13];
int _top;
void Write(int x) {
if (!x) {
putchar('0');
return;
}
if (x < 0) {
putchar('-');
x = -x;
}
while (x) {
st[++_top] = x % 10 + '0';
x /= 10;
}
while (_top > 0) putchar(st[_top--]);
}
} // namespace IO
const int max_n = 3e5 + 5;
int x[max_n], y[max_n], pos[max_n];
long long ans[max_n];
inline bool cmp(int a, int b) { return y[a] - x[a] < y[b] - x[b]; }
int main() {
int n = IO::read_int(), m = IO::read_int();
for (int i = 1; i <= n; ++i)
x[i] = IO::read_int(), y[i] = IO::read_int(), pos[i] = i;
sort(pos + 1, pos + n + 1, cmp);
long long sum = 0;
for (int j = 1, i = pos[j]; j <= n; ++j, i = pos[j]) {
ans[i] = 1ll * (j - 1) * x[i] + 1ll * (n - j) * y[i] + sum;
sum += y[i];
}
sum = 0;
for (int j = n, i = pos[j]; j >= 1; --j, i = pos[j]) {
ans[i] += sum;
sum += x[i];
}
for (int i = 1; i <= m; ++i) {
int u = IO::read_int(), v = IO::read_int();
int score = min(x[u] + y[v], x[v] + y[u]);
ans[u] -= score, ans[v] -= score;
}
for (int i = 1; i <= n; ++i) printf("%lld%c", ans[i], i < n ? ' ' : '\n');
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x;
char c;
while ((c = getchar()) < '0' || c > '9')
;
for (x = c - '0'; (c = getchar()) >= '0' && c <= '9';)
x = (x << 3) + (x << 1) + c - '0';
return x;
}
struct edge {
int nx, t, w;
} e[10100 * 2 + 5];
int p[200000 + 5], pn, u[200000 + 5], pw[101 + 5], c[101 + 5], lv[101 + 5];
int h[101 + 5], en, d[101 + 5], q[101 + 5], qn, cr[101 + 5];
inline void ins(int x, int y, int w) {
e[++en] = (edge){h[x], y, w};
h[x] = en;
e[++en] = (edge){h[y], x, 0};
h[y] = en;
}
bool bfs() {
int i, j;
memset(d, 0, sizeof(d));
for (d[q[i = qn = 0] = 101 + 1] = 1; i <= qn; ++i)
for (j = cr[q[i]] = h[q[i]]; j; j = e[j].nx)
if (e[j].w && !d[e[j].t]) d[q[++qn] = e[j].t] = d[q[i]] + 1;
return d[101 + 2];
}
int dfs(int x, int r) {
if (x == 101 + 2) return r;
int k, u = 0;
for (int& i = cr[x]; i; i = e[i].nx)
if (e[i].w && d[x] + 1 == d[e[i].t]) {
k = dfs(e[i].t, min(e[i].w, r - u));
u += k;
e[i].w -= k;
e[i ^ 1].w += k;
if (u == r) return u;
}
return d[x] = 0, u;
}
int main() {
int n, k, i, j, l = 1, r = 1, mid, s, ans = -1;
for (i = 2; i <= 200000; ++i) {
if (!u[i]) p[++pn] = i;
for (j = 1; i * p[j] <= 200000; ++j) {
u[i * p[j]] = 1;
if (i % p[j] == 0) break;
}
}
n = read();
k = read();
for (i = 1; i <= n; ++i)
pw[i] = read(), c[i] = read(), r = max(r, lv[i] = read());
while (l <= r) {
mid = l + r >> 1;
memset(h, 0, sizeof(h));
en = 1;
for (s = 0, i = 1; i <= n; ++i)
if (lv[i] <= mid && c[i] == 1) s = max(s, pw[i]);
if (s)
pw[n + 1] = s, c[n + 1] = 1, lv[n + 1] = 1;
else
lv[n + 1] = mid + 1;
for (s = 0, i = 1; i <= n + 1; ++i)
if (lv[i] <= mid && (c[i] > 1 || i > n)) {
s += pw[i];
if (c[i] & 1)
ins(101 + 1, i, pw[i]);
else
ins(i, 101 + 2, pw[i]);
for (j = 1; j < i; ++j)
if (lv[j] <= mid && (c[j] > 1 || j > n) && !u[c[i] + c[j]])
if (c[i] & 1)
ins(i, j, 0x7FFFFFFF);
else
ins(j, i, 0x7FFFFFFF);
}
while (bfs()) s -= dfs(101 + 1, 0x7FFFFFFF);
if (s < k)
l = mid + 1;
else
ans = mid, r = mid - 1;
}
printf("%d", ans);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
string tostr(const T &t) {
ostringstream os;
os << t;
return os.str();
}
string guess[12];
int N, X[12], Y[12];
bool is_valid(string &ans, int ind) {
int x = 0, y = 0;
set<char> S((ans).begin(), (ans).end());
for (int i = 0; i < 4; ++i) {
if (ans[i] == guess[ind][i])
x++;
else if (S.count(guess[ind][i]))
y++;
}
return X[ind] == x && Y[ind] == y;
}
bool check(string &ans) {
for (int i = 0; i < N; ++i)
if (!is_valid(ans, i)) return false;
return true;
}
int main(int argc, char *argv[]) {
string ans, s = "0000";
cin >> N;
for (int i = 0; i < N; ++i) cin >> guess[i] >> X[i] >> Y[i];
int cnt = 0;
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
if (j == i) continue;
for (int k = 0; k < 10; ++k) {
if (k == j || k == i) continue;
for (int v = 0; v < 10; ++v) {
if (v == i || v == j || v == k) continue;
s[0] = '0' + i;
s[1] = '0' + j;
s[2] = '0' + k;
s[3] = '0' + v;
if (check(s)) ans = s, cnt++;
}
}
}
}
if (cnt == 0)
cout << "Incorrect data" << endl;
else if (cnt == 1)
cout << ans << endl;
else
cout << "Need more data" << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long inv2 = (mod + 1) / 2;
long long C[55][55];
long long inv[55];
long long dp[55][55][3];
long long N, M;
int main() {
inv[1] = 1;
for (long long i = 2; i <= 50; ++i)
inv[i] = mod - mod / i * inv[mod % i] % mod;
for (long long i = 0; i <= 50; ++i) {
C[i][0] = 1;
for (long long j = 1; j <= i; ++j) {
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}
}
dp[0][0][1] = 1;
dp[1][0][0] = 1;
for (long long i = 2; i <= 50; ++i) {
for (long long i1 = 0; i1 < i; ++i1) {
long long i2 = i - i1 - 1;
for (long long j1 = 0; j1 <= i1 / 2; ++j1) {
for (long long j2 = 0; j2 <= i2 / 2; ++j2) {
for (long long k1 = 0; k1 <= 1; ++k1) {
for (long long k2 = 0; k2 <= 1; ++k2) {
long long k = !(k1 && k2);
long long j = j1 + j2 + k;
long long tmp = dp[i1][j1][k1] * dp[i2][j2][k2] % mod * i % mod *
C[i - 1][i1] % mod;
tmp = tmp * inv2 % mod;
dp[i][j][k] += tmp;
dp[i][j][k] %= mod;
}
}
}
}
}
}
scanf("%I64d%I64d", &N, &M);
printf("%I64d\n", (dp[N][M][0] + dp[N][M][1]) * inv[N] % mod);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const long long mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps = 1e-10;
const int N = 1e5 + 5;
int n, m;
bool ask(int id) {
printf("? %d\n? %d\n", id, id + m);
fflush(stdout);
int x, y;
scanf("%d %d", &x, &y);
if (x == y) {
printf("! %d\n", id);
exit(0);
}
return x < y;
}
int main() {
scanf("%d", &n);
m = n >> 1;
if (m & 1)
puts("! -1");
else {
int l = 1, r = m + 1;
bool sign = ask(1);
while (r - l > 1) {
int mid = l + r >> 1;
if (ask(mid) == sign)
l = mid;
else
r = mid;
}
printf("! %d\n", l);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int a, b, c, l;
long long ans;
void equal(int a, int b, int c) {
for (int i = (0); i <= (l); i++) {
int r = l - i;
int tot = a + i - b - c;
if (tot >= r || a + i <= b || a + i <= c) continue;
int bm = a + i - b;
int cm = a + i - c;
if (bm + cm - 2 <= tot) continue;
ans += (long long)bm * cm;
if (tot >= 0) {
ans -= (long long)(tot + 1) * (tot + 2) / 2;
if (tot >= bm) ans += (long long)(tot - bm + 1) * (tot - bm + 2) / 2;
if (tot >= cm) ans += (long long)(tot - cm + 1) * (tot - cm + 2) / 2;
}
int num = bm + cm - 3 - r;
if (num < 0) continue;
ans -= (long long)(num + 1) * (num + 2) / 2;
if (num >= bm) ans += (long long)(num - bm + 1) * (num - bm + 2) / 2;
if (num >= cm) ans += (long long)(num - cm + 1) * (num - cm + 2) / 2;
}
}
void equal2(int a, int b, int c) {
int r = l;
r -= abs(a - b);
int ab = max(a, b);
for (int i = (0); i <= (r / 2); i++) {
int rr = r - i * 2;
int up = min(ab + i - c - 1, rr);
ans += max(0, up + 1);
}
}
void equal3(int a, int b, int c) {
int r = l;
int m = max(a, max(b, c));
r -= m - a;
r -= m - b;
r -= m - c;
if (r < 0) return;
for (int i = (0); i <= (r / 3); i++) ans++;
}
int main() {
scanf("%d %d %d %d", &a, &b, &c, &l);
equal(a, b, c);
equal(b, c, a);
equal(c, a, b);
equal2(a, b, c);
equal2(b, c, a);
equal2(c, a, b);
equal3(a, b, c);
printf("%I64d\n", ans);
scanf("\n");
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct node {
char s[10];
int x, y;
} A[511111];
int main() {
int n, x, y;
scanf("%d", &n);
scanf("%d%d", &x, &y);
char s[10], xx, yy;
int B[10];
memset(B, -1, sizeof(B));
for (int i = 0; i < n; i++) {
scanf("%s%d%d", &A[i].s, &A[i].x, &A[i].y);
int xx = A[i].x, yy = A[i].y;
if (yy == y) {
if (xx > x) {
if (B[3] == -1)
B[3] = i;
else if (A[B[3]].x > xx)
B[3] = i;
} else {
if (B[7] == -1)
B[7] = i;
else if (A[B[7]].x < xx)
B[7] = i;
}
}
if (xx == x) {
if (yy > y) {
if (B[1] == -1)
B[1] = i;
else if (A[B[1]].y > yy)
B[1] = i;
} else {
if (B[5] == -1)
B[5] = i;
else if (A[B[5]].y < yy)
B[5] = i;
}
}
if (yy - xx == y - x) {
if (xx > x) {
if (B[2] == -1)
B[2] = i;
else if (A[B[2]].x > xx)
B[2] = i;
} else {
if (B[6] == -1)
B[6] = i;
else if (A[B[6]].x < xx)
B[6] = i;
}
}
if (yy + xx == y + x) {
if (xx > x) {
if (B[4] == -1)
B[4] = i;
else if (A[B[4]].x > xx)
B[4] = i;
} else {
if (B[8] == -1)
B[8] = i;
else if (A[B[8]].x < xx)
B[8] = i;
}
}
}
int flag = 0;
for (int i = 1; i < 9; i++) {
if (B[i] != -1) {
if (i % 4 == 1 || i % 4 == 3) {
if (A[B[i]].s[0] == 'R' || A[B[i]].s[0] == 'Q') {
flag = 1;
break;
}
} else {
if (A[B[i]].s[0] == 'B' || A[B[i]].s[0] == 'Q') {
flag = 1;
break;
}
}
}
}
if (flag)
printf("YES\n");
else
printf("NO\n");
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
// Preprocessor loop
#define CAT_I(x, y) x ## y
#define CAT(x, y) CAT_I(x, y)
#define INC0 1
#define INC1 2
#define INC2 3
#define INC3 4
#define INC4 5
#define INC5 6
#define INC6 7
#define INC7 8
#define INC8 9
#define INC(i) CAT(INC, i)
#define EMPTY(...)
#define DEF_COMMA0 _,1 EMPTY
#define COMMA0() ,0
#define IS_EMPTY_III(f, s) s
#define IS_EMPTY_II(t) IS_EMPTY_III t
#define IS_EMPTY_I(x) IS_EMPTY_II((DEF_ ## x()))
#define IS_EMPTY(x, ...) IS_EMPTY_I(x COMMA0)
#define IF_0(x, y) y
#define IF_1(x, y) x
#define IF(cond, x, y) CAT(IF_, cond)(x, y)
#define FOR_EACH_I9(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I8(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I7(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I6(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I5(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I4(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I3(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I2(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I1(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I0(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I(i, F, ...) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH(F, ...) FOR_EACH_I(0, F, __VA_ARGS__)
#define IF_0(x, y) y
#define IF_1(x, y) x
#define IF(cond, x, y) CAT(IF_, cond)(x, y)
#define FOR_EACH_I9(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I8(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I7(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I6(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I5(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I4(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I3(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I2(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I1(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I0(i, F, x, ...) F(x) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I(i, F, ...) IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH(F, ...) FOR_EACH_I(0, F, __VA_ARGS__)
// Preprocessor loop end
typedef long long ll;
typedef string str;
#define down_queue(x) priority_queue<x>
#define up_queue(x) priority_queue<x, vector<x>, greater<x>>
#define vec(x) vector<x>
ll gcd(ll a,ll b){while(b){ll tmp = a % b;a = b;b = tmp;}return a;}
ll lcm(ll a,ll b){return a / gcd(a,b) * b;}
#define rep(x, y) for(int x = 0; x < (y); x++)
#define REP(x, z, y) for(int x = (z); x < (y); x++)
#define all(x) x.begin(), x.end()
#define split_str(str, sp_word) istringstream stream(str); string res; for(int cnt = 0; getline(stream,res,sp_word); cnt++)
#define digit(x) ((int)log10((double)(x)) + 1)
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define out(x) for(auto i : x) cout << i << " "; cout << endl;
#define outln(x) for(auto i : x) cout << i << " " << endl;
#define vec_cpy(to,from) copy(all(to),back_inserter(from))
#define ary_cpy(to,from) memcpy(to, from, sizeof from)
#define MOD = 1000000007;
#define DEBUG_VAR(v) << #v << "=" << v << " "
#ifdef DEBUG
#define debug_echo(e) cout << "L" << __LINE__ << ": " << e << endl
#define debug_var(...) cout << "L" << __LINE__ << ": " FOR_EACH(DEBUG_VAR, __VA_ARGS__) << endl
#define debug_echo_dp1(dp, N) { cout << "[ "; rep(i, N) cout << dp[i] << " "; cout << "]" << endl; }
#else
#define debug_echo(e)
#define debug_var(...)
#define debug_echo_dp1(dp, N)
#endif
// Implementation
#define MAX_N 5000
int N;
vector<int> p(MAX_N);
int greed(vector<int>& z, int n) {
if (n >= N-1) return 0;
debug_var(n, max(0, z[n]), max(0, z[n+1]));
z[n+1] -= max(0, z[n]);
return greed(z, n+1)+max(0, z[n])*2;
}
int main() {
cin >> N;
rep(i, N) cin >> p[i];
int ans = INT_MAX;
for (int t=0; t<=p[0]; t++) {
vector<int> z = p;
debug_var(t);
z[0] -= t;
z[1] -= t;
ans = min(ans, p[0]-t + t*2 + greed(z, 1) + max(0, z[N-1]));
debug_var(ans);
}
cout << ans << endl;
}
| 0 |
//2017-10-15
//miaomiao
//
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
#define Set(a, v) memset(a, v, sizeof(a))
#define For(i, a, b) for(int i = (a); i <= (int)(b); ++i)
#define N (1000+5)
const int P = 1e9 + 7;
int Pow(LL a, LL anum){
a %= P;
int ret = 1;
while(anum){
if(anum & 1) ret = 1ll * ret * a % P;
a = 1ll * a * a % P; anum >>= 1;
}
return ret;
}
char mp[N][N];
struct Matrix{
int h[2][2];
Matrix(int op = 0){
Set(h, 0);
if(op) h[0][0] = h[1][1] = 1;
}
Matrix operator *(const Matrix &rhs)const{
Matrix ret;
For(i, 0, 1) For(j, 0, 1) For(k, 0, 1)
(ret.h[i][k] += 1ll * h[i][j] * rhs.h[j][k] % P) %= P;
return ret;
}
Matrix operator ^(LL knum){
Matrix ret(1), a = *this;
while(knum){
if(knum & 1) ret = ret * a;
a = a * a; knum >>= 1;
}
// printf("%d %d\n%d %d\n", ret.h[0][0], ret.h[0][1], ret.h[1][0], ret.h[1][1]);
return ret;
}
}a, tmp;
int main(){
int n, m;
LL rk;
scanf("%d%d%lld", &n, &m, &rk);
For(i, 1, n) scanf("%s", mp[i]+1);
int ncnt = 0, mcnt = 0, cnt = 0;
For(i, 1, n) For(j, 1, m) cnt += mp[i][j] == '#';
For(i, 1, n) ncnt += (mp[i][1]=='#' && mp[i][m]=='#');
For(i, 1, m) mcnt += (mp[1][i]=='#' && mp[n][i]=='#');
if(ncnt && mcnt) puts("1");
else if(!ncnt && !mcnt) printf("%d\n", Pow(cnt, rk-1));
else{
int rnum = 0;
if(ncnt){
For(i, 1, n) For(j, 1, m-1)
if(mp[i][j]=='#' && mp[i][j+1]=='#') --rnum;
}else{
For(i, 1, m) For(j, 1, n-1)
if(mp[j][i]=='#' && mp[j+1][i]=='#') --rnum;
}
// printf("cnt = %d rnum = %d ncnt = %d mcnt = %d\n", cnt, rnum, ncnt, mcnt);
a.h[0][0] = cnt; a.h[0][1] = rnum; a.h[1][1] = max(ncnt, mcnt);
tmp.h[0][0] = tmp.h[1][0] = 1;
printf("%d\n", (((a^(rk-1)) * tmp).h[0][0]+P) % P);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const double PI = 3.14159265359;
vector<long long> primes;
void GeneratingPrimes(int top) {
vector<bool> p(top + 1, 1);
p[0] = p[1] = 0;
for (int i = 2; i <= sqrt(top); i++) {
if (p[i]) {
for (int j = i * i; j <= top; j += i) {
p[j] = 0;
}
}
}
for (int i = 0; i < top + 1; i++)
if (p[i]) primes.push_back(i);
}
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
void EXTgcd(long long a, long long b, int& x0, int& y0) {
if (b == 0) {
x0 = 1, y0 = 0;
return;
}
EXTgcd(b, a % b, x0, y0);
long long x1 = y0;
long long y1 = x0 - (a / b) * y0;
x0 = x1;
y0 = y1;
}
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res % p;
}
double dist(double a, double b, double x, double y) {
return sqrt(pow(a - x, 2) + pow(b - y, 2));
}
double dist3(double a, double b, double c, double x, double y, double z) {
return sqrt(pow(a - x, 2) + pow(b - y, 2) + pow(c - z, 2));
}
int xadd[9] = {1, -1, 0, 1, -1, 0, 1, -1, 0};
int yadd[9] = {1, -1, 0, -1, 0, 1, 0, 1, -1};
long long const N = 100000 + 10;
long long mod = 998244353;
vector<int> ans;
bool chkCD(vector<int> v, int c, int d) {
if (v.size()) {
if (v.back() != 2) {
if (d) return 0;
}
if (d < c) return 0;
while (c--) {
v.push_back(3), v.push_back(2);
d--;
}
if (d) v.push_back(3), d--;
if (!d) {
ans = v;
return 1;
} else
return 0;
} else {
if (c > d) {
if (c - 1 > d) return 0;
for (int i = 0; i < d; i++) ans.push_back(2), ans.push_back(3);
ans.push_back(2);
return 1;
}
while (c--) {
ans.push_back(3), ans.push_back(2);
d--;
}
if (d) ans.push_back(3), d--;
return (!d);
}
}
bool chkBC(int b, vector<int> v, int c, int d) {
if (v.size()) {
if (v.back() == 0) {
if (c) return 0;
return chkCD(v, 0, d);
}
if (b > c) return 0;
for (int i = 0; i < b; i++) v.push_back(2), v.push_back(1);
if (c > b) v.push_back(2);
return chkCD(v, (c > b) ? c - b - 1 : 0, d);
} else {
if (b > c) {
if (b - 1 > c) return 0;
for (int i = 0; i < c; i++) v.push_back(1), v.push_back(2);
v.push_back(1);
return chkCD(v, 0, d);
}
for (int i = 0; i < b; i++) v.push_back(1), v.push_back(2);
if (chkCD(v, c - b, d)) return 1;
v.clear();
for (int i = 0; i < b; i++) v.push_back(2), v.push_back(1);
if (c > b) v.push_back(2);
return chkCD(v, (c == b) ? 0 : c - b - 1, d);
}
}
bool chkAB(int a, int b, int c, int d) {
if (a - 1 > b) return 0;
if (a - 1 == b) {
vector<int> v;
for (int i = 0; i < b; i++) {
v.push_back(0);
v.push_back(1);
}
v.push_back(0);
if (chkBC(0, v, c, d)) return 1;
return 0;
}
vector<int> v;
for (int i = 0; i < a; i++) v.push_back(0), v.push_back(1);
if (chkBC(b - a, v, c, d)) return 1;
v.clear();
for (int i = 0; i < a; i++) v.push_back(1), v.push_back(0);
if (b > a) v.push_back(1);
return (chkBC((b > a) ? b - a - 1 : b - a, v, c, d));
}
int a, b, c, d;
int main() {
cin >> a >> b >> c >> d;
if (!chkAB(a, b, c, d)) {
cout << "NO";
} else {
cout << "YES\n";
for (int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
const double PI = acos(-1.0);
using namespace std;
const double EPS = 1e-9;
const int N = 2e5 + 9;
int n, q, a[N], b[N];
long long sum;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i], sum += a[i];
cin >> q;
map<pair<int, int>, int> ck;
int u, t, v;
while (q--) {
cin >> u >> t >> v;
if (ck.count({u, t})) {
int x = ck[{u, t}];
b[x]--;
if (b[x] < a[x]) sum++;
}
if (v) {
ck[{u, t}] = v;
b[v]++;
if (b[v] <= a[v]) sum--;
} else
ck.erase({u, t});
cout << sum << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int mxn = 1000 * 100 + 10;
long long n, a, b;
set<long long> seti;
void Gen(long long x, long long y) {
if (x > n || y > 10) return;
seti.insert(x);
Gen(x * 10 + a, y + 1);
Gen(x * 10 + b, y + 1);
}
int main() {
cin >> n;
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++) {
if (i == j) continue;
a = i;
b = j;
Gen(0, 0);
}
cout << seti.size() - 1;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int low = 0, upp = 0;
for (int i = 0; i < s.length(); i++) {
if (isupper(s[i])) low++;
}
int ans = low;
for (int i = 0; i < s.length(); i++) {
if (isupper(s[i])) low--;
if (islower(s[i])) upp++;
if (upp + low < ans) ans = upp + low;
}
cout << ans << endl;
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
#define MAX_N 100005
typedef unsigned long long ull;
ull B=5575777;
ull ans=0;
int n,a,b;
vector<int> G[MAX_N];
unordered_map<ull,ull> mp;
ull dfs(int pos){
ull res=0;
for(int to:G[pos])res+=dfs(to);
res=res*B+1;
ans+=mp[res]++;
return res;
}
int main(){
scanf("%d",&n);
for(int i=0;i<n-1;i++){
scanf("%d %d",&a,&b);
G[a].push_back(b);
}
dfs(1);
printf("%lld\n",ans);
return 0;
}
| 0 |
#include<bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
cs int N = 2e6 + 50;
cs int INF = 1e9 + 7;
int n, m, k; char A[N], B[N];
queue<int> a, b, oa, ob;
int Fir(cs queue<int> &a){ return a.empty() ? INF : a.front(); }
void work(int x){
if(a.empty() && b.empty()) return;
int ta = Fir(a), tb = Fir(b);
if(ta == tb){
a.pop(); b.pop();
if(A[x]=='1'){
if(ta<=k) oa.push(ta), A[x]='0';
} else A[x]='1';
if(B[x]=='1'){
if(tb<=k) ob.push(tb), B[x]='0';
} else B[x]='1';
} else if(ta < tb){
a.pop(); if(A[x]=='0') A[x]='1';
else if(ta<=k) oa.push(ta), A[x]='0';
} else{
b.pop(); if(B[x]=='0') B[x]='1';
else if(tb<=k) ob.push(tb), B[x]='0';
} if(A[x]=='1' && B[x]=='1'){
int t = min(ta,tb);
if(t+1<=k) oa.push(t+1), ob.push(t+1), A[x]=B[x]='0';
} work(x);
}
int main(){
#ifdef FSYolanda
freopen("1.in","r",stdin);
#endif
scanf("%d%d%d",&n,&m,&k);
scanf("%s%s",A+1,B+1);
reverse(A+1,A+n+1);
reverse(B+1,B+m+1);
int fn=max(n,m)+k;
for(int i=n+1; i<=fn; i++) A[i]='0';
for(int i=m+1; i<=fn; i++) B[i]='0';
for(int i=1; i<=fn; i++){
if(A[i]=='1' && B[i]=='1')
A[i] = '0', B[i] = '0', oa.push(1), ob.push(1);
work(i); swap(oa,a); swap(ob,b);
}
for(int i=fn,ok=0; i>=1; i--){
if(A[i]=='1') ok=1;
if(ok) cout<<A[i];
} puts("");
for(int i=fn,ok=0; i>=1; i--){
if(B[i]=='1') ok=1;
if(ok) cout<<B[i];
} return 0;
}
| 0 |
#include <bits/stdc++.h>
const int maxn = 1000000 + 10;
const int MOD = 998244353LL;
int n;
char s[maxn];
long long fac[maxn], inv_fac[maxn];
long long sum[2][maxn];
inline long long mul(long long a, long long b) { return (a * b) % MOD; }
void add(long long& a, long long b) {
a += b;
if (a >= MOD) a -= MOD;
}
long long pow_mod(long long a, int n) {
long long ans = 1;
while (n) {
if (n & 1) ans = mul(ans, a);
a = mul(a, a);
n >>= 1;
}
return ans;
}
long long inverse(long long a) { return pow_mod(a, MOD - 2); }
long long dp[2][maxn];
void init_dp(int n) {
dp[0][0] = dp[1][0] = 1LL;
for (int i = 1; i < n; i++) {
dp[0][i] = mul(mul(dp[0][i - 1], n - i), inverse(i));
}
for (int i = 1; i <= n; i++) {
dp[1][i] = 0;
add(dp[1][i], dp[0][i - 1]);
add(dp[1][i], dp[0][i]);
}
for (int i = 1; i <= n; i++) {
if (i < n) add(dp[0][i], dp[0][i - 1]);
add(dp[1][i], dp[1][i - 1]);
}
}
int main() {
scanf("%s", s);
n = strlen(s);
long long ans = 0;
int a, b, c, d;
a = b = c = d = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '?')
d++;
else if (s[i] == ')')
b++;
}
int q = d;
init_dp(d);
for (int i = 0; i <= n; i++) {
if (s[i] == ')')
b--;
else if (s[i] == '?')
d--;
int k = b + d - a - 1;
if (k >= 0) {
if (s[i] == '(') {
if (k > q) k = q;
add(ans, dp[1][k]);
} else if (s[i] == '?') {
if (k > q - 1) k = q - 1;
add(ans, dp[0][k]);
}
}
if (s[i] == '(')
a++;
else if (s[i] == '?')
c++;
}
printf("%lld\n", ans);
return 0;
}
| 4 |
#include<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
int n;
int v[17];
int dfs(int pos,int sum){
if(pos==17){
if(sum==n)return 1;
else return 0;
}
int res=0;
for(int i=0;sum+v[pos]*i<=n;i++)res+=dfs(pos+1,sum+v[pos]*i);
return res;
}
int main(){
for(int i=0;i<17;i++)v[i]=(i+1)*(i+1);
while(cin>>n,n)cout<<dfs(0,0)<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int N = 100005;
int n, m, l, r, x, t;
int ql, qr;
long long a[N];
pair<long long, long long> b[N];
long long fibMat[64][2][2];
pair<long long, long long> seg_tree[N << 2];
long long lazy[N << 3];
inline void push(int id) {
long long ans = seg_tree[id].first, ans1 = seg_tree[id].second, x, x1;
long long num = lazy[id];
for (int i = 0; i < 64 && (num >> i); i++) {
if (num & (1LL << i)) {
x = ans, x1 = ans1;
ans1 = (fibMat[i][0][0] * x1 + fibMat[i][0][1] * x) % MOD;
ans = (fibMat[i][1][0] * x1 + fibMat[i][1][1] * x) % MOD;
}
}
seg_tree[id] = {ans, ans1};
lazy[id << 1] += num, lazy[id << 1 | 1] += num;
lazy[id] = 0;
}
inline void build(int id = 1, int l = 0, int r = n) {
if (r - l < 2) {
seg_tree[id] = b[l];
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid, r);
seg_tree[id].first =
(seg_tree[id << 1].first + seg_tree[id << 1 | 1].first) % MOD;
seg_tree[id].second =
(seg_tree[id << 1].second + seg_tree[id << 1 | 1].second) % MOD;
}
inline void modify(int val, int id = 1, int l = 0, int r = n) {
if (lazy[id]) push(id);
if (ql >= r || l >= qr) return;
if (ql <= l && r <= qr) {
lazy[id] += val;
push(id);
return;
}
int mid = (l + r) >> 1;
modify(val, id << 1, l, mid);
modify(val, id << 1 | 1, mid, r);
seg_tree[id].first =
(seg_tree[id << 1].first + seg_tree[id << 1 | 1].first) % MOD;
seg_tree[id].second =
(seg_tree[id << 1].second + seg_tree[id << 1 | 1].second) % MOD;
}
inline long long query(int id = 1, int l = 0, int r = n) {
if (lazy[id]) push(id);
if (ql >= r || l >= qr) return 0;
if (ql <= l && r <= qr) return seg_tree[id].first;
int mid = (l + r) >> 1;
return (query(id << 1, l, mid) + query(id << 1 | 1, mid, r)) % MOD;
}
void pre() {
fibMat[0][0][1] = fibMat[0][1][1] = fibMat[0][1][0] = 1LL;
for (int x = 1; x < 64; x++) {
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++)
fibMat[x][i][j] += (fibMat[x - 1][i][k] * fibMat[x - 1][k][j]) % MOD;
fibMat[x][i][j] %= MOD;
}
}
}
inline pair<long long, long long> getFib(long long num) {
long long ans = 1, ans1 = 0, x, x1;
num--;
for (int i = 0; i < 32; i++) {
if (num & (1LL << i)) {
x = ans, x1 = ans1;
ans1 = (fibMat[i][0][0] * x1 + fibMat[i][0][1] * x) % MOD;
ans = (fibMat[i][1][0] * x1 + fibMat[i][1][1] * x) % MOD;
}
}
return {ans, ans1};
}
int main() {
pre();
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%lld", a + i);
b[i] = getFib(a[i]);
}
build();
while (m--) {
scanf("%d %d %d", &t, &l, &r);
ql = --l, qr = r;
if (t == 1) {
scanf("%d", &x);
modify(x);
} else {
printf("%lld\n", (query() + MOD) % MOD);
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int del(int n) {
double q = n;
if (n % 2 == 0) return 2;
for (int i = 3; i < sqrt(q) + 1; i = i + 2) {
if (n % i == 0) return i;
}
return 1;
}
int main() {
int n;
cin >> n;
long int s;
s = n;
int de = n;
while (n != 1) {
de = del(n);
n = n / de;
if (de == 1) {
s++;
cout << s;
return 0;
};
s += n;
}
cout << s;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b;
cin >> a >> b;
sort(b.rbegin(), b.rend());
for (int i = 0, j = 0; i < a.size() && j < b.size(); i++)
if (b[j] > a[i]) a[i] = b[j], j++;
cout << a << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int inp[200009];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> inp[i];
long long c, d, cnt;
c = d = cnt = 0;
for (int i = n; i >= 1; i--) {
if (inp[i])
c += cnt;
else
cnt++;
}
cnt = 0;
for (int i = 1; i <= n; i++) {
if (inp[i])
cnt++;
else
d += cnt;
}
if (c > d) c = d;
cout << c << '\n';
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long a[100000], sum[100000], k, n;
bool can(int l, int pos) {
long long need = (pos - l + 1) * a[pos];
long long have = sum[l] - ((pos < n - 1) ? sum[pos + 1] : 0);
return need - have <= k;
}
int find(int l, int r, int pos) {
while (l <= r) {
int mid = (l + r) / 2;
if (can(mid, pos)) {
r = mid - 1;
} else {
l = mid + 1;
}
}
return l;
}
int main() {
int i, res = -1, mx = -1;
cin >> n >> k;
for (i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
for (i = n - 1; i >= 0; i--) {
sum[i] = a[i];
if (i < n - 1) {
sum[i] += sum[i + 1];
}
}
for (i = 0; i < n; i++) {
int l = find(0, i, i);
if (i - l + 1 > mx) {
mx = i - l + 1;
res = a[i];
}
}
cout << mx << " " << res << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int _I() {
int x;
scanf("%d", &x);
return x;
}
long long _ILL() {
long long x;
scanf("%lld", &x);
return x;
}
struct node {
int a, b;
long long c;
int d;
node(){};
node(int a, long long c) {
this->a = a;
this->c = c;
}
bool operator<(const node &R) const { return c > R.c; }
};
long long ar[200005];
void solve() {
int n = _I();
for (int i = 0; i < n; i++) {
cin >> ar[i];
}
long long pos = LLONG_MIN, neg = LLONG_MIN;
bool p = 0, nn = 0;
long long sum1 = 0, sum2 = 0;
for (int i = 0; i < n;) {
if (p == 0) {
neg = LLONG_MIN;
while (ar[i] < 0 && i < n) {
neg = max(neg, ar[i]);
i++;
}
p = 1;
if (neg == LLONG_MIN) continue;
sum1 += neg;
}
if (p == 1) {
neg = LLONG_MIN;
while (ar[i] > 0 && i < n) {
neg = max(neg, ar[i]);
i++;
}
p = 0;
if (neg == LLONG_MIN) continue;
sum2 += neg;
}
}
long long ans1 = sum1 + sum2;
sum1 = 0, sum2 = 0;
p = 0;
for (int i = 0; i < n;) {
if (p == 0) {
neg = LLONG_MIN;
while (ar[i] > 0 && i < n) {
neg = max(neg, ar[i]);
i++;
}
p = 1;
if (neg == LLONG_MIN) continue;
sum1 += neg;
}
if (p == 1) {
neg = LLONG_MIN;
while (ar[i] < 0 && i < n) {
neg = max(neg, ar[i]);
i++;
}
p = 0;
if (neg == LLONG_MIN) continue;
sum2 += neg;
}
}
long long ans2 = sum1 + sum2;
printf("%lld\n", max(ans2, ans1));
}
int main() {
int t = _I();
while (t--) solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
int main(void) {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
int n;
std::string str;
std::cin >> n >> str;
int cur = 0;
std::vector<int> answer;
for (int i = 0; i < n; ++i) {
if (str[i] == 'B') {
++cur;
continue;
}
if (cur > 0) {
answer.push_back(cur);
cur = 0;
}
}
if (cur > 0) {
answer.push_back(cur);
}
std::cout << answer.size() << '\n';
for (int i = 0; i < answer.size(); ++i) {
std::cout << answer[i] << ' ';
}
std::cout << '\n';
return 0;
}
| 1 |
#include <bits/stdc++.h>
const int INFI = (1 << 29);
const long long INFL = (1LL << 62);
using namespace std;
long long f[1000010];
void solve() {
long long n, k, d, c = 0, r = INFL;
cin >> n >> k >> d;
vector<long long> v(n);
for (long long i = 0; i < n; i++) cin >> v[i];
for (long long i = 0; i < n; i++) {
f[v[i]]++;
c += (f[v[i]] == 1);
if (i >= d) {
f[v[i - d]]--;
c -= (!f[v[i - d]]);
}
if (i >= (d - 1)) r = min(r, c);
}
cout << r << "\n";
for (long long i = 0; i < n; i++) f[v[i]] = 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long casos;
cin >> casos;
while (casos--) solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 20;
long long menu[MAX] = {0}, sat[MAX][MAX] = {0}, dp[1 << MAX][MAX];
int n, m, k;
long long dfs(int a, int b, int p) {
if (dp[a][b] != -1) {
return dp[a][b];
}
if (p == m) {
return 0;
}
long long ans = 0;
for (int i = 0; i < n; i++) {
if (a & (1 << i)) {
continue;
}
ans = max(ans, dfs(a | (1 << i), i, p + 1) + menu[i] + sat[b][i]);
}
return dp[a][b] = ans;
}
int main() {
scanf("%d%d%d", &n, &m, &k);
memset(dp, -1, sizeof(dp));
memset(menu, 0, sizeof(menu));
memset(sat, 0, sizeof(sat));
for (int i = 0; i < n; i++) {
scanf("%d", &menu[i]);
sat[i][i] = menu[i];
}
for (int i = 0; i < k; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
sat[a - 1][b - 1] = c;
}
printf("%lld\n", dfs(0, 19, 0));
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
int n;
int a[maxn];
int l[maxn], r[maxn];
int main() {
ios::sync_with_stdio(false);
cin >> n;
int ans = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] > a[i - 1])
l[i] = l[i - 1] + 1;
else
l[i] = 1;
ans = max(ans, l[i]);
}
r[n] = 1;
for (int i = n - 1; i >= 1; i--) {
if (a[i] < a[i + 1])
r[i] = r[i + 1] + 1;
else
r[i] = 1;
}
for (int i = 2; i < n; i++) {
if (a[i + 1] > a[i - 1]) ans = max(ans, l[i - 1] + r[i + 1]);
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, pre, now, ans, a[1000005];
string s;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> s;
n = (int)s.size();
for (int i = 0; i < n; i++) a[i + 1] = s[i] - '0';
pre = n + 1;
for (int i = n - 2; i > 0; i--) {
now = pre;
for (int k = 1; i + 2 * k < pre; k++) {
if (a[i] == a[i + k] && a[i] == a[i + 2 * k]) {
now = i + 2 * k;
pre = now;
break;
}
}
ans += n - now + 1;
}
cout << ans;
return 0;
}
| 2 |
#include<bits/stdc++.h>
#define N 200005
using namespace std;
int now=0,n,cnt,num;
int a[N],b[N],fa[N];
int in[N],out[N];
int sz[N];
map<int,int> mp;
int get(int x){
return fa[x]==x?x:fa[x]=get(fa[x]);
}
void add(int x,int y){
in[y]++; out[x]++; cnt++;
x=get(x); y=get(y);
if (x!=y) sz[y]+=sz[x],fa[x]=y;
}
int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++){
scanf("%d",&a[i]);
now^=a[i];
if (!mp[a[i]]) mp[a[i]]=++num;
}
for (int i=1;i<=n;i++){
scanf("%d",&b[i]);
if (!mp[b[i]]) mp[b[i]]=++num;
}
if (!mp[now]) mp[now]=++num;
++in[mp[now]];
for (int i=1;i<=num;i++) sz[i]=1,fa[i]=i;
for (int i=1;i<=n;i++)
if (a[i]!=b[i])
add(mp[b[i]],mp[a[i]]);
int dif=0;
for (int i=1;i<=num;i++)
if (out[i]>in[i]||in[i]+1<out[i])
return puts("-1"),0;
else if (out[i]!=in[i]) ++dif;
if (dif>1) return puts("-2"),0;
for (int i=1;i<=num;i++)
if (get(i)==i){
if (sz[i]==1) cnt+=(mp[now]==i);
else ++cnt;
}
printf("%d\n",cnt-1);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using lld = long double;
using ll = long long int;
using ar = vector<ll>;
using mat = vector<vector<ll>>;
mat cn(ll n, ll m) { return vector<vector<ll>>(n, vector<ll>(m)); }
ll const MOD = 1000000007;
template <class T>
inline bool chmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
template <class T>
inline bool chmadp2(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
template <class T>
inline void add(T &a, T b) {
a += b;
a >= MOD ? a - MOD : a;
}
template <class T>
inline void sub(T &a, T b) {
a -= b;
a < 0 ? a + MOD : a;
}
template <class T>
inline void mul(T &a, T b) {
return (a * b) % MOD;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
for (auto &dp2 : v) out << dp2 << ' ';
return out;
}
template <class T>
void remDup(vector<T> &v) {
sort((v).begin(), (v).end());
v.erase(unique((v).begin(), (v).end()), end(v));
}
bool comp1(char &s1, char &s2) { return s1 > s2; }
bool comp2(const pair<ll, pair<ll, ll>> &a, const pair<ll, pair<ll, ll>> &b) {
if (a.first > b.first) return 1;
if (a.first == b.first && a.second.second > b.second.second) return 1;
return 0;
}
class Pair {
public:
ll first, second;
};
bool comp3(const Pair &a, const Pair &b) {
if (a.first > b.first) return 1;
return 0;
}
class Trips {
public:
ll first, second, third;
};
bool comp4(const Trips &a, const Trips &b) {
if (a.third > b.third) return 1;
return 0;
}
void readArray(ll a[], int n) {
for (ll i = 0; i < n; i++) cin >> a[i];
}
ll const inf = 1e9 + 7;
ll const maxn = 5e5 + 1;
bool comp(string &a, string &b) {
ll m = a.size();
for (int i = 0; i < m; i++) {
if (a[i] != b[i]) {
if (i & 1) {
if (a[i] < b[i])
return 0;
else
return 1;
} else {
if (a[i] < b[i])
return 1;
else
return 0;
}
}
}
return 0;
}
void solve() {
ll t1 = 1;
for (ll tt = 1; tt <= t1; tt++) {
ll n, m;
cin >> n >> m;
unordered_map<string, ll> pos;
string s[n];
for (ll i = 0; i < n; i++) {
cin >> s[i];
pos[s[i]] = i + 1;
}
sort(s, s + n, comp);
for (ll i = 0; i < n; i++) cout << pos[s[i]] << " ";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
return 0;
}
| 1 |
#include <cmath>
#include <iostream>
using namespace std;
#define PI 3.1415926535
#define EPS 0.00000000001
int main()
{
int n,a;
int a1,a2;
double h1,h2;
while(cin >> n, n)
{
a1=a2=0;
h1=h2=0;
while(--n)
{
cin >> a;
a1 += a;
h1 += sin(a*PI/180.0);
}
cin >> n;
while(--n)
{
cin >> a;
a2 += a;
h2 += sin(a*PI/180.0);
}
h1 += sin((360-a1)*PI/180.0);
h2 += sin((360-a2)*PI/180.0);
if(-EPS<h1-h2 && h1-h2<EPS)
{
cout << 0 << endl;
}
else if(h1>h2)
{
cout << 1 << endl;
}
else
{
cout << 2 << endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, countx = 0, countX = 0;
string s;
cin >> n >> s;
a = n / 2;
for (int i = 0; i < n; i++) {
if (s.substr(i, 1) == "x")
countx++;
else
countX++;
}
int sum = abs(a - countx);
cout << sum << endl;
int cnt;
string goal, goal1;
if (countx > countX) {
goal = "X", goal1 = "x";
cnt = countX;
} else {
goal = "x", goal1 = "X";
cnt = countx;
}
if (cnt == a) {
cout << s;
return 0;
}
for (int i = 0; i < n; i++) {
if (cnt == a) {
cout << s;
return 0;
}
if (s.substr(i, 1) == goal1) {
s.replace(i, 1, goal);
cnt++;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10, size = 1 << 20, mod = 998244353, inf = 2e9;
const long long INF = 1e15;
template <class o>
void qr(o& x) {
char c = getchar();
x = 0;
int f = 1;
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
x *= f;
}
template <class o>
void qw(o x) {
if (x / 10) qw(x / 10);
putchar(x % 10 + '0');
}
template <class o>
void pr1(o x) {
if (x < 0) x = -x, putchar('-');
qw(x);
putchar(' ');
}
template <class o>
void pr2(o x) {
if (x < 0) x = -x, putchar('-');
qw(x);
putchar('\n');
}
long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long power(long long a, long long b = mod - 2, long long p = mod) {
long long c = 1;
while (b) {
if (b & 1) c = c * a % p;
b /= 2;
a = a * a % p;
}
return c;
}
template <class o>
void cmax(o& x, o y) {
if (x < y) x = y;
}
template <class o>
void cmin(o& x, o y) {
if (x > y) x = y;
}
template <typename t1, typename t2>
void ad(t1& x, t2 y) {
x += y;
if (x >= mod) x -= mod;
}
template <typename t1, typename t2>
void dl(t1& x, t2 y) {
x -= y;
if (x < 0) x += mod;
}
template <typename T>
struct BIT {
T* c;
int n;
BIT(int _n) : n(_n) {
c = new T[n];
c--;
for (int i = 1; i <= n; i++) c[i] = T(0);
}
void add(int x, T y) {
for (; x <= n; x += x & -x) c[x] = c[x] + y;
}
T sum(int x) {
T y = T(0);
for (; x; x &= x - 1) y = y + c[x];
return y;
}
};
long long jc[N], inv[N];
void jc_init(int n) {
jc[0] = 1;
for (int i = 1; i <= n; i++) jc[i] = jc[i - 1] * i % mod;
inv[n] = power(jc[n]);
for (int i = n; i; i--) inv[i - 1] = inv[i] * i % mod;
}
long long C(int x, int y) {
if (x < y || y < 0) return 0;
return jc[x] * inv[y] % mod * inv[x - y] % mod;
}
int n, m, dif, id[150];
char hy[] = "0AGCT";
string s[N], ans[N], out[N];
void upd(int x) {
if (x < dif) {
dif = x;
for (int i = 0; i <= n - 1; i++) out[i] = ans[i];
}
}
void solve(int* a) {
ans[0][0] = a[0];
ans[0][1] = a[1];
ans[1][0] = a[2];
ans[1][1] = a[3];
int sum = 0;
for (int i = 0; i <= 1; i++) {
for (int j = 2; j <= m - 1; j++) ans[i][j] = ans[i][j - 2];
for (int j = 0; j <= m - 1; j++) sum += ans[i][j] != s[i][j];
}
for (int i = 2; i <= n - 1; i++) {
int u = 0, v = 0;
for (int j = 0; j <= m - 1; j++) {
char c = s[i][j];
u += c != ans[i - 2][j & 1];
v += c != ans[i - 2][j & 1 ^ 1];
}
int o = (u > v);
for (int j = 0; j <= m - 1; j++) ans[i][j] = ans[i - 2][j & 1 ^ o];
sum += min(u, v);
}
upd(sum);
sum = 0;
for (int j = 0; j <= 1; j++) {
for (int i = 2; i <= n - 1; i++) ans[i][j] = ans[i - 2][j];
for (int i = 0; i <= n - 1; i++) sum += ans[i][j] != s[i][j];
}
for (int j = 2; j <= m - 1; j++) {
int u = 0, v = 0;
for (int i = 0; i <= n - 1; i++) {
char c = s[i][j];
u += c != ans[i & 1][j - 2];
v += c != ans[i & 1 ^ 1][j - 2];
}
int o = (u > v);
for (int i = 0; i <= n - 1; i++) ans[i][j] = ans[i & 1 ^ o][j - 2];
sum += min(u, v);
}
upd(sum);
}
void solve() {
qr(n);
qr(m);
dif = inf;
id['A'] = 1;
id['G'] = 2;
id['C'] = 3;
id['T'] = 4;
for (int i = 0; i <= n - 1; i++) {
cin >> s[i];
for (auto& c : s[i]) c = id[(int)c];
ans[i].resize(m);
}
int a[] = {1, 2, 3, 4};
do solve(a);
while (next_permutation(a, a + 4));
for (int i = 0; i <= n - 1; i++) {
for (auto& c : out[i]) c = hy[(int)c];
puts(out[i].c_str());
}
}
int main() {
int T = 1;
while (T--) solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int num[] = {4, 8, 15, 16, 23, 42};
int a, b, c;
int d, e, f;
a = b = c = d = e = f = 0;
printf("? 1 2\n");
fflush(stdout);
int rps1 = 0;
scanf("%d", &rps1);
printf("? 2 3\n");
fflush(stdout);
int rps2 = 0;
scanf("%d", &rps2);
for (int i = 0; i < 6; i++) {
if (rps1 % num[i] != 0) continue;
if (rps2 % num[i] != 0) continue;
int t1 = rps1 / num[i];
int t2 = rps2 / num[i];
if (t1 == t2 || t1 == num[i] || t2 == num[i]) continue;
int cnt = 0;
for (int j = 0; j < 6; j++) {
if (t1 == num[j]) cnt++;
if (t2 == num[j]) cnt++;
}
if (cnt <= 1) continue;
a = t1;
b = num[i];
c = t2;
num[i] = INT_MAX;
for (int j = 0; j < 6; j++) {
if (num[j] == t1 || num[j] == t2) num[j] = INT_MAX;
}
}
printf("? 4 5\n");
fflush(stdout);
scanf("%d", &rps1);
printf("? 5 6\n");
fflush(stdout);
scanf("%d", &rps2);
for (int i = 0; i < 6; i++) {
if (rps1 % num[i] != 0) continue;
if (rps2 % num[i] != 0) continue;
int t1 = rps1 / num[i];
int t2 = rps2 / num[i];
if (t1 == t2 || t1 == num[i] || t2 == num[i]) continue;
int cnt = 0;
for (int j = 0; j < 6; j++) {
if (t1 == num[j]) cnt++;
if (t2 == num[j]) cnt++;
}
if (cnt <= 1) continue;
d = t1;
e = num[i];
f = t2;
num[i] = INT_MAX;
for (int j = 0; j < 6; j++) {
if (num[j] == t1 || num[j] == t2) num[j] = INT_MAX;
}
}
printf("! %d %d %d %d %d %d\n", a, b, c, d, e, f);
fflush(stdout);
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
template <class T>
inline T sqr(T x) {
return x * x;
}
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)5e5 + 10;
int x[maxn], s[maxn], n;
bool solve(double t) {
double ma = -1e100;
for (int i = 0; i < n; i++) {
double X = x[i] + s[i] * t;
if (s[i] > 0) {
ma = max(ma, X);
} else {
if (ma > X) return true;
}
}
return false;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) scanf("%d%d", &x[i], &s[i]);
double l = 0.0;
double r = 1e10;
for (int i = 0; i < 100; i++) {
double mid = (l + r) / 2.0;
if (solve(mid))
r = mid;
else
l = mid;
}
if (l > 1e9)
printf("-1\n");
else
printf("%.9lf\n", l);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LLINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1000000007;
const double EPS = 1e-8;
const long double EULER = 2.71828182845904523536;
const long double PI = 3.14159265358979323846;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
string s;
while (cin >> n >> k >> s) {
string ans = s;
for (int i = 0; i < n; i++) ans[i] = ans[i % k];
for (int i = 0; i < n; i++) {
if (ans[i] > s[i]) break;
if (ans[i] < s[i]) {
int i = k - 1;
while (ans[i] == '9') {
ans[i] = '0';
i--;
}
ans[i]++;
for (int i = 0; i < n; i++) ans[i] = ans[i % k];
break;
}
}
cout << n << endl << ans << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
char s[100007], t[100007];
int mark[100007];
const int mod = 1e9 + 7;
int dp[100007];
int sum[100007];
void get_next(char p[], int next[]) {
int i = 0, k = -1, len = strlen(p);
next[0] = -1;
while (i < len)
if (k == -1 || p[i] == p[k])
i++, k++, next[i] = k;
else
k = next[k];
}
void match(char s[], char p[]) {
memset(mark, 0, sizeof(mark));
int next[100007];
get_next(p, next);
int len1 = strlen(s);
int len2 = strlen(p);
int i = 0, j = 0;
while (i < len1) {
if (j == -1 || s[i] == p[j])
i++, j++;
else
j = next[j];
if (j == len2) mark[i] = i - len2 + 1;
}
}
int main() {
while (~scanf("%s", s)) {
scanf("%s", t);
match(s, t);
sum[0] = dp[0] = 0;
int n = strlen(s);
for (int i = 1; i <= n; i++)
if (!mark[i]) mark[i] = mark[i - 1];
for (int i = 1; i <= n; i++) {
dp[i] = dp[i - 1];
int l = mark[i];
if (!l) continue;
dp[i] += (sum[l - 1] + l) % mod;
dp[i] %= mod;
sum[i] = sum[i - 1] + dp[i];
sum[i] %= mod;
}
printf("%d\n", dp[n]);
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool check(const vector<int> matches) {
for (int i = 0; i < (int)(matches.size()); i++)
if (matches[i] > 0) return true;
return false;
}
int main() {
string S;
while (cin >> S) {
int N;
cin >> N;
vector<string> B;
B.resize(N);
for (int i = 0; i < (int)(N); i++) cin >> B[i];
static bool startPos[100000][10];
static bool endPos[1000000][10];
memset(startPos, false, sizeof(startPos));
memset(endPos, false, sizeof(endPos));
for (int i = 0; i < (int)(S.size()); i++) {
for (int j = 0; j < (int)(N); j++) {
if (i + B[j].size() - 1 < S.size()) {
bool match = true;
for (int k = 0; k < (int)(B[j].size()); k++) {
if (S[i + k] != B[j][k]) {
match = false;
break;
}
}
if (match) {
startPos[i][j] = true;
endPos[i + B[j].size() - 1][j] = true;
}
}
}
}
int len = 0, pos = 0;
int left = 0, right = 0;
vector<int> matches(N, 0);
while (right < S.size()) {
while (right < S.size() && !check(matches)) {
if (len < right - left) {
len = right - left;
pos = left;
}
for (int i = 0; i < (int)(N); i++)
if (endPos[right][i]) matches[i]++;
right++;
}
while (left < S.size() && check(matches)) {
for (int i = 0; i < (int)(N); i++)
if (startPos[left][i]) matches[i]--;
left++;
}
}
if (!check(matches) && len < right - left) {
len = right - left;
pos = left;
}
cout << len << " " << pos << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
inline int Rand() { return (rand() << 16) | rand(); }
using namespace std;
const int N = 200005;
const double eps = 1e-9;
int s[N];
int main() {
ios_base::sync_with_stdio(false);
int n, a, b, c;
cin >> n;
cout << "? 1 2\n";
cin >> a;
cout << "? 1 3\n";
cin >> b;
cout << "? 2 3\n";
cin >> c;
s[2] = -((b - a - c) / (2));
s[1] = a - s[2];
s[3] = c - s[2];
int i = 4;
while (i <= n) {
int x;
cout << "? 1 " << i << "\n";
cin >> x;
s[i] = x - s[1];
i++;
}
cout << "!";
for (int i = 1; i <= n; ++i) cout << " " << s[i];
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool comp(int a, int b) { return a > b; }
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int in;
vector<bool> p(n, 0), vis(n, 0);
for (int i = 0; i < n; i++) {
int k;
cin >> k;
for (int j = 0; j < k; j++) {
cin >> in;
in--;
if (p[in] || vis[i])
continue;
else {
p[in] = 1;
vis[i] = 1;
}
}
}
bool band = false;
int in1, in2;
in1 = in2 = -1;
for (int i = 0; i < n; i++) {
if (!vis[i]) {
in1 = i;
break;
}
}
for (int i = 0; i < n; i++) {
if (!p[i]) {
in2 = i;
break;
}
}
if (in1 != -1) {
cout << "IMPROVE\n";
cout << in1 + 1 << " " << in2 + 1 << "\n";
} else
cout << "OPTIMAL\n";
}
return 0;
}
| 2 |
#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
struct BitVector{
vector<uint64_t> v;
vector<int> r;
BitVector(){}
void build(){
r.assign(v.size() + 1, 0);
for(int i = 0; i < v.size(); ++i)
r[i + 1] = r[i] + __builtin_popcountll(v[i]);
}
bool access(int x){
return (v[x >> 6] >> (x & 63)) & 1;
}
// [0, x)の1の出現回数
int rank(int x){
return r[x >> 6] + __builtin_popcountll(v[x >> 6] & ((1uLL << (x & 63)) - 1));
}
int rank(int x, bool fl){
return fl ? rank(x) : x - rank(x);
}
};
template <typename T, int W>
struct WaveletMatrix{
array<BitVector, W> bv;
array<int, W> zero_cnt;
WaveletMatrix(vector<T>& a){
int n = a.size();
vector<T> v(a);
for(int i = W - 1; i >= 0; --i){
vector<uint64_t> b((n >> 6) + 1, 0);
vector<T> v1, v2;
for(int j = 0; j < n; ++j){
((v[j] >> i) & 1 ? v2 : v1).push_back(v[j]);
b[j >> 6] |= uint64_t((v[j] >> i) & 1) << (j & 63);
}
for(int j = 0; j < v.size(); ++j)
v[j] = (j < v1.size() ? v1[j] : v2[j - v1.size()]);
bv[i].v = move(b);
bv[i].build();
zero_cnt[i] = bv[i].rank(n, 0);
}
}
// [l, r)内のxの数
int count(int l, int r, T x){
for(int i = W - 1; i >= 0; --i){
bool fl = (x >> i) & 1;
int st = bv[i].rank(l, fl);
int en = bv[i].rank(r, fl);
l = (fl ? zero_cnt[i] : 0) + st;
r = (fl ? zero_cnt[i] : 0) + en;
}
return r - l;
}
// [l, r)内で[0, x)を満たす値の数
int count_lower(int l, int r, T x){
int cnt = 0;
for(int i = W - 1; i >= 0; --i){
bool fl = (x >> i) & 1;
int st = bv[i].rank(l, fl);
int en = bv[i].rank(r, fl);
if(fl){
st += zero_cnt[i];
en += zero_cnt[i];
cnt += (bv[i].rank(r, 0) - bv[i].rank(l, 0));
}
l = st, r = en;
}
return cnt;
}
// [l, r)内で[x, y)を満たす値の数
int count_range(int l, int r, T x, T y){
return count_lower(l, r, y) - count_lower(l, r, x);
}
// 0-indexedでk番目に小さいものを返す
T kth_min(int l, int r, int k){
T ans = 0;
for(int i = W - 1; i >= 0; --i){
int st = bv[i].rank(l, 0);
int en = bv[i].rank(r, 0);
if(en - st <= k){
k -= en - st;
l = zero_cnt[i] + bv[i].rank(l, 1);
r = zero_cnt[i] + bv[i].rank(r, 1);
ans |= (1uLL << i);
}
else{
l = st, r = en;
}
}
return ans;
}
// [l, r)でのx以上最小値
pair<T, bool> predecessor(int l, int r, T x){
int idx = count_lower(l, r, x);
if(idx == r - l){
return make_pair((1uLL << W) - 1, false);
}
return make_pair(kth_min(l, r, idx), true);
}
// [l, r)でのx以下最大値
pair<T, bool> successor(int l, int r, T x){
int idx = count_lower(l, r, x + 1);
if(idx == 0)
return make_pair(0, false);
return make_pair(kth_min(l, r, idx - 1), true);
}
};
signed main(){
int n, q;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; ++i)
cin >> a[i];
cin >> q;
WaveletMatrix<int, 31> wm(a);
for(int i = 0; i < q; ++i){
int l, r, k;
cin >> l >> r >> k;
auto res = wm.predecessor(l, r + 1, k);
auto res2 = wm.successor(l, r + 1, k);
cout << min(res.second ? res.first - k : INT_MAX, res2.second ? k - res2.first : INT_MAX) << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename V>
void bugp(const pair<T, V> &x) {
cerr << '{' << x.first << ", " << x.second << '}' << endl;
}
template <typename T, typename U, typename V>
void bugpp(const pair<T, pair<U, V> > &x) {
cerr << '{' << x.first << ", {" << x.second.first << ", " << x.second.second
<< "}}" << endl;
}
template <typename T>
bool maximize(T &x, const T &y) {
if (x < y) {
x = y;
return 1;
}
return 0;
}
template <typename T>
bool minimize(T &x, const T &y) {
if (x > y) {
x = y;
return 1;
}
return 0;
}
const int N = 100010;
int n, arr[N], f[N], g[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
int sum = 0;
for (int i = 1, _n = (n); i <= _n; ++i) {
cin >> arr[i];
sum += arr[i];
f[i] = max(f[i - 1] + arr[i], -sum);
}
sum = 0;
int ans = f[n];
for (int i = (n); i > 0; --i) {
sum += arr[i];
g[i] = max(g[i + 1] + arr[i], -sum);
maximize(ans, g[i] + f[i - 1]);
}
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int z[10];
int main() {
int i = 0, A, B, a, n, tmp;
string sB;
cin >> a >> sB;
if (a == 0) z[0] = 1;
while (a > 0) {
z[a % 10]++;
a /= 10;
}
A = 0;
for (i = 1; i < 10; i++)
if (z[i]) {
A = i;
z[i]--;
break;
}
for (i = 0; i < 10; i++)
while (z[i]) {
A = A * 10 + i;
z[i]--;
}
i = 0;
B = 0;
while (sB[i]) {
B = B * 10 + (sB[i] - '0');
i++;
}
if (A == B) {
if (A == 0 && (sB[1] == 0))
cout << "OK" << endl;
else if (sB[0] != '0')
cout << "OK" << endl;
else
cout << "WRONG_ANSWER" << endl;
} else
cout << "WRONG_ANSWER" << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
void file() {}
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
}
int main() {
file();
fast();
int n;
cin >> n;
int x = (n + 1) / 2 + ((n + 1) & 1);
cout << x << "\n";
int cnt = 0;
for (int i = 1; i <= x; i++) {
for (int j = 1; j <= x; j++) {
if (i > 1) j = x;
cout << i << ' ' << j << "\n";
cnt++;
if (cnt == n) return 0;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int inv2 = 500000004;
int sum[200010];
long long mul[200010];
int a[200010];
long long inv[200010];
int n;
struct BIT {
long long T[200010];
BIT() {memset(T, 0, sizeof(T));}
void modify(int x, long long v) {
while (x <= n) (T[x] += v) %= mod, x += x & -x;
}
long long query(int x) {
long long res = 0;
while (x) (res += T[x]) %= mod, x -= x & -x;
return res;
}
};
BIT T1, T2, T3, T;
int zero[200010], num[200010];
long long power(long long a, int b) {
if (!b) return 1;
long long tmp = power(a, b >> 1);
return b & 1 ? tmp * tmp % mod * a % mod : tmp * tmp % mod;
}
int get_left(int x) {
int l = 0, r = x;
while (l < r) {
int mid = l + r >> 1;
if (num[mid] == num[x]) r = mid;
else l = mid + 1;
}
return l;
}
int get_right(int x) {
int l = x, r = n + 1;
while (l < r) {
int mid = l + r >> 1;
if (num[mid] == num[x]) l = mid + 1;
else r = mid;
}
return l;
}
int main () {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
sum[a[i]]++;
}
for (int i = n; i >= 1; i--) {
sum[i] += sum[i + 1];
}
long long all = 1;
mul[0] = 1;
for (int i = 1; i <= n; i++) {
mul[i] = sum[i] - (n - i);
(all *= mul[i]) %= mod;
if (mul[i] == 0) {
printf("0\n");
return 0;
}
mul[i] = power(mul[i], mod - 2) * (mul[i] - 1) % mod;
if (mul[i]) (mul[i] *= mul[i - 1]) %= mod;
else mul[i] = mul[i - 1], num[i] = 1;
num[i] += num[i - 1];
inv[i] = power(mul[i], mod - 2);
}
if (num[n] == n) {
long long ans = 0;
for (int i = n; i >= 1; i--) {
ans += T.query(a[i] - 1);
T.modify(a[i], 1);
}
printf("%lld\n", ans % mod);
return 0;
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
int pos, sum;
// < a[i]
pos = get_left(a[i]);
sum = T1.query(a[i] - 1) - T1.query(pos);
ans += all * mul[a[i]] % mod * (T3.query(a[i] - 1) - T3.query(pos) + mod) % mod * inv2 % mod;
// == a[i]
sum = T1.query(a[i]) - T1.query(a[i] - 1);
ans += all * inv2 % mod * sum % mod;
// > a[i]
pos = get_right(a[i]) - 1;
sum = T1.query(n) - T1.query(a[i]);
ans += all * sum % mod - all * inv[a[i]] % mod * (T2.query(pos) - T2.query(a[i]) + mod) % mod * inv2 % mod + mod;
T1.modify(a[i], 1), T2.modify(a[i], mul[a[i]]), T3.modify(a[i], inv[a[i]]);
ans %= mod;
}
printf("%lld\n", ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int Z = (int)2e5 + 111;
const int inf = (int)1e9 + 111;
const long long llinf = (long long)1e18 + 5;
const int MOD = (int)1e9 + 7;
vector<long long> pref[4 * Z];
vector<int> t[4 * Z];
int a[Z];
void build(int v, int vl, int vr) {
if (vl == vr) {
t[v].push_back(a[vl]);
pref[v].push_back(a[vl]);
return;
}
int vm = (vl + vr) / 2;
build(2 * v, vl, vm);
build(2 * v + 1, vm + 1, vr);
merge((t[2 * v]).begin(), (t[2 * v]).end(), (t[2 * v + 1]).begin(),
(t[2 * v + 1]).end(), back_inserter(t[v]));
pref[v].resize((int)t[v].size());
pref[v][0] = t[v][0];
for (int i = 1; i < (int)t[v].size(); ++i) {
pref[v][i] = pref[v][i - 1] + t[v][i];
}
}
pair<long long, long long> query(int v, int vl, int vr, int l, int r, int val1,
int val2) {
if (vl > r || vr < l) return make_pair(0, 0);
if (l <= vl && r >= vr) {
long long cnt = upper_bound((t[v]).begin(), (t[v]).end(), val2) -
lower_bound((t[v]).begin(), (t[v]).end(), val1);
int posL = lower_bound((t[v]).begin(), (t[v]).end(), val1) - t[v].begin();
int posR = upper_bound((t[v]).begin(), (t[v]).end(), val2) - t[v].begin();
posR--;
long long sum;
if (posR == -1)
sum = 0;
else
sum = pref[v][posR] - (posL == 0 ? 0 : pref[v][posL - 1]);
return make_pair(sum, cnt);
}
int vm = (vl + vr) / 2;
pair<long long, long long> ql = query(2 * v, vl, vm, l, r, val1, val2);
pair<long long, long long> qr =
query(2 * v + 1, vm + 1, vr, l, r, val1, val2);
return make_pair(ql.first + qr.first, ql.second + qr.second);
}
int main() {
srand(time(0));
ios_base::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
build(1, 1, n);
long double ans = 0;
for (int i = 1; i <= n; ++i) {
pair<long long, long long> l;
if (a[i] - 2 >= 1)
l = query(1, 1, n, i + 1, n, 1, a[i] - 2);
else
l = make_pair(0, 0);
pair<long long, long long> r = query(1, 1, n, i + 1, n, a[i] + 2, inf);
ans += l.first + r.first - (l.second + r.second) * a[i];
}
cout << fixed << setprecision(0) << ans;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 300000;
const int MAXN = 1000010;
struct Node {
int x, l, r, type;
friend bool operator<(const Node &p, const Node &q) {
return (p.x == q.x) ? (p.type < q.type) : (p.x < q.x);
}
} Q[MAXN];
int n, L[MAXN], R[MAXN], V[MAXN], tot, ans, ansL, ansR, con;
struct Seg {
int l, r;
int lazy;
int M, pos;
} T[MAXN << 1];
Seg Merge(const Seg &p, const Seg &q) {
Seg con;
con.l = p.l, con.r = q.r, con.lazy = 0;
con.M = max(p.M, q.M);
if (con.M == p.M) con.pos = p.pos;
if (con.M == q.M) con.pos = q.pos;
return con;
}
void PushDown(int x) {
T[x << 1].lazy += T[x].lazy;
T[x << 1].M += T[x].lazy;
T[x << 1 | 1].lazy += T[x].lazy;
T[x << 1 | 1].M += T[x].lazy;
T[x].lazy = 0;
}
void MakeTree(int x, int l, int r) {
T[x].l = l, T[x].r = r, T[x].pos = l;
if (l == r) return;
int mid = (l + r) >> 1;
MakeTree(x << 1, l, mid);
MakeTree(x << 1 | 1, mid + 1, r);
}
void Change(int x, int l, int r, int tar) {
if (T[x].l == l && T[x].r == r) {
T[x].M += tar, T[x].lazy += tar;
return;
}
int mid = (T[x].l + T[x].r) >> 1;
PushDown(x);
if (r <= mid)
Change(x << 1, l, r, tar);
else if (l > mid)
Change(x << 1 | 1, l, r, tar);
else
Change(x << 1, l, mid, tar), Change(x << 1 | 1, mid + 1, r, tar);
T[x] = Merge(T[x << 1], T[x << 1 | 1]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d%d%d", &L[i], &V[i], &R[i]);
for (int i = 1; i <= n; i++) {
Q[++tot].x = L[i], Q[tot].l = V[i], Q[tot].r = R[i], Q[tot].type = 1;
Q[++tot].x = V[i], Q[tot].l = V[i], Q[tot].r = R[i], Q[tot].type = 3;
}
for (int i = 1; i <= n; i++) Q[++tot].x = L[i], Q[tot].type = 2;
sort(Q + 1, Q + tot + 1);
MakeTree(1, 1, N);
for (int i = 1; i <= tot; i++) {
if (Q[i].type == 1)
Change(1, Q[i].l, Q[i].r, 1);
else if (Q[i].type == 3)
Change(1, Q[i].l, Q[i].r, -1);
else if (Q[i].type == 2 && T[1].M > ans)
ans = T[1].M, ansL = Q[i].x, ansR = T[1].pos;
}
printf("%d\n", ans);
for (int i = 1; i <= n; i++)
if (L[i] <= ansL && R[i] >= ansR && V[i] <= ansR && V[i] >= ansL)
printf("%d ", i);
puts("");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, a[1010];
int f[1010][1010], pre[1010][1010];
void init() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
}
void print(int dep, int i) {
if (dep == 1 && i == 1) return;
int t = pre[dep][i];
print(dep - 2, t);
if (i < dep - 1)
printf("%d %d\n", dep - 1, dep);
else if (i == dep)
printf("%d %d\n", t, dep - 1);
else if (i == dep - 1)
printf("%d %d\n", t, dep);
}
void solve() {
memset(f, 0x3f, sizeof f);
f[1][1] = 0;
for (int i = 3; i <= n; i += 2) {
for (int j = 1; j <= i; j++) {
if (f[i - 2][j] != 0x3f3f3f3f || j > i - 2) {
if (j <= i - 2) {
int t = f[i - 2][j] + max(a[i], a[i - 1]);
if (f[i][j] > t) {
f[i][j] = t;
pre[i][j] = j;
}
} else {
for (int k = 1; k <= i; k++)
if (f[i - 2][k] != 0x3f3f3f3f) {
if (j == i - 1) {
int t = f[i - 2][k] + max(a[i], a[k]);
if (f[i][j] > t) {
f[i][j] = t;
pre[i][j] = k;
}
} else if (j == i) {
int t = f[i - 2][k] + max(a[k], a[i - 1]);
if (f[i][j] > t) {
f[i][j] = t;
pre[i][j] = k;
}
}
}
}
}
}
}
int ret = 0x3f3f3f3f, reti;
for (int i = 1; i <= n; i++) {
if (n & 1) {
if (ret > f[n][i] + a[i]) ret = f[n][i] + a[i], reti = i;
} else {
if (ret > f[n - 1][i] + max(a[n], a[i]))
ret = f[n - 1][i] + max(a[n], a[i]), reti = i;
}
}
printf("%d\n", ret);
if (n & 1) {
print(n, reti);
printf("%d\n", reti);
} else {
print(n - 1, reti);
printf("%d %d\n", reti, n);
}
}
int main() {
init();
solve();
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, t, count[21] = {0};
scanf("%d", &n);
while (n--) {
scanf("%d", &t);
count[t + 10] += 1;
}
long long result = static_cast<long long>(count[10]) *
static_cast<long long>(count[10] - 1) / 2;
for (int i = 0; i < 10; ++i) {
result += static_cast<long long>(count[i]) *
static_cast<long long>(count[20 - i]);
}
printf("%I64d\n", result);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
int n, m;
int ma[610][610];
int ps[610][610];
int sum(int a, int b, int c, int d) {
return ps[c][d]-ps[a-1][d]-ps[c][b-1]+ps[a-1][b-1];
}
char buf[310];
int main() {
int i, j, k;
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++) {
scanf("%s",buf);
for (j=1;j<=m;j++) ma[i+j-1][i-j+m] = (buf[j-1]=='#');
}
for (i=1;i<n+m;i++) {
for (j=1;j<n+m;j++) {
ps[i][j]=ps[i-1][j]+ps[i][j-1]-ps[i-1][j-1]+ma[i][j];
}
}
ll su = 0;
for (i=1;i<n+m;i++) {
for (j=1;j<n+m;j++) {
if (!ma[i][j]) continue;
for (k=1;k<n+m;k++) {
if (k==j) continue;
if (!ma[i][k]) continue;
if (abs(k-j)<i)su += sum(i-abs(k-j),min(j,k),i-abs(k-j),max(j,k))-ma[i-abs(k-j)][j];
if (abs(k-j)+i<n+m)su += sum(i+abs(k-j),min(j,k),i+abs(k-j),max(j,k))-ma[i+abs(k-j)][j];
}
for (k=1;k<n+m;k++) {
if (k==i) continue;
if (!ma[k][j]) continue;
if (abs(k-i)<j)su += sum(min(i,k),j-abs(k-i),max(i,k),j-abs(k-i))-ma[i][j-abs(k-i)];
if (abs(k-i)+j<n+m)su += sum(min(i,k),j+abs(k-i),max(i,k),j+abs(k-i))-ma[i][j+abs(k-i)];
}
}
}
printf("%lld\n",su/2);
return 0;
}
| 0 |
#include <bits/stdc++.h>
bool IsCircular(const std::string &s, const int n) {
bool isCircular = true;
for (int i = 0; i < n; i++) {
int j = (i + 1) % n;
isCircular &=
((s[i] == '>' || s[i] == '-') && (s[j] == '>' || s[j] == '-'));
}
if (isCircular) return true;
isCircular = true;
for (int i = 0; i < n; i++) {
int j = (i + 1) % n;
isCircular &=
((s[i] == '<' || s[i] == '-') && (s[j] == '<' || s[j] == '-'));
}
return isCircular;
}
int main() {
int t;
std::cin >> t;
while (t--) {
int n;
std::string s;
std::cin >> n >> s;
if (IsCircular(s, n))
std::cout << n << std::endl;
else {
int ans = 0;
for (int i = 0; i < n; i++) {
int j = (i - 1 + n) % n;
if (s[i] == '-' || s[j] == '-') ans++;
}
std::cout << ans << std::endl;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
cin >> n;
long long int sl = 0, sr = 0;
vector<int> a;
for (int i = 0; i < n; i++) {
cin >> x;
a.push_back(x);
}
sort(a.begin(), a.end());
int l = 0, r = n - 1;
while (r >= l) {
if (sl <= sr) {
sl += a[l];
l++;
} else {
sr += a[r];
r--;
}
}
cout << max(sr, sl) - min(sr, sl) + 1;
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 1e18;
inline int readint() {
int x = 0, s = 1, c = getchar();
while (c <= 32) c = getchar();
if (c == '-') s = -1, c = getchar();
for (; isdigit(c); c = getchar()) x = x * 10 + c - 48;
return x * s;
}
const int maxn = 2e5 + 5;
int n;
char s[maxn];
int a[maxn];
int dp[maxn][3], pre[maxn][3];
void print(int cur, int i) {
if (!cur) return;
print(cur - 1, pre[cur][i]);
if (i == 0)
putchar('R');
else if (i == 1)
putchar('G');
else
putchar('B');
}
int main() {
n = readint();
scanf("%s", s + 1);
for (int i = 1; i <= n; i++)
if (s[i] == 'R')
a[i] = 0;
else if (s[i] == 'G')
a[i] = 1;
else
a[i] = 2;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= 2; j++) {
dp[i][j] = inf;
for (int k = 0; k <= 2; k++) {
if (k == j) continue;
int t = dp[i - 1][k];
if (dp[i][j] > t) {
dp[i][j] = t;
pre[i][j] = k;
}
}
if (j != a[i]) dp[i][j]++;
}
}
int q, ans = inf;
for (int i = 0; i <= 2; i++)
if (dp[n][i] < ans) {
ans = dp[n][i];
q = i;
}
cout << ans << endl;
print(n, q);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, cnt = 0, r = 0, d, b, x = 1, k, ans = 0;
cin >> n >> k;
vector<pair<long long, long long>> v(k);
for (int i = 1; i <= k; i++) {
for (int mask = 0; mask <= 20; mask++) {
if (i & (1 << mask)) {
v.push_back({pow(2, mask), i});
break;
}
}
}
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
vector<long long> h;
for (int i = 0; i < v.size(); i++) {
if (cnt + v[i].first <= n) {
cnt += v[i].first;
h.push_back(v[i].second);
}
if (cnt == n) {
break;
}
}
if (h.size() == -1 || cnt != n) {
cout << -1;
} else {
cout << h.size() << endl;
for (int i = 0; i < h.size(); i++) {
cout << h[i] << " ";
}
}
return 0;
}
| 2 |
#include<bits/stdc++.h>
#define N 10000
using namespace std;
typedef long long ll;
typedef complex<ll> P;
string s;
int p,flag;
bool check(P A){
if(abs(A.real()) > N || abs(A.imag()) > N) return false;
return true;
}
P bnf();
P get_num(){
if(s[p]=='('){
p++;
P r=bnf();
p++;
return r;
}
if(s[p]=='i'){
p++;
return P(0,1);
}
ll num=0;
while('0'<=s[p]&&s[p]<='9'){
num=num*10+s[p]-'0';
p++;
if(!check(P(num,0))) flag=1;
}
return P(num,0);
}
P bnf2(){
P res=get_num();
while(p<s.size()&&!flag){
if(s[p]=='*'){
p++;
P r=get_num();
res*=r;
if(!check(res)) flag=1;
}else break;
}
return res;
}
P bnf(){
P res=bnf2();
while(p<s.size()&&!flag){
if(s[p]=='+'){
p++;
P r=bnf2();
res+=r;
if(!check(res)) flag=1;
}
else if(s[p]=='-'){
p++;
P r=bnf2();
res-=r;
if(!check(res)) flag=1;
}else break;
}
return res;
}
int main(){
while(cin>>s){
p=0;
flag=0;
P ans=bnf();
if(flag) cout<<"overflow"<<endl;
else{
ll A=ans.real(),B=ans.imag();
if(A&&B){
cout<<A;
if(B>0) cout<<'+';
cout<<B<<"i"<<endl;
}
if(!A&&B) cout<<B<<"i"<<endl;
if(A&&!B) cout<<A<<endl;
if(!A&&!B) cout<<0<<endl;
}
}
return 0;
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.