solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include<cstdio>
#include<algorithm>
using namespace std;
int bit[100001];
int n,q;
void add(int i,int v){
while(i<=n){
bit[i] += v;
i += i & -i;
}
}
int sum(int i){
int res = 0;
while(i>0){
res += bit[i];
i -= i & -i;
}
return res;
}
int main(){
scanf("%d %d",&n,&q);
fill(bit,bit+n+1,0);
for(int i=0;i<q;i++){
int cmd,x,y;
scanf("%d %d %d",&cmd,&x,&y);
if(cmd==0){
add(x,y);
}else{
printf("%d\n",sum(y)-sum(x-1));
}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long INF = INT_MAX;
long long ans = INT_MIN;
long long xx[] = {0, 0, 1, 1, 1, -1, -1, -1};
long long yy[] = {-1, 1, -1, 0, 1, -1, 0, 1};
void solve() {
long long n, m;
cin >> n >> m;
if (n == m)
cout << 0;
else {
if (m % n != 0)
cout << -1;
else {
m = m / n;
long long c = 0;
while (m % 2 == 0) {
c++;
m /= 2;
}
while (m % 3 == 0) {
c++;
m /= 3;
}
if (m == 1)
cout << c;
else
cout << -1;
}
}
}
int32_t main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
{
solve();
cout << endl;
}
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
const int N=2e3+10;
int gi() {
int x=0,o=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if(ch=='-') o=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*o;
}
int n,ans=1e9,l[N],r[N],mx[N];
char a[N],b[N];
void work() {
for(int i=0;i<n;i++) {
l[i]=r[i]=0;
while(b[(i+r[i])%n]!='1') ++r[i];
while(b[(i+l[i]+n)%n]!='1') --l[i];
}
for(int s=0;s<n;s++) {
for(int i=0;i<n;i++) mx[i]=0;
int dif=0;
for(int i=0;i<n;i++)
if(a[i]!=b[(i+s)%n])
mx[-l[i]]=max(mx[-l[i]],r[i]-s),++dif;
int mn=1e9,y=0;
for(int x=n-1;x>=0;x--)
mn=min(mn,x+y),y=max(y,mx[x]);
ans=min(ans,2*mn+s+dif);
}
}
int main() {
scanf("%s",a),scanf("%s",b),n=strlen(a);
int A=0,B=0;
for(int i=0;i<n;i++) {
if(a[i]=='1') ++A;
if(b[i]=='1') ++B;
}
if(!B) {
if(!A) puts("0");
else puts("-1");
return 0;
}
work();
reverse(a,a+n),reverse(b,b+n);
work();
printf("%d\n",ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, k, i, rem, flag = 0, sum = 0, c = 0;
cin >> n >> m >> k;
int ar[n];
for (i = 0; i < n; i++) cin >> ar[i];
sort(ar, ar + n);
for (i = n - 1; i >= 0; i--) {
if (k >= m)
break;
else
k = k + ar[i] - 1;
c++;
}
if (k >= m)
cout << c;
else
cout << "-1";
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
int c=0;
int a;
cin>>n>>m;
for (int i=0;i<n;i++)
{
cin>>a;
if (a>=m)
c++;
}
cout<<c;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int dp[1010][55][55];
int failS[1010];
int failT[1010];
char c[1010];
char s[1010];
char t[1010];
int main(void) {
cin >> c;
cin >> s;
cin >> t;
int S = strlen(s);
int T = strlen(t);
int C = strlen(c);
int idx;
idx = 0;
for (int i = 1; i <= S; i++) {
while (idx && s[i] != s[idx]) idx = failS[idx - 1];
if (s[i] == s[idx]) {
idx++;
failS[i] = idx;
}
}
idx = 0;
for (int i = 1; i <= T; i++) {
while (idx && t[i] != t[idx]) idx = failT[idx - 1];
if (t[i] == t[idx]) {
idx++;
failT[i] = idx;
}
}
for (int i = 0; i <= C; i++) {
for (int j = 0; j <= S; j++) {
for (int k = 0; k <= T; k++) dp[i][j][k] = -1010;
}
}
dp[0][0][0] = 0;
for (int i = 0; i < C; i++) {
for (int j = 0; j < S; j++) {
for (int k = 0; k < T; k++) {
if (dp[i][j][k] < -1010 / 10) continue;
for (int l = 'a'; l <= 'z'; l++) {
if (c[i] == l || c[i] == '*') {
int tj = j;
int tk = k;
int v = 0;
while (tj && l != s[tj]) tj = failS[tj - 1];
while (tk && l != t[tk]) tk = failT[tk - 1];
if (l == s[tj]) {
tj++;
if (tj == S) {
v++;
tj = failS[tj - 1];
}
}
if (l == t[tk]) {
tk++;
if (tk == T) {
v--;
tk = failT[tk - 1];
}
}
dp[i + 1][tj][tk] = max(dp[i + 1][tj][tk], dp[i][j][k] + v);
}
}
}
}
}
int ans = -1010;
for (int i = 0; i < S; i++) {
for (int j = 0; j < T; j++) ans = max(ans, dp[C][i][j]);
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N, dp1[1 << 10], dp2[1 << 10];
string S;
while(cin >> N, N) {
memset(dp1, 0, sizeof(dp1));
cin >> S;
for(int i = 0, curr = 0; i < N; i++) {
if(S[i] == 'u') {
curr = 0;
} else {
dp1[curr] |= 1 << (S[i] - '0');
curr |= 1 << (S[i] - '0');
}
}
for(int i = 0; i < (1 << 10); i++) {
dp2[i] = dp1[i];
}
for(int i = 1; i < 10; i++) {
for(int j = 0; j < (1 << 10); j++) {
for(int k = 0; k < (1 << 10); k++) {
if((j & k) != k) continue;
if(dp2[j - k] == 0) continue;
if(dp1[k] == 0) continue;
dp2[j] |= dp2[j - k] | dp1[k];
}
}
}
if([&]()
{
for(int i = 0; i < (1 << 10); i++) {
if(dp2[i] && (dp2[i] & i) == dp2[i]) return (false);
}
return (true);
}
())
cout << "SAFE" << endl;
else {
cout << "UNSAFE" << endl;
}
}
} | 0 |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<pair<int,int>,string> item;
int main(){
int n,i,w,s,S;
for(string f;cin>>n,n;){
vector<item>items;
vector<int>num,result;
for(i=0;i<n;i++)cin>>f>>w>>s,items.push_back(make_pair(make_pair(w,s),f)),num.push_back(i);
S=99999;
do{
for(s=w=0,i=n-1;~i;i--){
pair<int,int> weight=items[num[i]].first;
if(weight.second<w)break;
w+=weight.first;
s+=(i+1)*weight.first;
}
if(i<0&&s<S)S=s,result=num;
}while(next_permutation(num.begin(),num.end()));
for(i=0;i<n;i++)cout<<items[result[i]].second<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int a[310][310];
int main() {
int n, m, k, i, j, x, y;
cin >> n >> m >> k;
int r = (n * m) / k;
for (i = 1; i <= n; ++i) {
a[i][0] = a[i][m + 1] = 1;
}
x = 1;
y = 1;
bool b = 0;
if (r * (k - 1) > 0) {
cout << r << ' ';
for (i = 1; i <= r * (k - 1); ++i) {
if (i % r == 1 && i != 1) cout << '\n' << r << ' ';
cout << x << ' ' << y << ' ';
if (a[x][y + 1] == 0) {
a[x][y] = 1;
y++;
} else {
if (a[x][y - 1] == 0) {
a[x][y] = 1;
y--;
} else {
a[x][y] = 1;
x++;
}
}
}
b = 1;
}
if (n * m - r * (k - 1) > 0) {
if (b)
cout << '\n' << n * m - r * (k - 1) << ' ';
else
cout << n * m - r * (k - 1) << ' ';
for (i = r * (k - 1) + 1; i <= n * m; ++i) {
cout << x << ' ' << y << ' ';
if (a[x][y + 1] == 0) {
a[x][y] = 1;
y++;
} else {
if (a[x][y - 1] == 0) {
a[x][y] = 1;
y--;
} else {
a[x][y] = 1;
x++;
}
}
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct pt {
long long int x1, x2, y1, y2;
};
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, i;
pt a, b, c;
cin >> a.x1 >> a.y1 >> a.x2 >> a.y2;
cin >> b.x1 >> b.y1 >> b.x2 >> b.y2;
cin >> c.x1 >> c.y1 >> c.x2 >> c.y2;
if (a.x1 >= b.x1 && a.x2 <= b.x2 && a.y1 >= b.y1 && a.y2 <= b.y2)
return cout << "NO", 0;
if (a.x1 >= c.x1 && a.x2 <= c.x2 && a.y1 >= c.y1 && a.y2 <= c.y2)
return cout << "NO", 0;
if (a.x1 >= b.x1 && a.x2 <= b.x2 && a.y2 <= b.y2 && a.x1 >= c.x1 &&
a.x2 <= c.x2 && a.y1 >= c.y1 && b.y1 <= c.y2)
return cout << "NO", 0;
if (a.x1 >= c.x1 && a.x2 <= c.x2 && a.y2 <= c.y2 && a.x1 >= b.x1 &&
a.x2 <= b.x2 && a.y1 >= b.y1 && c.y1 <= b.y2)
return cout << "NO", 0;
if (a.x1 >= b.x1 && a.y1 >= b.y1 && a.y2 <= b.y2 && a.x2 <= c.x2 &&
a.y1 >= c.y1 && a.y2 <= c.y2 && b.x2 >= c.x1)
return cout << "NO", 0;
if (a.x1 >= c.x1 && a.y1 >= c.y1 && a.y2 <= c.y2 && a.x2 <= b.x2 &&
a.y1 >= b.y1 && a.y2 <= b.y2 && c.x2 >= b.x1)
return cout << "NO", 0;
cout << "YES";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244853;
int C[4010][4010];
int dp[4010][4010];
int calc(int n, int m) {
if (n - m >= 1) return C[n + m][m];
int tot = n + m, val = 2 - (n - m), x = tot + val >> 1, y = tot - val >> 1;
if (x < 0 || y < 0) return 0;
return C[x + y][x];
}
int main() {
for (int i = 0; i <= 4005; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
if (i == 0)
dp[i][j] = 0;
else if (j == 0)
dp[i][j] = i;
else {
long long tmp = dp[i - 1][j];
tmp =
(tmp + C[i - 1 + j][j] + dp[i][j - 1] - calc(i, j - 1) + mod) % mod;
dp[i][j] = tmp;
}
}
}
printf("%d\n", dp[n][m]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = 10 * x + ch - '0';
ch = getchar();
}
return x * f;
}
int n, m, a[100000 + 5];
inline void print(double ans) { printf("%.6f\n", ans); }
int main() {
n = read();
for (int i = 1; i <= (n); i++) a[i] = read();
for (int i = 1; i <= (n); i++)
for (int j = (i + 1); j <= (n); j++)
if (a[i] > a[j]) m++;
if (m & 1)
print(2 * m - 1);
else
print(2 * m);
return 0;
}
| 2 |
//O(NlogN + Qlog^2N)の適当解法(クエリ入れ替えなし)
//意味が分からないバグに悩まされる。→セグメントツリーの子はind*2 + 1とind*2 + 2。間違えてたし気づかなすぎたので、頭ない。
#include<iostream>
#include<string>
#include<algorithm>
#include<functional>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
using namespace std;
int d;
int x[252521];
int q;
int l[100000], r[100000], e[100000];
const int INF = 1145141919;
const int depth = 17; //ここをデバッグ用に6に設定していて、3WA出すなんて、あほすぎる。
class Segtree {
public:
vector<int> data[1<<(depth+1)];
void ini(int index = 0, int a = 0, int b = (1<<(depth)) - 1) {
//[a, b]の昇順ソート列をdata[index]に入れる。
for(int i = a; i <= b; i++ )
data[index].push_back(x[i]);
sort(data[index].begin(), data[index].end() );
if ( b - a > 0 ) {
ini(index*2 + 1, a, a + (b-a-1)/2);
ini(index*2 + 2, a + (b-a+1)/2, b);
}
}
//[l, r]にv以下の要素がいくつあるか。
int count(int l, int r, int v, int index = 0, int a = 0, int b = (1<<(depth)) - 1) {
if ( a > r || b < l || data[index].size() == 0 ) return 0;
if ( l <= a && b <= r ) {
//[a, b]の昇順ソート列の中で、v(以下の要素のうち最大の要素)は何番目にある?
//最初を0番とする。
int st = -1, ed = data[index].size() - 1, medi;
while( st <= ed ) {
medi = (st + ed)/2;
if ( data[index][medi] > v )
ed = medi-1;
else {
if ( medi+1 == data[index].size() || data[index][medi+1] > v )
break;
st = medi+1;
}
}
//Index + 1を返す
return medi + 1;
}
int cnt1 = count(l, r, v, index*2 + 1, a, a + (b-a-1)/2);
int cnt2 = count(l, r, v, index*2 + 2, a + (b-a+1)/2, b);
return cnt1 + cnt2;
}
}seg;
signed main() {
int i, j;
cin >> d;
for( i = 0; i < d; i++ )
cin >> x[i];
for( i = d; i < 252521; i++ )
x[i] = INF;
seg.ini();
cin >> q;
for( i = 0; i < q; i++ ) {
cin >> l[i] >> r[i] >> e[i];
l[i]--; r[i]--;
int a = max(x[l[i]], x[r[i]]) + e[i];
int b = min(x[l[i]], x[r[i]]) - e[i];
int cnt1 = seg.count(l[i], r[i], a);
int cnt2 = seg.count(l[i], r[i], b-1);
int ans = r[i] - l[i] + 1 - (cnt1 - cnt2);
cout << ans << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int IN() {
int c, x;
while (!isdigit(c = getchar()))
;
x = c - '0';
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + c - '0';
return x;
}
const int N = 1000 + 19;
int A[N], Max, n;
int main() {
n = IN();
for (int i = 1; i < n + 1; i++) A[i] = IN();
for (int i = 1; i < n + 1; i++) {
int tmp = 1;
for (int j = i - 1; j; j--)
if (A[j] <= A[j + 1])
tmp++;
else
break;
for (int j = i + 1; j <= n; j++)
if (A[j] <= A[j - 1])
tmp++;
else
break;
Max = max(Max, tmp);
}
printf("%d\n", Max);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long maxPrimeFactors(long long n) {
long long maxPrime = -1;
while (n % 2 == 0) {
maxPrime = 2;
n >>= 1;
}
for (int i = 3; i <= sqrt(n); i += 2) {
while (n % i == 0) {
maxPrime = i;
n = n / i;
}
}
if (n > 2) maxPrime = n;
return maxPrime;
}
long long int smallestDivisor(int n) {
if (n % 2 == 0) return 2;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) return i;
}
return n;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int i = 1, count = 0;
i = smallestDivisor(n);
n /= i;
cout << (1) * n << " " << (i - 1) * n << "\n";
}
return 0;
}
| 2 |
// abc113_c.cc
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
map<int, map<int, int>> mp;
vector<int> p(m), y(m);
for (int i = 0; i < m; i++) {
cin >> p[i] >> y[i];
mp[p[i]][y[i]]++;
}
for (auto& imp : mp) {
int i = 1;
for (auto& it : imp.second)
it.second = i++;
}
for (int i = 0; i < m; i++)
printf("%06d%06d\n", p[i], mp[p[i]][y[i]]);
} | 0 |
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
typedef long long int ll;
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n; cin >> n;
vector<int> a(n);
for(int i=0;i<n;i++){
cin >> a[i];
}
int ans=0;
for(int i=0;i<n-1;i++){
if(a[i+1]>a[i])ans++;
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
string second;
string t;
cin >> second;
cin >> t;
int a = 0, b = 0, t1 = 0, t2 = 0;
for (int i = 0; i < second.length(); i++) {
if (second[i] == 'a') a++;
if (t[i] == 'a') a++;
if (second[i] == 'b') b++;
if (t[i] == 'b') b++;
if (second[i] == 'a' && t[i] == 'b') t1++;
if (second[i] == 'b' && t[i] == 'a') t2++;
}
if (a % 2 == 1 || b % 2 == 1)
cout << -1;
else {
cout << t1 / 2 + t1 % 2 + t2 / 2 + t2 % 2 << endl;
int pos1, pos2, k = 0, m = 0;
for (int i = 0; i < second.length(); i++) {
if (second[i] == 'a' && t[i] == 'b' && k == 1) {
cout << i + 1 << " " << pos1 + 1 << endl;
k = 0;
} else if (second[i] == 'a' && t[i] == 'b') {
pos1 = i;
k = 1;
}
if (second[i] == 'b' && t[i] == 'a' && m == 1) {
cout << i + 1 << " " << pos2 + 1 << endl;
m = 0;
} else if (second[i] == 'b' && t[i] == 'a') {
pos2 = i;
m = 1;
}
}
if (k == 1) {
cout << pos1 + 1 << " " << pos1 + 1 << endl;
cout << pos1 + 1 << " " << pos2 + 1;
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
int arr[200] = {}, i, j, k, w, ans = 0;
for (i = 0; i < n; i++) cin >> arr[i];
cin >> w;
sort(arr, arr + n);
for (i = 0; i < n; i++) {
int c = 1;
for (j = i + 1; j < n; j++) {
if (arr[j] - arr[i] <= w)
c++;
else
break;
}
ans = (ans > c ? ans : c);
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<int, int> > a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i].first;
a[i].second = i;
}
sort((a).begin(), (a).end());
int m = 0;
for (int i = 0; i < n; ++i) {
a[i].first = max(a[i].first, m);
m = a[i].first + 1;
swap(a[i].first, a[i].second);
}
sort((a).begin(), (a).end());
for (int i = 0; i < n; ++i) cout << a[i].second << ' ';
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline long long gcd(long long a, long long b) {
if (b == 0) return a;
a %= b;
return gcd(b, a);
}
inline long long max(long long a, long long b) { return ((a > b) ? a : b); }
inline long long min(long long a, long long b) { return ((a > b) ? b : a); }
long long power(long long x, long long y);
long long powermod(long long x, long long ex, long long md);
const long long inf = 1e18 + 9;
const long long mod = 1e9 + 7;
const long double PI = acos(-1);
const long double eps = 1e-9;
const long long N = 1e5 + 11;
long long mul(long long i) {
long long p = 1;
p *= i;
p %= mod;
p *= (i - 1);
p %= mod;
p *= powermod(2, mod - 2, mod);
p %= mod;
p = (p + i) % mod;
return p % mod;
}
void solve() {
string second;
cin >> second;
long long n = (int)(second.size());
long long ans = 0;
for (int i = 0; i < n; i++) {
long long p = ((second[i] - '0') % mod * mul(i)) % mod;
p %= mod;
p *= powermod(10, n - i - 1, mod);
ans += p;
ans %= mod;
}
for (int i = 0; i <= n - 2; i++) {
long long p = ((second[i] - '0')) % mod;
long long a = (powermod(10, n - i - 1, mod) * (n - i - 2)) % mod;
a %= mod;
a *= 9;
a %= mod;
long long b =
(powermod(2, n - i + 2, mod) * powermod(5, n - i - 1, mod)) % mod;
b %= mod;
long long c = 1;
long long sum = a;
sum += b;
sum %= mod;
sum += c;
sum %= mod;
sum *= powermod(81, mod - 2, mod);
sum %= mod;
p = (p % mod * sum % mod) % mod;
ans = (ans % mod + p % mod) % mod;
ans %= mod;
}
cout << ans << "\n";
}
int main() {
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
};
cout << fixed << setprecision(20);
long long NTC = 1;
long long PTC = 0;
while ((PTC++) < NTC) {
solve();
}
}
long long power(long long x, long long y) {
if (y == 0) return 1;
long long a = power(x, y / 2);
if (y % 2 == 0)
return a * a;
else
return x * a * a;
}
long long powermod(long long x, long long ex, long long md) {
long long ans = 1ll;
while (ex > 0) {
if (ex & 1ll) ans = (ans * x) % md;
ex >>= 1ll;
x = (x * x) % md;
}
return ans;
}
| 3 |
#include <bits/stdc++.h>
int main(int argc, char* argv[]) {
printf("black\n");
fflush(stdout);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 5;
const int nax = 1e6 + 5;
const int pot = 256 * 1024;
void upd(int* tr, int r, int day, int diff) {
day += pot;
tr[day] = min(r, tr[day] + diff);
for (int x = day / 2; x >= 1; x /= 2) tr[x] = tr[2 * x] + tr[2 * x + 1];
}
long long s(int* tr, int low, int high) {
if (low > high) return 0;
low += pot;
high += pot;
long long ans = tr[low];
if (low != high) ans += tr[high];
while (low < high - 1) {
if (low % 2 == 0) ans += tr[low + 1];
if (high % 2 == 1) ans += tr[high - 1];
low /= 2;
high /= 2;
}
return ans;
}
int tr1[2 * pot], tr2[2 * pot];
int r1, r2;
void upd(int day, int diff) {
upd(tr1, r1, day, diff);
upd(tr2, r2, day, diff);
}
int main() {
int n, gap, q;
scanf("%d%d%d%d%d", &n, &gap, &r1, &r2, &q);
while (q--) {
int type;
scanf("%d", &type);
if (type == 1) {
int day, diff;
scanf("%d%d", &day, &diff);
upd(day, diff);
} else {
assert(type == 2);
int day;
scanf("%d", &day);
printf("%lld\n", s(tr2, 1, day - 1) + s(tr1, day + gap, n));
}
}
return 0;
}
| 2 |
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
int a,b,c;
cin>>a>>b>>c;
if(a*a+b*b==c*c || a*a==b*b+c*c || a*a+c*c==b*b){
cout<<"YES"<<endl;
}
else cout<<"NO"<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int N, Q;
int a[200005];
long long k[200005], s[200005];
int main() {
ios::sync_with_stdio(0);
cin >> N >> Q;
for (int i = 1; i <= N; i++) {
cin >> a[i];
s[i] = s[i - 1] + a[i];
}
for (int i = 1; i <= Q; i++) cin >> k[i];
int f = 1, res = a[1];
for (int i = 1; i <= Q; i++) {
int p = lower_bound(s + f, s + 1 + N, s[f - 1] + k[i] + (a[f] - res)) - s;
if (p > N || p == N && s[f - 1] + k[i] + (a[f] - res) == s[p]) {
f = 1;
res = a[f];
} else {
if (s[f - 1] + k[i] + (a[f] - res) == s[p]) {
res = a[p + 1];
f = p + 1;
} else {
res = s[p] - (s[f - 1] + k[i] + (a[f] - res));
f = p;
}
}
cout << (N - f + 1) << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long now = 1;
while (1) {
if (n % now != 0) {
cout << n / now + 1 << endl;
return 0;
}
now *= 3;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m, tot, pre[200010 << 1], ans[200010], now[200010], d[200010],
son[200010 << 1], z[200010], s[200010], g[200010];
map<string, int> c[200010];
string ch[200010];
vector<int> a[200010], b[200010];
void put(int x, int y) {
pre[++tot] = now[x];
now[x] = tot;
son[tot] = y;
}
void dfs1(int u, int fa) {
d[u] = d[fa] + 1, s[u] = 1;
int num = 0;
for (int i = now[u]; i; i = pre[i])
if (son[i] != fa) {
dfs1(son[i], u), s[u] += s[son[i]];
if (num < s[son[i]]) num = s[son[i]], z[u] = son[i];
}
}
void qk(int u, int fa) {
if (u != 0) {
c[d[u]][ch[u]]--;
if (!c[d[u]][ch[u]]) g[d[u]]--;
}
for (int i = now[u]; i; i = pre[i])
if (son[i] != fa) qk(son[i], u);
}
void dfs3(int u, int fa, int zz) {
if (u != 0) {
if (!c[d[u]][ch[u]]) g[d[u]]++;
c[d[u]][ch[u]]++;
}
for (int i = now[u]; i; i = pre[i])
if (son[i] != fa && son[i] != zz) dfs3(son[i], u, zz);
}
void dfs2(int u, int fa) {
for (int i = now[u]; i; i = pre[i])
if (son[i] != fa && son[i] != z[u]) dfs2(son[i], u), qk(son[i], u);
if (z[u]) dfs2(z[u], u);
dfs3(u, fa, z[u]);
for (int i = 0; i < a[u].size(); i++) ans[b[u][i]] = g[a[u][i] + d[u]];
}
signed main() {
scanf("%d", &n);
for (int i = 1, x; i <= n; i++)
cin >> ch[i], scanf("%d", &x), put(x, i), put(i, x);
scanf("%d", &m);
for (int i = 1, x, y; i <= m; i++)
scanf("%d%d", &x, &y), a[x].push_back(y), b[x].push_back(i);
dfs1(0, 0);
dfs2(0, 0);
for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
return 0;
}
| 5 |
#include <iostream>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
int main() {
double a,b,c[] = {0.2, 0.6, 1.1};
int l[4]= {}, r[4] = {};
while(cin >> a >> b) {
bool fa = 1, fb = 1;
rep(i,4) {
if(c[i]>a && fa) {
l[i]++;
fa = 0;
}
if(c[i]>b && fb) {
r[i]++;
fb = 0;
}
}
if(fa) l[3]++;
if(fb) r[3]++;
}
rep(i,4) {
cout << l[3-i] << " " << r[3-i] << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int m;
cin >> m;
vector<int> q(m);
for (int i = 0; i < m; i++) cin >> q[i];
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
sort(a.begin(), a.end());
sort(q.begin(), q.end());
reverse(a.begin(), a.end());
int c = 0;
int ans = 0;
for (int i = 0; i < n; i++, c++) {
if (c == q[0]) {
i += 2;
c = 0;
}
if (i >= n) break;
ans += a[i];
}
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
map<pair<int, pair<int, int> >, int> was;
int f(int n, int type) {
if (n == 0) return 1;
int h = 0;
if (was.find({n, {h, type}}) != was.end()) return was[{n, {h, type}}];
int ans = 0;
for (int i = (n - 1) / 2; i <= (n) / 2; i++) {
if (type == 1 && (i % 2) == 0) continue;
if (type == 0 && (n - 1 - i) % 2) continue;
int l = 0;
int r = 0;
l = f(i, 0);
r = f(n - i - 1, 1);
ans = (ans + l * 1ll * r) % 998244353;
}
return was[{n, {h, type}}] = ans;
}
int solve(int n) {
int ans = f(n, 2);
return ans;
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
cout << solve(n) << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
bool k[210];
int main() {
int q;
cin >> q;
while (q--) {
int n, x;
cin >> n >> x;
memset(k, false, sizeof k);
for (int i = 1; i <= n; i++) {
int w;
cin >> w;
k[w] = true;
}
for (int i = 1; i <= 205; i++) {
if (k[i] == false) x--, k[i] = true;
if (x == 0) {
for (int j = i; j <= 205; j++) {
if (k[j] == false) {
cout << j - 1 << endl;
break;
}
}
break;
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int C = 350, MAXN = 1e5 + C - 1, MAXC = MAXN / C;
int cnt[MAXC][MAXN];
deque<int> deq[MAXC];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int c = i / C, a;
scanf("%d", &a);
deq[c].push_back(a);
cnt[c][a]++;
}
int q;
cin >> q;
int lastans = 0;
while (q--) {
int t, l, r;
scanf("%d%d%d", &t, &l, &r);
l = ((l + lastans - 1) % n) + 1, r = ((r + lastans - 1) % n) + 1;
if (l > r) swap(l, r);
l--;
if (t == 1) {
int add = deq[(r - 1) / C][r - (r - 1) / C * C - 1];
for (int s = l / C; s * C < r; s++) {
int sl = s * C, sr = sl + C;
if (l <= sl && r >= sr) {
cnt[s][add]++;
deq[s].push_front(add);
add = deq[s].back();
cnt[s][add]--;
deq[s].pop_back();
} else {
int L = max(sl, l), R = min(sr, r), tmp = deq[s][R - 1 - sl];
cnt[s][add]++;
cnt[s][tmp]--;
for (int i = R - 1 - sl; i > L - sl; i--) deq[s][i] = deq[s][i - 1];
deq[s][L - sl] = add;
add = tmp;
}
}
} else {
int k;
scanf("%d", &k);
k = ((k + lastans - 1) % n) + 1;
int ans = 0;
for (int s = l / C; s * C < r; s++) {
int sl = s * C, sr = sl + C;
if (l <= sl && r >= sr)
ans += cnt[s][k];
else {
int L = max(l, sl), R = min(r, sr);
for (int i = L; i < R; i++)
if (deq[s][i - sl] == k) ans++;
}
}
printf("%d\n", ans);
lastans = ans;
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1000000000 + 7;
const long long int N = 1000000 + 6;
int recur_depth = 0;
template <typename Ostream, typename Cont>
typename enable_if<is_same<Ostream, ostream>::value, Ostream&>::type operator<<(
Ostream& os, const Cont& v) {
os << "[";
for (auto& x : v) {
os << x << ", ";
}
return os << "]";
}
template <typename Ostream, typename... Ts>
Ostream& operator<<(Ostream& os, const pair<Ts...>& p) {
return os << "{" << p.first << ", " << p.second << "}";
}
void inp(long long int* arr, long long int n) {
for (int i = 0; i < n; i++) cin >> arr[i];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int testcases = 1;
cin >> testcases;
while (testcases--) {
int n;
cin >> n;
int z = 0, o = 0;
int arr[n + 1];
for (int i = 1; i <= n; i++) {
cin >> arr[i];
if (arr[i])
o++;
else {
z++;
}
}
if (z >= (n / 2)) {
cout << n / 2 << '\n';
for (int i = 0; i < n / 2; i++) cout << 0 << ' ';
cout << '\n';
} else {
int x = o;
if (x % 2) x--;
cout << x << '\n';
for (int i = 0; i < x; i++) cout << 1 << ' ';
cout << '\n';
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> all;
inline void fast() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
const long long nn = 9e18;
class data {
public:
string s;
long long remain;
bool status;
data(string a, long long b, bool c) { s = a, remain = b, status = c; }
};
long long cal(string &s) {
long long total = 0;
long long n = s.size();
for (long long i = 0; i < n; ++i) {
if (s[i] == '1') {
total += all[i];
}
}
return total;
}
data check(long long n) {
string s = "";
long long k = all.size();
long long original = n;
bool status = false;
for (long long i = k - 1; i >= 0; --i) {
if (n >= all[i]) {
s += '1';
n -= all[i];
} else {
s += '0';
}
if (n == 0) {
status = true;
}
}
reverse(s.begin(), s.end());
data obj(s, n, status);
return obj;
}
long long find_it(long long n) {
auto pp = check(n);
if (pp.status) {
return cal(pp.s);
}
string s = pp.s;
long long tt = s.size();
long long remain = pp.remain;
for (long long i = 0; i < tt; ++i) {
if (s[i] == '0') {
if (all[i] >= remain) {
s[i] = '1';
for (long long j = i - 1; j >= 0; --j) {
s[j] = '0';
}
break;
}
}
}
return cal(s);
}
void preprocess() {
long long power = 1;
while (true) {
all.push_back(power);
if (power >= (nn) / 3) {
break;
}
power *= 3;
}
}
int main() {
fast();
preprocess();
long long q;
cin >> q;
while (q--) {
long long n;
cin >> n;
cout << find_it(n) << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n, k, a, ans = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> k >> a;
ans = max(k + 1, ans);
a--;
while (a != 0) {
k++;
a /= 4;
}
ans = max(k, ans);
}
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int N,x=1;string S;cin>>N>>S;
for(int i=1;i<N;i++){
if(S.at(i-1)!=S.at(i))x++;
}
cout<<x<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> s;
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'H' || s[i] == 'Q' || s[i] == '9') {
cout << "YES";
return 0;
}
}
cout << "NO";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int arr[1000][1000];
int values[1000000];
bool good[1000][1000];
int n, m;
bool check(int k) {
memset(good, 0, sizeof good);
for (int i = 0; i < n; i++) {
vector<int> idx;
for (int j = 0; j < m; j++)
if (arr[i][j] >= k) idx.push_back(j);
for (int j = 0; j < idx.size(); j++)
for (int z = j + 1; z < idx.size(); z++)
if (good[idx[j]][idx[z]])
return 1;
else
good[idx[j]][idx[z]] = 1;
}
return 0;
}
int main() {
scanf("%d %d", &n, &m);
int z = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &arr[i][j]);
values[z++] = arr[i][j];
}
}
sort(values, values + m * n);
int s = 0, e = m * n - 1;
while (true) {
if (s == e) break;
if (e == s + 1) {
if (!check(values[s + 1])) break;
s++;
break;
}
int mid = (s + e) / 2;
if (check(values[mid]))
s = mid;
else
e = mid - 1;
}
printf("%d\n", values[s]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr long long LINF = numeric_limits<long long>::max() / 3;
struct Double {
double d;
explicit Double(double x) : d(x) {}
};
ostream& operator<<(ostream& os, const Double x) {
os << fixed << setprecision(20) << x.d;
return os;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
os << "[";
for (const auto& v : vec) {
os << v << ",";
}
os << "]";
return os;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& st) {
os << "{";
for (T v : st) os << v << ",";
os << "}";
return os;
}
template <typename T, typename U>
inline void chmax(T& x, U y) {
if (y > x) x = y;
}
template <typename T, typename U>
inline void chmin(T& x, U y) {
if (y < x) x = y;
}
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
constexpr double eps = 1e-10;
constexpr long long mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
using P = pair<long long, long long>;
map<long long, long long> mp;
int main() {
int n, k;
cin >> n >> k;
vector<long long> p(n);
vector<long long> c(n);
for (int i = 0; i < (int)(n); i++) cin >> p[i];
for (int i = 0; i < (int)(n); i++) cin >> c[i];
vector<P> pi(n);
for (int i = 0; i < (int)(n); i++) pi[i] = P(p[i], i);
sort((pi).begin(), (pi).end());
priority_queue<P> que;
for (int i = 0; i < n; i++) {
int idx = pi[i].second;
long long cnt = 0;
long long coins = c[idx];
queue<P> save;
while (cnt < k and !que.empty()) {
auto tp = que.top();
que.pop();
save.push(tp);
if (tp.second >= p[idx]) continue;
coins += tp.first;
cnt++;
}
mp[idx] = coins;
while (!save.empty()) {
que.push(save.front());
save.pop();
}
que.push(P(c[idx], p[idx]));
}
for (int i = 0; i < n; i++) {
if (i) cout << " ";
cout << mp[i];
}
cout << endl;
}
| 2 |
#include <bits/stdc++.h>
const long long MOD = 1000000007;
const int INF = 1 << 30;
using namespace std;
vector<int> a;
int cc = 0;
int sum1 = 0;
int sum2 = 0;
int all = 0;
int rr = 0;
void done() {
all++;
int ss = 0;
for (int i = 0; i < a.size(); ++i) {
if (a[i] == 1)
ss++;
else
ss--;
}
if ((ss + sum2) == sum1) {
rr++;
}
}
void rec(int idx) {
if (idx == cc) {
done();
return;
}
for (int i = 1; i <= 2; ++i) {
a[idx] = i;
rec(idx + 1);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s1, s2;
cin >> s1 >> s2;
for (int i = 0; i < s2.size(); ++i) {
if (s1[i] == '+') {
sum1++;
} else {
sum1--;
}
if (s2[i] == '?')
cc++;
else if (s2[i] == '+')
sum2++;
else
sum2--;
}
if (cc == 0) {
if (sum1 == sum2)
cout << 1;
else
cout << 0;
return 0;
}
a = vector<int>(cc);
rec(0);
printf("%.9lf", (double)rr / (double)all);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll n,m;
cin >> n >> m;
ll N=n;
map<ll,ll> mp;
while(n--)
{
ll x;
cin >> x;
//if(mp.find(x)==mp.end())mp[x]=0LL;
mp[x]++;
}
ll M = m;
while (m--)
{
ll b,c;
cin >> b >> c;
//if(mp.find(c)==mp.end())mp[c]=0LL;
mp[c] += b;
}
ll sum = 0;
//auto itr = mp.end();
ll cnt = 0;
auto i = mp.end();
i--;
for(;;)
{
if((*i).second==0)i--;
sum+=(*i).first;
(*i).second--;
//cout<<(*i).first<<';'<<(*i).second<<endl;
if(++cnt==N)break;
}
cout<<sum<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void input(T &x) {
register char c = getchar();
x = 0;
int neg = 0;
for (; ((c < 48 || c > 57) && c != '-'); c = getchar())
;
if (c == '-') {
neg = 1;
c = getchar();
}
for (; c > 47 && c < 58; c = getchar()) {
x = (x << 1) + (x << 3) + c - 48;
}
if (neg) x = -x;
}
inline long long bigmod(long long p, long long e, long long M) {
long long ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % M;
p = (p * p) % M;
}
return ret;
}
template <class T>
inline T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
return bigmod(a, M - 2, M);
}
const int N = 1e5 + 5;
char s[N];
char cor(char c) {
if (c == ')') return '(';
if (c == '}') return '{';
if (c == ']') return '[';
}
int P[N];
int main() {
scanf("%s", s);
for (int i = 0; s[i]; i++) {
P[i] = (i > 0 ? P[i - 1] : 0);
if (s[i] == '[') P[i]++;
}
stack<pair<char, int> > st;
vector<pair<int, int> > Cor;
for (int i = 0; s[i]; i++) {
if (s[i] == '(' || s[i] == '{' || s[i] == '[') {
st.push(make_pair(s[i], i));
} else {
int v = cor(s[i]);
if (st.size() && st.top().first == v) {
Cor.push_back(make_pair(st.top().second, i));
st.pop();
} else
while (st.size()) st.pop();
}
}
map<int, int> L, R;
for (auto x : Cor) R[x.first] = x.second, L[x.second] = x.first;
for (int i = 0; i < Cor.size(); i++) {
int x = Cor[i].first, y = Cor[i].second;
int l = x, r = y;
if (L.count(x - 1)) l = min(l, L[x - 1]);
if (R.count(y + 1)) r = max(r, R[y + 1]);
L[y] = min(L[y], l);
R[x] = max(R[x], r);
}
Cor.clear();
for (auto x : L) Cor.push_back(make_pair(x.second, x.first));
int ans = 0;
pair<int, int> I = make_pair(1, 0);
for (int i = 0; i < Cor.size(); i++) {
int x = Cor[i].first, y = Cor[i].second;
int v = P[y] - (x > 0 ? P[x - 1] : 0);
if (v > ans) ans = v, I = Cor[i];
}
cout << ans << endl;
for (int i = I.first; i <= I.second; i++) printf("%c", s[i]);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long a[110000];
struct UnionFind {
vector<int> parent, activated;
vector<long long> group_size;
long long mx = 0;
UnionFind(int n) {
group_size = vector<long long>(n);
parent = vector<int>(n);
activated = vector<int>(n);
for (int i = 0; i < n; i++) {
parent[i] = i;
group_size[i] = a[i];
activated[i] = 0;
}
}
int find_set(int node) {
if (parent[node] == node) return node;
return parent[node] = find_set(parent[node]);
}
bool same_group(int node1, int node2) {
int leader1 = find_set(node1);
int leader2 = find_set(node2);
return leader1 == leader2;
}
void merge_groups(int node1, int node2) {
if (!activated[node1] || !activated[node2]) return;
int leader1 = find_set(node1);
int leader2 = find_set(node2);
if (group_size[leader1] > group_size[leader2]) {
parent[leader2] = leader1;
group_size[leader1] += group_size[leader2];
} else {
parent[leader1] = leader2;
group_size[leader2] += group_size[leader1];
}
}
long long get_size(int node) { return group_size[find_set(node)]; }
void activate(int node) {
activated[node] = 1;
if (node + 1 < parent.size()) merge_groups(node, node + 1);
if (node - 1 >= 0) merge_groups(node, node - 1);
mx = max(mx, get_size(node));
}
};
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int order[n];
for (int i = 0; i < n; i++) cin >> order[i];
vector<long long> sol;
UnionFind uf(n);
for (int i = n - 1; i >= 0; i--) {
sol.push_back(uf.mx);
uf.activate(order[i] - 1);
}
for (int i = n - 1; i >= 0; i--) cout << sol[i] << '\n';
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
bool isBelong(int a[], int x) {
for (int i = 0; i < n; i++) {
if (a[i] == x) return true;
}
return false;
}
int main() {
scanf("%d", &n);
int a[1000], b[1000];
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i < n; i++) scanf("%d", &b[i]);
int res[1000];
int index = 0;
int Count = 0;
for (int i = 0; i < n; i++) {
if (a[i] == b[i])
res[i] = a[i];
else {
res[i] = 0;
index = i;
Count++;
}
}
int dp[100000];
for (int i = 0; i < n; i++) {
dp[res[i]] = 1;
}
vector<int> temp;
for (int i = 1; i <= n; i++) {
if (dp[i] != 1) temp.push_back(i);
}
bool check = false;
if (Count == 1) {
for (int i = 1; i <= n; i++)
if (dp[i] != 1) res[index] = i;
} else {
for (int i = 0; i < n; i++) {
if (res[i] == 0) {
res[i] = temp[0];
res[index] = temp[1];
if ((res[i] != a[i] && res[i] != b[i]) ||
(res[index] != a[index] && res[index] != b[index])) {
res[i] = temp[1];
res[index] = temp[0];
break;
} else
break;
}
}
}
for (int i = 0; i < n; i++) printf("%d ", res[i]);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int X,Y,Z;
cin>>X>>Y>>Z;
cout<<Z<<" "<<X<<" "<<Y;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long infl = 1e18 + 5;
long long int n, m, k, q, x, y, mx = -1, mn = infl, f, val, sz, sm, cnt, ans,
t = 1, i, j, ind = -1;
long long int a[1000004], b[1000004], c[1000004];
std::set<int> v;
std::map<long long int, long long int> mrk;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if (fopen("inp.txt", "r")) {
freopen("myfile.txt", "w", stdout);
freopen("inp.txt", "r", stdin);
}
cin >> k >> n;
for (i = 0; i < k; i++) {
cin >> a[i];
c[i + 1] += c[i] + a[i];
}
for (i = 0; i < n; i++) {
cin >> b[i];
mrk[b[i]] = 1;
}
for (i = 0; i < k; i++) {
v.insert(b[0] - c[i + 1]);
}
for (auto it : v) {
x = it;
cnt = 0;
map<long long int, long long int> cp;
for (j = 0; j < k; j++) {
y = x + c[j + 1];
if (mrk.find(y) != mrk.end() && cp.find(y) == cp.end()) {
cnt++;
cp[y] = 1;
}
}
if (cnt == n) ans++;
}
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
mt19937 gen(time(0));
int n, m;
set<int> s;
int cnt[200005], wa[200005];
vector<int> edge[200005];
queue<int> que;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0, a, b; i < m; i++) {
cin >> a >> b;
edge[a - 1].push_back(b - 1);
edge[b - 1].push_back(a - 1);
}
for (int i = 0; i < n; i++) {
cin >> wa[i];
if (wa[i] == 0) que.push(i);
}
while (!que.empty()) {
auto u = que.front();
que.pop();
if (s.count(u)) continue;
s.insert(u);
cnt[u]++;
for (auto i : edge[u]) {
cnt[i]++;
if (cnt[i] == wa[i]) {
que.push(i);
}
}
}
for (int i = 0; i < n; i++) {
if (wa[i] == cnt[i]) {
cout << -1 << '\n';
return 0;
}
}
cout << s.size() << '\n';
for (auto i : s) cout << i + 1 << " ";
cout << '\n';
return 0;
}
| 4 |
#include<cstdio>
int main(){
int H,W,A,B;
scanf("%d%d%d%d",&H,&W,&A,&B);
for(int i=1;i<=H;i++,puts(""))
for(int j=1;j<=W;j++)
putchar((i<=B)==(j<=A)?'1':'0');
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
double cross(pair<int, int> A, pair<int, int> B, pair<int, int> C) {
double tmp;
tmp = ((B.first - A.first) * (C.second - B.second) -
(C.first - B.first) * (B.second - A.second));
tmp = tmp * 0.5;
return tmp;
}
pair<int, int> p[500];
double area, maxminus, maxplus, tmp;
int main() {
int n;
cin >> n;
area = 0;
for (int i = 1; i <= n; i++) {
cin >> p[i].first >> p[i].second;
}
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
maxminus = -1;
maxplus = -1;
for (int k = 1; k <= n; k++) {
if (k == i || k == j) continue;
tmp = cross(p[i], p[j], p[k]);
if (tmp <= 0) {
maxminus = max(maxminus, -tmp);
} else {
maxplus = max(maxplus, tmp);
}
}
if (maxplus >= 0 && maxminus >= 0) area = max(area, maxplus + maxminus);
}
}
cout << fixed << setprecision(12) << area << endl;
}
| 2 |
#include <bits/stdc++.h>
const int N = 1e4 + 10;
int n, cnt[N];
int q[N], tot;
bool v[N];
bool cmp(int a, int b) { return a < b; }
int main() {
int T;
scanf("%d", &T);
while (T--) {
for (int i = 1; i <= 10000; i++) cnt[i] = 0;
tot = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
cnt[x]++;
if (cnt[x] >= 2 && !v[x]) {
v[x] = 1;
q[++tot] = x;
}
}
std::sort(q + 1, q + 1 + tot, cmp);
int x, y;
double ans = 1e9;
for (int i = 1; i <= tot; i++) {
if (cnt[q[i]] >= 4) {
x = y = q[i];
ans = 0;
}
if (i < tot && q[i] * 1.0 / q[i + 1] + q[i + 1] * 1.0 / q[i] < ans) {
ans = q[i] * 1.0 / q[i + 1] + q[i + 1] * 1.0 / q[i];
x = q[i];
y = q[i + 1];
}
v[q[i]] = 0;
}
printf("%d %d %d %d\n", x, x, y, y);
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, num, numbers[100005], cant;
set<int> sol, aux;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &num);
cant = 0;
for (auto i = aux.begin(); i != aux.end(); i++) {
numbers[cant++] = (*i | num);
}
aux.clear();
numbers[cant++] = num;
aux.insert(numbers, numbers + cant);
sol.insert(aux.begin(), aux.end());
}
printf("%d\n", sol.size());
return 0;
}
| 1 |
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;
typedef int Weight;
struct Edge {
int src, dst;
Weight weight;
Edge(int src, int dst, Weight weight) :
src(src), dst(dst), weight(weight) { }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
Graph g;
vector<bool> visited;
vector<int> v;
int solve(int now, int sum) {
int res = 0;
visited[now] = 1;
// cout << string(nest,' ');
// printf("%d %d\n", now,modoru);
REP(i,g[now].size()) {
int d = g[now][i].dst;
if (!visited[d] && g[d].size() > 1) {
res += g[now][i].weight + solve(d, sum + g[now][i].weight);
} else if (!visited[d] && g[d].size() == 1) {
v.push_back(sum);
}
}
// cout << res << endl;
return res;
}
int main() {
int n;
while(cin >> n,n) {
g = Graph(n);
visited.assign(n,0);
REP(i,n-1) {
int a,b,c;
cin >> a>>b>>c;
a--;b--;
g[a].push_back(Edge(a,b,c));
g[b].push_back(Edge(b,a,c));
}
v.clear();
int res = solve(0,0)*2;
sort(ALL(v),greater<int>());
// FOR(it,v)
// cout <<*it << " ";
cout << res - v[0] << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j;
int n, b, a;
cin >> n >> b >> a;
int bn = b, an = a;
vector<int> s(n + 1);
for (i = 0; i < n; i++) cin >> s[i];
for (i = 0; i < n; i++) {
if (bn == 0 && an == 0) {
break;
} else if (bn == 0) {
an--;
} else if (an == 0) {
bn--;
if (s[i] == 1) an++;
} else {
if (s[i] == 1) {
if (an == a) {
an--;
} else {
bn--;
an++;
}
} else {
an--;
}
}
}
cout << i << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200009;
const int MOD = 1e9 + 7;
void add(int &a, int b) {
a += b;
if (a >= MOD) a -= MOD;
}
vector<vector<int>> mul(vector<vector<int>> &A, vector<vector<int>> &B) {
int n = ((int)(A).size());
vector<vector<int>> res(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
add(res[i][j], 1ll * A[i][k] * B[k][j] % MOD);
}
}
}
return res;
}
int main() {
ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
int n, x;
cin >> n >> x;
string s;
cin >> s;
s += "#";
n++;
vector<int> pi(n);
for (int i = 1; i < n; ++i) {
int j = pi[i - 1];
while (j && s[i] != s[j]) j = pi[j - 1];
if (s[i] == s[j]) j++;
pi[i] = j;
}
vector<vector<int>> aut(n, vector<int>(2));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 2; ++j) {
if (i > 0 && '0' + j != s[i])
aut[i][j] = aut[pi[i - 1]][j];
else
aut[i][j] = i + (s[i] == '0' + j);
}
}
vector<vector<vector<int>>> cal;
vector<vector<int>> Mat(n + 1, vector<int>(n + 1, 0));
for (int i = 0; i < n; ++i) {
Mat[i][i]++;
Mat[i][aut[i][0]]++;
if (aut[i][0] == n - 1) Mat[i][n]++;
}
Mat[n][n] += 2;
cal.push_back(Mat);
Mat.assign(n + 1, vector<int>(n + 1, 0));
for (int i = 0; i < n; ++i) {
Mat[i][i]++;
Mat[i][aut[i][1]]++;
if (aut[i][1] == n - 1) Mat[i][n]++;
}
Mat[n][n] += 2;
cal.push_back(Mat);
for (int i = 2; i <= x; ++i) cal.push_back(mul(cal[i - 1], cal[i - 2]));
cout << cal[x][0][n] << '\n';
return 0;
}
| 6 |
#include <iostream>
using namespace std;
int main(){
string s;
cin >> s;
int a,b,c,d;
a=(int)(s[0]-'0');
b=(int)(s[1]-'0');
c=(int)(s[2]-'0');
d=(int)(s[3]-'0');
for(int i=0;i<8;i++){
if(a+((i&1)*2-1)*b+(((i>>1)&1)*2-1)*c+(((i>>2)&1)*2-1)*d==7){
cout << a << ((i&1)?'+':'-') << b << ((i&2)?'+':'-') << c << ((i&4)?'+':'-') << d << "=7" << endl;
return 0;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int zerkalo(int n) {
long long int nn = n, l = 0;
long long int step = 128;
long long int res = 0;
while (nn != 0) {
nn /= 2;
l++;
}
nn = 0;
while (nn < 8) {
nn++;
if (nn <= l) {
res += step * (n % 2);
n /= 2;
}
step /= 2;
}
return res % 256;
}
int main() {
string s;
getline(cin, s);
int pred = 0;
for (int i = 0; i < s.length(); i++) {
int k = zerkalo(int(s[i]));
if (pred >= k)
cout << pred - k << endl;
else
cout << 256 - abs(pred - k) << endl;
pred = k;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
class HashNum {
private:
inline void normalize() {
a_ = (a_ % MA + MA) % MA;
b_ = (b_ % MB + MB) % MB;
}
public:
int a_, b_;
static constexpr int MA = 1e9 + 7, MB = 1e9 + 9;
HashNum(int num = 0) : a_(num), b_(num) { normalize(); }
HashNum(int first, int second) : a_(first), b_(second) { normalize(); }
friend inline HashNum& operator+=(HashNum& lhs, const HashNum& rhs) {
lhs.a_ += rhs.a_;
lhs.b_ += rhs.b_;
lhs.normalize();
return lhs;
}
friend inline HashNum operator+(const HashNum& lhs, const HashNum& rhs) {
return HashNum(lhs.a_ + rhs.a_, lhs.b_ + rhs.b_);
}
friend inline HashNum& operator-=(HashNum& lhs, const HashNum& rhs) {
lhs.a_ -= rhs.a_;
lhs.b_ -= rhs.b_;
lhs.normalize();
return lhs;
}
friend inline HashNum operator-(const HashNum& lhs, const HashNum& rhs) {
return HashNum(lhs.a_ - rhs.a_, lhs.b_ - rhs.b_);
}
friend inline HashNum& operator*=(HashNum& lhs, const HashNum& rhs) {
lhs.a_ = (static_cast<long long>(lhs.a_) * rhs.a_) % MA;
lhs.b_ = (static_cast<long long>(lhs.b_) * rhs.b_) % MB;
return lhs;
}
friend inline HashNum operator*(const HashNum& lhs, const HashNum& rhs) {
HashNum ans(lhs);
ans *= rhs;
return ans;
}
inline bool operator==(const HashNum& rhs) const {
return a_ == rhs.a_ && b_ == rhs.b_;
}
inline bool operator<(const HashNum& rhs) const {
return std::make_pair(a_, b_) < std::make_pair(rhs.a_, rhs.b_);
}
inline explicit operator long long() const {
return static_cast<long long>(a_) * MB + b_ + 1;
}
friend std::ostream& operator<<(std::ostream& output, const HashNum& hash) {
output << hash.a_ << " " << hash.b_;
return output;
}
};
template <typename T_Type>
inline const T_Type GCD(T_Type first, T_Type second, T_Type& coeff_first,
T_Type& coeff_second) {
if (first == 0) {
coeff_first = 0;
coeff_second = 1;
return second;
}
T_Type tmp_coeff_first, tmp_coeff_second;
T_Type ans = GCD(second % first, first, tmp_coeff_first, tmp_coeff_second);
coeff_first = tmp_coeff_second - (second / first) * tmp_coeff_first;
coeff_second = tmp_coeff_first;
return ans;
}
template <typename T_Type>
inline const T_Type UnitElement(T_Type remainder, T_Type modulo) {
T_Type first, second;
GCD(remainder, modulo, first, second);
return (first % modulo + modulo) % modulo;
}
int main() {
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(7);
int n;
long long k;
std::cin >> n >> k;
++n;
std::vector<long long> a(n);
std::vector<HashNum> hashes(n + 1, HashNum(1));
HashNum big;
for (size_t i = 0; i < n; ++i) {
hashes[i + 1] = 2 * hashes[i];
std::cin >> a[i];
big += hashes[i] * a[i];
}
int MA = big.MA, MB = big.MB;
int ans = 0;
for (size_t i = 0; i < n; ++i) {
HashNum cur_hash = big - hashes[i] * a[i];
long long t1 = (cur_hash * UnitElement(MA - hashes[i].a_, MA)).a_;
long long t2 = (cur_hash * UnitElement(MB - hashes[i].b_, MB)).b_;
std::vector<long long> f{t1, t1 - MA};
std::vector<long long> s{t2, t2 - MB};
bool suc = false;
long long tmp = 0;
for (size_t ff = 0; ff < 2; ff++) {
for (size_t ss = 0; ss < 2; ss++) {
if (f[ff] <= k && f[ff] >= -k && f[ff] == s[ss]) {
suc = true;
tmp = f[ff];
break;
}
}
}
if (suc) {
if (i == n - 1 && tmp == 0) {
} else {
++ans;
}
}
}
std::cout << ans << std::endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class T, class L>
bool smax(T &x, L y) {
return x < y ? (x = y, 1) : 0;
}
template <class T, class L>
bool smin(T &x, L y) {
return y < x ? (x = y, 1) : 0;
}
const int maxn = 1e7 + 17, tof = 1e7;
pair<int, long long> ans;
long long po(int x) { return (long long)x * x * x; }
void go(long long x, int k, long long s) {
if (x == 0) {
smax(ans, pair<int, long long>(k, s));
return;
}
int l = 0;
while (po(l + 1) <= x) l++;
go(x - po(l), k + 1, s + po(l));
if (l) go(po(l) - po(l - 1) - 1, k + 1, s + po(l - 1));
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
long long n;
cin >> n;
go(n, 0, 0);
cout << ans.first << ' ' << ans.second << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 50 + 5;
int n, h[maxn], ecnt;
struct enode {
int v, n;
enode() {}
enode(int _v, int _n) : v(_v), n(_n) {}
} e[maxn << 1];
inline void addedge(int u, int v) {
ecnt++;
e[ecnt] = enode(v, h[u]);
h[u] = ecnt;
}
double f[maxn][maxn], g[maxn], dp[maxn], fac[maxn];
int sz[maxn];
double C(int n, int m) {
if (n < 0 || m < 0 || n < m) return 0;
return fac[n] / fac[m] / fac[n - m];
}
void dfs(int u, int fa) {
sz[u] = 1;
for (int i = h[u]; ~i; i = e[i].n) {
int v = e[i].v;
if (v == fa) continue;
dfs(v, u);
sz[u] += sz[v];
}
int now = 1;
f[u][0] = 1;
for (int i = h[u]; ~i; i = e[i].n) {
int v = e[i].v;
if (v == fa) continue;
memset(g, 0, sizeof(g));
memset(dp, 0, sizeof(dp));
for (int j = 0; j <= sz[v]; j++) {
for (int k = 0; k <= j; k++) {
if (k < j)
g[j] = (double)g[j] + 0.5 * f[v][k];
else
g[j] = (double)g[j] + 1.0 * (sz[v] - j) * f[v][k];
}
}
for (int j = 0; j < now; j++) {
for (int k = 0; k <= sz[v]; k++) {
dp[k + j] =
(double)dp[k + j] + 1.0 * f[u][j] * g[k] * C(j + k, k) *
C(now + sz[v] - 1 - k - j, now - 1 - j);
}
}
for (int j = 0; j < now + sz[v]; j++) f[u][j] = (double)dp[j];
now += sz[v];
}
}
int main() {
scanf("%d", &n);
ecnt = 0;
memset(h, -1, sizeof(h));
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
addedge(u, v);
addedge(v, u);
}
fac[0] = 1;
for (int i = 1; i <= 50; i++) fac[i] = (double)fac[i - 1] * i;
for (int i = 1; i <= n; i++) {
memset(f, 0, sizeof(f));
dfs(i, 0);
double Ans = (double)f[i][n - 1] / fac[n - 1];
printf("%.9lf\n", Ans);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
long long MOD = 1000000007;
long double EPS = 1e-9;
long long binpow(long long b, long long p, long long mod) {
long long ans = 1;
b %= mod;
for (; p; p >>= 1) {
if (p & 1) ans = ans * b % mod;
b = b * b % mod;
}
return ans;
}
void pre() {}
template <int MOD>
struct mod_int {
static const int mod = MOD;
unsigned x;
mod_int() : x(0) {}
mod_int(int sig) {
int sigt = sig % MOD;
if (sigt < 0) sigt += MOD;
x = sigt;
}
mod_int(long long sig) {
int sigt = sig % MOD;
if (sigt < 0) sigt += MOD;
x = sigt;
}
int get() const { return (int)x; }
mod_int& operator+=(mod_int that) {
if ((x += that.x) >= MOD) x -= MOD;
return *this;
}
mod_int& operator-=(mod_int that) {
if ((x += MOD - that.x) >= MOD) x -= MOD;
return *this;
}
mod_int& operator*=(mod_int that) {
x = (unsigned long long)x * that.x % MOD;
return *this;
}
mod_int& operator/=(mod_int that) { return *this *= that.inverse(); }
mod_int operator+(mod_int that) const { return mod_int(*this) += that; }
mod_int operator-(mod_int that) const { return mod_int(*this) -= that; }
mod_int operator*(mod_int that) const { return mod_int(*this) *= that; }
mod_int operator/(mod_int that) const { return mod_int(*this) /= that; }
mod_int inverse() const {
long long a = x, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
return mod_int(u);
}
};
template <int MOD>
istream& operator>>(istream& is, mod_int<MOD>& val) {
long long x;
is >> x;
val = x;
return is;
}
template <int MOD>
ostream& operator<<(ostream& os, const mod_int<MOD>& val) {
os << val.get();
return os;
}
using mint = mod_int<1000000007>;
template <typename T>
T binpow(T b, long long k) {
T ans = T(1);
for (; k; k >>= 1) {
if (k & 1) ans = ans * b;
b = b * b;
}
return ans;
}
int n, m;
vector<string> arr;
mint dp[2020][2020][2];
mint delta[2020][2020][2];
int rowcnt[2020][2020];
int colcnt[2020][2020];
void solve() {
cin >> n >> m;
arr.resize(n);
for (long long i = 0; i < (n); ++i) cin >> arr[i];
for (long long i = 0; i < (n); ++i)
for (long long j = 0; j < (m); ++j) {
if (arr[i][j] == 'R') {
rowcnt[i][j] = 1;
colcnt[i][j] = 1;
}
}
for (long long i = n - 1; i >= 0; i--) {
for (long long j = m - 1; j >= 0; j--) {
colcnt[i][j] += colcnt[i][j + 1];
rowcnt[i][j] += rowcnt[i + 1][j];
}
}
if (n == 1 & m == 1) {
cout << 1 << '\n';
return;
}
dp[0][0][0] = 1;
dp[0][0][1] = 1;
for (long long i = 0; i < (n); ++i)
for (long long j = 0; j < (m); ++j) {
dp[i][j][0] += delta[i][j][0];
delta[i][j + 1][0] += delta[i][j][0];
dp[i][j][1] += delta[i][j][1];
delta[i + 1][j][1] += delta[i][j][1];
{
int blcnt = rowcnt[i + 1][j];
int lo = i + 1, hi = n - 1 - blcnt;
if (lo <= hi) {
delta[lo][j][1] += dp[i][j][0];
delta[hi + 1][j][1] -= dp[i][j][0];
}
}
{
int blcnt = colcnt[i][j + 1];
int lo = j + 1, hi = m - 1 - blcnt;
if (lo <= hi) {
delta[i][lo][0] += dp[i][j][1];
delta[i][hi + 1][0] -= dp[i][j][1];
}
}
}
cout << dp[n - 1][m - 1][0] + dp[n - 1][m - 1][1] << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
pre();
long long _t = 1;
for (long long i = 1; i <= _t; i++) {
solve();
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void pr(vector<T> &v) {
for (int i = 0; i < (int)(v).size(); i++) cout << v[i] << " ";
cout << '\n';
;
}
template <typename T>
void pr(vector<vector<T>> &v) {
for (int i = 0; i < (int)(v).size(); i++) {
pr(v[i]);
}
}
template <typename T>
void re(T &x) {
cin >> x;
}
template <typename T>
void re(vector<T> &a) {
for (int i = 0; i < (int)(a).size(); i++) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &...rest) {
re(first);
re(rest...);
}
template <typename T>
void pr(T x) {
cout << x << '\n';
;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &...rest) {
cout << first << " ";
pr(rest...);
cout << '\n';
;
}
void ps() {
cout << '\n';
;
}
template <class T, class... Ts>
void ps(const T &t, const Ts &...ts) {
cout << t;
if (sizeof...(ts)) cout << " ";
ps(ts...);
}
const long long MOD = 998244353;
long double PI = 4 * atan(1);
long double eps = 1e-12;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
int adjmat[410][410] = {};
vector<vector<int>> adjlist1(410);
vector<vector<int>> adjlist2(410);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adjmat[u][v] = 1;
adjmat[v][u] = 1;
}
for (int i = 1; i < n + 1; i++) {
for (int j = 1; j < n + 1; j++) {
if (adjmat[i][j] == 1) {
adjlist1[i].push_back(j);
} else {
adjlist2[i].push_back(j);
}
}
}
int len = 0;
int dis[410] = {};
for (int i = 0; i < 410; i++) {
dis[i] = MOD;
}
if (adjmat[1][n] == 1) {
queue<pair<int, int>> q;
q.push(make_pair(1, 0));
dis[1] = 0;
while (!q.empty()) {
pair<int, int> curr = q.front();
q.pop();
for (int next : adjlist2[curr.first]) {
if (dis[next] == MOD) {
dis[next] = curr.second + 1;
q.push(make_pair(next, dis[next]));
}
}
}
cout << ((dis[n] == MOD) ? -1 : dis[n]) << '\n';
;
} else {
queue<pair<int, int>> q;
q.push(make_pair(1, 0));
dis[1] = 0;
while (!q.empty()) {
pair<int, int> curr = q.front();
q.pop();
for (int next : adjlist1[curr.first]) {
if (dis[next] == MOD) {
dis[next] = curr.second + 1;
q.push(make_pair(next, dis[next]));
}
}
}
cout << ((dis[n] == MOD) ? -1 : dis[n]) << '\n';
;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-9;
const ll MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
for(int i=0;i<(int)(s.size());++i) {
cout << s[i];
if(i == (int)(s.size())-1) cout << endl;
else cout << " ";
}
}
template<typename T1, typename T2>
ostream& operator<<(ostream &os, const pair<T1, T2> p) {
os << p.first << ":" << p.second;
return os;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
int m, n; cin >> m >> n;
vector<vector<double>> p(m, vector<double>(n));
for(int i=0;i<m;++i) {
for(int j=0;j<n;++j) {
cin >> p[i][j];
}
}
vector<vector<vector<double>>> dp(m, vector<vector<double>> (2, vector<double>(1<<m, 0.0)));
for(int i=0;i<m;++i) {
for(int k=0;k<(1<<m);++k) {
dp[i][n%2][k] = 1;
}
}
for(int j=n-1;j>=0;--j) {
int nj = j % 2;
for(int k=(1<<m)-1;k>=0;--k) {
for(int i=0;i<m;++i) {
if (!((k >> i) & 1)) {
continue;
}
for (int l = 0; l < m; ++l) {
if (!((k >> l) & 1)) dp[i][nj][k] = max(dp[i][nj][k], (1 - p[i][j]) * dp[l][nj][k | (1 << l)]);
}
dp[i][nj][k] += p[i][j] * dp[i][!nj][k];
}
}
for(int i=0;i<m;++i) {
for(int k=0;k<(1<<m);++k) {
dp[i][!nj][k] = 0;
}
}
}
double ans = 0.0;
for(int i=0;i<m;++i) {
for(int k=0;k<(1<<m);++k) {
ans = max(ans, dp[i][0][k]);
}
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int MOD = (int)1e9 + 7;
const int N = (int)2e5 + 911;
int mult(int a, int b){
return (a * 1ll * b) % MOD;
}
int add(int a, int b){
a += b;
if(a >= MOD) a -= MOD;
return a;
}
int powr(int n, int k){
if(k == 0) return 1;
int p = powr(n,k/2);
p = mult(p, p);
if((k&1)) p = mult(p, n);
return p;
}
int fac[N];
int ncr(int n, int k){
return mult(fac[n],mult(powr(fac[k],MOD-2),powr(fac[n-k],MOD-2)));
}
int f[N];
int g[N];
int main(){
fastIO;
fac[0]=1;
for(int i = 1; i < N ; i ++ ){
fac[i]=mult(fac[i-1],i);
}
int n, a, b, c;
cin >> n >> a >> b >> c;
for(int i = 1; i <= 2*n-1; i ++ ){
f[i]=mult(i,mult(100,powr(100-c,MOD-2)));
}
int i100 = powr(a+b, MOD-2);
int ia, ib;
for(int i = n; i <= 2 * n - 1; i ++ ){
g[i]=ncr(i-1,n-1);
ia = mult(powr(mult(a,i100),n),powr(mult(b,i100),i-n));
ib = mult(powr(mult(b,i100),n),powr(mult(a,i100),i-n));
g[i] = mult(g[i], add(ia, ib));
}
int ans = 0;
for(int i = n; i <= 2 * n - 1; i ++ ){
ans = add(ans, mult(g[i],f[i]));
}
cout << ans << "\n";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int k1 = 0;
string s0 = "";
for (int i = 0; i < n; i++) {
if (s[i] == '0')
s0 += "0";
else
k1++;
}
if (k1 == 0) {
cout << "0" << endl;
return 0;
} else {
cout << "1" << s0 << endl;
}
return 0;
}
| 1 |
#include <algorithm>
#include <iostream>
#include <string>
#include <random>
#include <cassert>
#include <cstring>
#include <chrono>
#include <map>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
map<string, ll> ans;
ll Solve(string s) {
if (s == "") return 1;
if (ans.count(s)) return ans[s];
ans[s] = Solve(s.substr(1));
if (s[0] == '1') {
ans[s] = ans[s] * 2 % mod;
}
for (int t = 1; t < s.size(); ++t) {
for (int k = 2; k * t <= s.size(); ++k) {
string ss(t, '1');
for (int i = 0; i < t * k; ++i) {
if (s[i] == '0') ss[i % t] = '0';
}
ans[s] += Solve(ss) * Solve(s.substr(t * k));
ans[s] %= mod;
}
}
return ans[s];
}
int main()
{
string s;
cin >> s;
cout << Solve(s) << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int x, y, _x, _y, n, tx, ty;
string s;
bool judge(long long num) {
long long yu = num % n;
long long ax = (num / n) * tx;
long long ay = (num / n) * ty;
long long px = x + ax;
long long py = y + ay;
for (int i = 0; i < yu; i++) {
if (s[i] == 'U')
py++;
else if (s[i] == 'D')
py--;
else if (s[i] == 'L')
px--;
else
px++;
}
long long sum = 1ll * abs(px - 1ll * _x) + 1ll * abs(py - 1ll * _y);
return num >= sum;
}
int main() {
cin >> x >> y >> _x >> _y;
cin >> n >> s;
for (int i = 0; i < n; i++) {
if (s[i] == 'U')
ty++;
else if (s[i] == 'D')
ty--;
else if (s[i] == 'L')
tx--;
else
tx++;
}
long long l = 1, r = 1e18;
while (l <= r) {
long long m = l + r >> 1;
if (judge(m)) {
r = m - 1;
} else
l = m + 1;
}
if (r >= 1e18)
cout << -1 << endl;
else
cout << l << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, n, m;
cin >> n >> m >> k;
if (m < n) {
cout << "NO";
return 0;
}
if (k < n) {
cout << "NO";
return 0;
}
cout << "YES";
return 0;
}
| 1 |
#include<cstdio>
#include<cctype>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define REP(i, N) for(int i=0;i<(int)(N);++i)
int unit[20];
int need[20];
int nextInt() {int r = 0, ch; while( isspace(ch = getchar())); do {r = r * 10 + ch - '0';}while( isdigit( ch = getchar() ) ); return r;}
bool visited[1 << 20];
struct State {
int one, mask, have;
// bool operator<(const State& t) const {
// if( one != t.one ) return one > t.one;
// return have < t.have;
// }
};
int solve(int N, int U) {
memset(visited, 0, sizeof(visited));
// priority_queue<State> up;
queue<State> up;
State s;
s.one = 0;
s.mask = 0;
s.have = 0;
visited[s.mask] = 1;
up.push(s);
while( !up.empty() ) {
s = up.front(); up.pop();
if( s.have >= U ) return s.one;
for(int i = 0; i < N; i++)
if ( visited[s.mask | (1 << i)] == 0 && (s.mask | need[i]) == s.mask )
{
State next;
next.mask = s.mask | (1 << i);
next.one = s.one + 1;
next.have = s.have + unit[i];
visited[next.mask] = 1;
up.push(next);
}
}
return -1;
}
int main() {
for(;;) {
int N, U;
N = nextInt();
U = nextInt();
if( N == 0 && U == 0 ) break;
REP(i, N) need[i] = 0;
int all = 0;
REP(i, N) {
unit[i] = nextInt();
all += unit[i];
int K = nextInt();
REP(k, K) {
int x = nextInt();
need[i] |= (1 << x);
}
}
if( all == U ) {
printf("%d\n", N);
continue;
}
int best = solve(N, U);
printf("%d\n", best);
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
typedef long long ll;
typedef pair<int,int> pii;
#define _overload4(_1,_2,_3,_4,name,...) name
#define _overload3(_1,_2,_3,name,...) name
#define _rep1(n) _rep2(i,n)
#define _rep2(i,n) _rep3(i,0,n)
#define _rep3(i,a,b) for(ll i=a;i<b;++i)
#define _rep4(i,a,b,c) for(ll i=a;i<b;i+=c)
#define rep(...) _overload4(__VA_ARGS__,_rep4,_rep3,_rep2,_rep1)(__VA_ARGS__)
#define _rrep1(n) _rrep2(i,n)
#define _rrep2(i,n) _rrep3(i,0,n)
#define _rrep3(i,a,b) for(ll i=b-1;i>=a;i--)
#define _rrep4(i,a,b,c) for(ll i=a+(b-a-1)/c*c;i>=a;i-=c)
#define rrep(...) _overload4(__VA_ARGS__,_rrep4,_rrep3,_rrep2,_rrep1)(__VA_ARGS__)
#define each(i,a) for(auto&& i:a)
#define sum(...) accumulate(range(__VA_ARGS__),0LL)
#define _range(i) (i).begin(),(i).end()
#define _range2(i,k) (i).begin(),(i).begin()+k
#define _range3(i,a,b) (i).begin()+a,(i).begin()+b
#define range(...) _overload3(__VA_ARGS__,_range3,_range2,_range)(__VA_ARGS__)
const ll LINF=0x3fffffffffffffff;
const ll MOD=1000000007;
const ll MODD=0x3b800001;
const int INF=0x3fffffff;
#define yes(i) out(i?"yes":"no")
#define Yes(i) out(i?"Yes":"No")
#define YES(i) out(i?"YES":"NO")
#define Possible(i)out(i?"Possible":"Impossible")
#define unless(a) if(!(a))
//#define START auto start=system_clock::now()
//#define END auto end=system_clock::now();cerr<<duration_cast<milliseconds>(end-start).count()<<" ms\n"
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)
#define vec(type,name,...) vector<type> name(__VA_ARGS__);
#define VEC(type,name,size) vector<type> name(size);in(name)
#define vv(type,name,h,w,value) vector<vector<type>>name(h,vector<type>(w,value))
#define pll pair<ll,ll>
struct SETTINGS{SETTINGS(){cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);};}SETTINGS;
template<typename T>
inline bool update_min(T& mn,const T& cnt){if(mn>cnt){mn=cnt;return 1;}else return 0;}
template<typename T>
inline bool update_max(T& mx,const T& cnt){if(mx<cnt){mx=cnt;return 1;}else return 0;}
template<typename T>
inline T max(vector<T>& vec){return *max_element(range(vec));}
template<typename T>
inline constexpr T gcd (T a,T b) {if(a==b)return a;else return gcd(b,(a-1)%b+1);}
inline void in() {}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec);
template<typename T,size_t size>
istream& operator >> (istream& is, array<T,size>& vec);
template<typename T,typename L>
istream& operator >> (istream& is, pair<T,L>& p);
template<typename T>
ostream& operator << (ostream& os, vector<T>& vec);
template<typename T,typename L>
ostream& operator << (ostream& os, pair<T,L>& p);
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){for(T& x: vec) is >> x;return is;}
template<typename T,typename L>
istream& operator >> (istream& is, pair<T,L>& p){is >> p.first;is >> p.second;return is;}
template<typename T>
ostream& operator << (ostream& os, vector<T>& vec){os << vec[0];rep(i,1,vec.size()){os << ' ' << vec[i];}return os;}
template<typename T>
ostream& operator << (ostream& os, deque<T>& vec){os << vec[0];rep(i,1,vec.size()){os << ' ' << vec[i];}return os;}
template<typename T,typename L>
ostream& operator << (ostream& os, pair<T,L>& p){os << p.first << " " << p.second;return os;}
template <class Head, class... Tail>
inline void in(Head&& head,Tail&&... tail){cin>>head;in(move(tail)...);}
template <typename T>
inline void out(T t){cout<<t<<'\n';}
inline void out(){cout<<'\n';}
template <class Head, class... Tail>
inline void out(Head head,Tail... tail){cout<<head<<' ';out(move(tail)...);}
signed main(){
VEC(int,a,12);
sort(range(a));
#define same(a,b,c,d) a==b&&b==c&&c==d
yes(same(a[0],a[1],a[2],a[3])&&same(a[4],a[5],a[6],a[7])&&same(a[8],a[9],a[10],a[11]));
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int f[N], a[N], ne[N], sz[N], n, tot, m, k, vis[N];
struct node {
int nu, nv, u, v;
friend bool operator<(const node a, const node b) {
if (a.nu == b.nu) {
return a.nv < b.nv;
}
return a.nu < b.nu;
}
} p[N];
void inint(int n) {
for (int i = 0; i <= n; i++) {
f[i] = i;
sz[i] = 1;
}
tot = 0;
}
int F(int x) { return f[x] == x ? x : F(f[x]); }
void mere(int x, int y) {
int xx = F(x), yy = F(y);
if (xx != yy) {
if (sz[xx] > sz[yy]) swap(xx, yy);
sz[yy] += sz[xx];
f[xx] = f[yy];
ne[++tot] = xx;
}
}
void roll_back(int l) {
while (tot > l) {
int now = ne[tot--];
sz[f[now]] -= sz[now];
f[now] = now;
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
inint(2 * n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
int cnt = 1;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
if (a[u] == a[v]) {
if (!vis[a[u]] && F(u) == F(v)) {
k--;
vis[a[u]] = 1;
} else {
mere(u, v + n);
mere(u + n, v);
}
} else {
if (a[u] > a[v]) swap(u, v);
p[cnt].u = u, p[cnt].v = v;
p[cnt].nv = a[v];
p[cnt++].nu = a[u];
}
}
long long ans = (long long)(k - 1) * k / 2;
sort(p + 1, p + cnt);
int i, j;
int Len = tot;
for (i = 1; i < cnt; i = j + 1) {
roll_back(Len);
for (j = i; j < cnt - 1; j++) {
if (p[j].nu != p[j + 1].nu || p[j].nv != p[j + 1].nv) {
break;
}
}
if (vis[p[j].nu] || vis[p[j].nv]) {
continue;
}
for (int kk = i; kk <= j; kk++) {
if (F(p[kk].u) == F(p[kk].v)) {
ans--;
break;
} else {
mere(p[kk].u, p[kk].v + n);
mere(p[kk].v, p[kk].u + n);
}
}
}
printf("%lld\n", ans);
return 0;
}
| 3 |
#include <bits//stdc++.h>
using namespace std;
int main(void){
int a,b,c,d,e;
cin>>a>>b>>c>>d>>e;
int ans;
if(a<b&&a<c)
ans=a;
else if(b<c)
ans=b;
else
ans=c;
if(d<e)
ans=ans+d;
else
ans=ans+e;
cout<<ans-50<<endl;
}
| 0 |
#include<stdio.h>
int a[3000000];
int main()
{
int n,i,j,s=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0,j=1;i<n;i++)
{
if(a[i]==j)
{
s++;j++;}
}
if(j==1)
printf("-1\n");
else
printf("%d\n",n-s);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
long long n, t, a[maxn];
int main() {
while (~scanf("%I64d %I64d", &n, &t)) {
long long ans = 0;
for (long long i = 1; i <= n; i++) {
scanf("%I64d", &a[i]);
}
for (long long i = 1; i <= n; i++) {
t -= (86400 - a[i]);
if (t <= 0) {
ans = i;
break;
}
}
printf("%I64d\n", ans);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2020;
const long long mod = 998244353;
const long long g = 3;
const double eps = 1e-8;
template <class T>
void Mul(T &x, T y) {
x = x * y % mod;
}
template <class T>
void Add(T &x, T y) {
x = (x + y) % mod;
}
template <class T>
void Sub(T &x, T y) {
x = (x - y + mod) % mod;
}
template <class T>
void Mn(T &x, T y) {
x = min(x, y);
}
template <class T>
void Mx(T &x, T y) {
x = max(x, y);
}
long long qpow(long long x, long long y = mod - 2) {
long long res = 1;
while (y) {
if (y & 1) Mul(res, x);
Mul(x, x), y >>= 1;
}
return res;
}
void Div(long long &x, long long y) { Mul(x, qpow(y)); }
int n, m, k, T, tax[N * N], tnum = 0;
pair<int, int> a[N], b[N];
pair<int, int> lim[N];
priority_queue<pair<int, int> > pq;
int calc(int v) {
int res = 0;
return res;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second;
for (int i = 1; i <= m; i++) cin >> b[i].first >> b[i].second;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (b[j].first >= a[i].first && b[j].second >= a[i].second) {
pq.push(pair<int, int>(b[j].second - a[i].second + 1,
b[j].first - a[i].first + 1));
tax[++tnum] = b[j].first - a[i].first + 1;
}
}
}
int ans = 1e9;
sort(tax + 1, tax + tnum + 1);
tnum = unique(tax + 1, tax + tnum + 1) - tax - 1;
if (!pq.empty()) Mn(ans, pq.top().first);
for (int i = 0, nw; i <= tnum; i++) {
nw = tax[i];
while (!pq.empty() && pq.top().second <= nw) pq.pop();
if (!pq.empty())
Mn(ans, nw + pq.top().first);
else
Mn(ans, nw);
}
cout << ans << '\n';
}
| 4 |
#include <bits/stdc++.h>
int main(void) {
int n;
long s;
scanf("%d%ld", &n, &s);
if (1.0 * s / n > s / n)
printf("%ld", s / n + 1);
else
printf("%ld", s / n);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m, x, y;
cin >> n >> m >> x >> y;
int two = -1, one = -1;
if (x < y) {
one = x;
} else {
two = y;
}
int dot1 = 0, dot2 = 0, p = 0;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
dot1 = 0, dot2 = 0;
for (int j = 0; j <= m;) {
if (j < m - 1 && s[j] == '.' && s[j + 1] == '.') {
dot2++;
j += 2;
} else if (s[j] == '.') {
dot1++;
j++;
} else
j++;
}
int sum1 = y * dot2 + dot1 * x;
int sum2 = x * dot2 * 2 + dot1 * x;
p += min(sum1, sum2);
}
cout << p << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int maxi = -1, maxiidx;
for (int i = 0; i < n; i++) {
if (s[i] - 'a' < maxi && maxi != -1) {
cout << "YES" << endl;
cout << maxiidx << " " << i + 1;
return 0;
}
if (s[i] - 'a' > maxi) {
maxiidx = i + 1;
maxi = s[i] - 'a';
}
}
cout << "NO";
return 0;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int a, x, y;
cin >> a;
while (a--) {
cin >> x >> y;
x = x - 1;
while (x--) {
y = y + 9;
}
cout << y << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long f = 1, w = 0;
char x = 0;
while (x < '0' || x > '9') {
if (x == '-') f = -1;
x = getchar();
}
while (x != EOF && x >= '0' && x <= '9') {
w = (w << 3) + (w << 1) + (x ^ 48);
x = getchar();
}
return w * f;
}
const long long N = 1e6 + 10;
long long n, num_edge, S;
long long m, num_Sdge, Time, Cnt, Top;
long long Low[N], Vis[N], Val[N], U[N];
long long head[N], Sead[N], Dfn[N], f[N];
long long V[N], D[N], Col[N], Dag[N], Stk[N];
struct Edge {
long long next, to, dis;
} edge[N], Sdge[N];
inline long long SM(long long W) {
long long X = (sqrt(1 + (W << 3)) - 1) / 2;
return W * (X + 1) - X * (X + 1) * (X + 2) / 6;
}
inline void Add(long long from, long long to, long long dis) {
edge[++num_edge].next = head[from];
edge[num_edge].dis = dis;
edge[num_edge].to = to;
head[from] = num_edge;
}
inline void Sdd(long long from, long long to, long long dis) {
Sdge[++num_Sdge].next = Sead[from];
Sdge[num_Sdge].dis = dis;
Sdge[num_Sdge].to = to;
Sead[from] = num_Sdge;
}
inline void Tarjan(long long pos) {
Dfn[pos] = Low[pos] = ++Time, Stk[++Top] = pos, Vis[pos] = 1;
for (long long i = head[pos]; i; i = edge[i].next)
if (!Dfn[edge[i].to])
Tarjan(edge[i].to), Low[pos] = min(Low[edge[i].to], Low[pos]);
else if (Vis[edge[i].to])
Low[pos] = min(Dfn[edge[i].to], Low[pos]);
if (Low[pos] == Dfn[pos]) {
long long Now;
++Cnt;
do Now = Stk[Top--], Vis[Now] = 0, Col[Now] = Cnt;
while (Now != pos);
}
}
signed main() {
n = read(), m = read();
for (long long i = 1; i <= m; i++)
U[i] = read(), V[i] = read(), D[i] = read(), Add(U[i], V[i], D[i]);
S = read();
for (long long i = 1; i <= n; i++)
if (!Dfn[i]) Tarjan(i);
for (long long i = 1; i <= m; i++)
if (Col[U[i]] == Col[V[i]]) Val[Col[U[i]]] += SM(D[i]);
for (long long i = 1; i <= n; i++)
for (long long j = head[i]; j; j = edge[j].next)
if (Col[i] != Col[edge[j].to])
Sdd(Col[i], Col[edge[j].to], edge[j].dis + Val[Col[edge[j].to]]),
Dag[Col[edge[j].to]]++;
queue<long long> Q;
for (long long i = 1; i <= Cnt; i++) f[i] = -(1LL << 60);
f[Col[S]] = Val[Col[S]];
for (long long i = 1; i <= Cnt; i++)
if (!Dag[i]) Q.push(i);
while (Q.size()) {
long long x = Q.front();
Q.pop();
for (long long i = Sead[x]; i; i = Sdge[i].next) {
f[Sdge[i].to] = max(f[Sdge[i].to], f[x] + Sdge[i].dis);
if (!(--Dag[Sdge[i].to])) Q.push(Sdge[i].to);
}
}
long long Max = 0;
for (long long i = 1; i <= Cnt; i++) Max = max(Max, f[i]);
printf("%lld", Max);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n, k;
pair<int, int> p[maxn];
set<pair<int, int> > s, t;
pair<int, int> operator+(pair<int, int> A, pair<int, int> B) {
return {A.first + B.first, A.second + B.second};
}
bool operator==(pair<int, int> A, pair<int, int> B) {
return A.first == B.first && A.second == B.second;
}
bool operator<(pair<int, int> A, pair<int, int> B) {
if (A.first != B.first) return A.first < B.first;
return A.second < B.second;
}
void check(int l, int r) {
int cnt = 0;
pair<int, int> point = p[l] + p[r];
l = 1, r = n;
while (l <= r) {
if (point == p[l] + p[r]) {
l++, r--;
} else if (point < p[l] + p[r]) {
cnt++, r--;
} else {
cnt++, l++;
}
}
if (cnt <= k) s.insert(point);
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d %d", &p[i].first, &p[i].second);
if (n <= k) return !printf("-1\n");
sort(p + 1, p + 1 + n);
for (int i = 1; i <= k + 1; i++)
for (int j = n - k; j <= n; j++) check(i, j);
printf("%d\n", (int)s.size());
for (auto i : s)
printf("%.8lf %.8lf\n", (double)i.first / 2.0, (double)i.second / 2.0);
}
| 6 |
#include<iostream>
using namespace std;
int main(){
int n,sum,box[]={500,100,50,10,5,1};
while(cin>>n,n){
n=1000-n;
sum=0;
for(int i=0;i<6;i++){
sum+=n/box[i];
n=n-n/box[i]*box[i];
}
cout<<sum<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
const ll inf=1e9+7;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t;
cin>>t;
while(t--)
{
string s;
cin>>s;
bool a=true;
for(ll i=0;i<s.length();i++)
{
if(a)
{
if(s[i]!='a')
{
s[i]='a';
}
else
{
s[i]='b';
}
a=false;
}
else
{
if(s[i]!='z')
{
s[i]='z';
}
else
{
s[i]='y';
}
a=true;
}
}
cout<<s<<"\n";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
#define INF 1000000000000000
#define fi first
#define se second
#define all(v) v.begin(),v.end()
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
#define pb push_back
struct edge{int to,cost;};
typedef pair<int,int> P;
int n,m,r,d[111111];
vector<edge>v[111111];
void dijkstra(int x){
fill(d,d+n,INF);
d[x]=0;
priority_queue<P,vector<P>,greater<P>>q;
q.push({x,0});
while(!q.empty()){
P p=q.top();
q.pop();
for(edge e:v[p.first]){
if(d[e.to]>d[p.first]+e.cost){
d[e.to]=d[p.first]+e.cost;
q.push({e.to,d[e.to]});
}
}
}
}
signed main(){
cin>>n>>m>>r;
rep(i,m){
int a,b,c;
cin>>a>>b>>c;
v[a].pb({b,c});
}
dijkstra(r);
rep(i,n){
if(d[i]==INF)puts("INF");
else cout<<d[i]<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, l;
int t[100];
int sol;
int main() {
scanf("%d %d", &n, &l);
for (int i = 0; i < n; ++i) scanf("%d", t + i);
for (int i = l; i <= 100; ++i) {
int sum = 0;
for (int j = 0; j < n; ++j) sum += t[j] / i;
sum *= i;
sol = max(sol, sum);
}
printf("%d\n", sol);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false);
long long int n, k, a, b;
cin >> n >> k >> a >> b;
char s[n];
int i;
for (i = 0; i < n; i++) {
s[i] = '1';
}
if (a < (n / (k + 1)) || b < (n / (k + 1)))
cout << "NO" << endl;
else {
if (a > b) {
int i;
for (i = k; i < n; i += k + 1) {
s[i] = 'B';
b--;
}
for (i = 0; i < n; i++) {
if (i % 2 && a && s[i] == '1') {
s[i] = 'G';
a--;
} else if ((i % 2) == 0 && b && s[i] == '1') {
s[i] = 'B';
b--;
}
}
for (i = 0; i < n; i++) {
if (s[i] == '1' && a) {
s[i] = 'G';
a--;
}
}
for (i = 0; i < n; i++) {
if (s[i] == '1' && b) {
s[i] = 'B';
b--;
}
}
} else {
int i, q = 0;
for (i = k; i < n; i += k + 1) {
s[i] = 'G';
a--;
}
for (i = 0; i < n; i++) {
if ((q % 2) == 0 && a && s[i] == '1') {
s[i] = 'G';
a--;
q++;
} else if ((q % 2) && b && s[i] == '1') {
s[i] = 'B';
b--;
q++;
}
}
for (i = 0; i < n; i++) {
if (s[i] == '1' && b) {
s[i] = 'B';
b--;
}
}
for (i = 0; i < n; i++) {
if (s[i] == '1' && a) {
s[i] = 'G';
a--;
}
}
}
int j = 1;
for (i = 0; i < n; i++) {
cout << s[i];
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int capacity = 0;
int max_capacity = 0;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
capacity -= a;
capacity += b;
if (capacity > max_capacity) max_capacity = capacity;
}
cout << max_capacity << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1e6 + 1, mod = 1e9 + 7;
struct num {
static const int MA = 1e9 + 7, MB = 1e9 + 9;
int a, b;
num() {}
num(int x) : a((x % MA + MA) % MA), b((x % MB + MB) % MB) {}
num(int x, int y) : a((x % MA + MA) % MA), b((y % MB + MB) % MB) {}
num operator+(const num &he) const { return num(a + he.a, b + he.b); }
num operator-(const num &he) const { return num(a - he.a, b - he.b); }
num operator*(const num &he) const {
return num(ll(a) * he.a % MA, ll(b) * he.b % MB);
}
bool operator==(const num &he) const { return a == he.a && b == he.b; }
};
struct segTree {
int n;
vector<int> t;
segTree(int n) : n(n), t(2 * n, -1) {}
void upd(int v, int x) {
t[v += n] = x;
for (v >>= 1; v; v >>= 1) t[v] = max(t[v * 2], t[v * 2 + 1]);
}
int get(int l, int r) {
int ret = -1;
for (l += n, r += n; l <= r; l = (l + 1) >> 1, r = (r - 1) >> 1) {
if (l & 1) ret = max(ret, t[l]);
if (!(r & 1)) ret = max(ret, t[r]);
}
return ret;
}
};
int P = max(239, rand());
num h[maxn], p[maxn];
num ss(int l, int r) { return h[r + 1] - h[l] * p[r - l + 1]; }
int n;
string s;
int d[maxn];
vector<int> out[maxn];
int ans[maxn];
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(time(NULL) + 'L' + 'O' + 'L' + 'L' + 'Y' + 'P' + 'O' + 'P');
cin >> n >> s;
h[0] = 0, p[0] = 1;
for (int i = 0; i < n; i++) {
p[i + 1] = p[i] * P;
h[i + 1] = h[i] * P + s[i];
}
for (int i = 0; i < n / 2; i++) {
int j = n - 1 - i;
if (s[i] != s[j]) {
d[i] = 0;
continue;
}
int lf = 1, rg = i + 1;
while (lf < rg) {
int md = (lf + rg + 1) >> 1;
if (ss(i - md + 1, i + md - 1) == ss(j - md + 1, j + md - 1))
lf = md;
else
rg = md - 1;
}
d[i] = lf;
}
segTree t(n);
for (int i = n / 2 - 1; i >= 0; i--) {
int dd = d[i];
if (dd != 0) {
out[i - dd + 1].push_back(i);
t.upd(i, i);
}
int u = t.get(i, n - 1);
if (u == -1)
ans[i] = -1;
else
ans[i] = 2 * (u - i) + 1;
for (int x : out[i]) t.upd(x, -1);
}
for (int i = 0; i < n / 2; i++) cout << ans[i] << ' ';
if (n & 1) cout << -1;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n;
int res;
pair<int, int> X[501];
int ap[501];
int pos[501];
vector<int> fuck, mooo;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int a, b;
scanf("%d%d", &a, &b);
X[i] = make_pair(-a, i * 2 - 1);
X[i + n] = make_pair(-a - b, i * 2);
}
sort(X + 1, X + 2 * n + 1);
for (int i = 1; i <= 2 * n; i++) pos[X[i].second] = i;
for (int i = 1; i <= 2 * n; i++)
if (X[i].second % 2)
ap[i] = 1;
else
ap[i] = 0;
for (int i = 1; i <= 2 * n; i++)
if (ap[i]) {
int id = X[i].second;
id++;
if (pos[id] > i)
mooo.push_back(i);
else
fuck.push_back(i);
}
if (mooo.size()) {
sort(mooo.begin(), mooo.end());
for (int i = 0; i < mooo.size(); i++) {
int id = mooo[i];
id = X[id].second;
id++;
for (int j = mooo[i] + 1; j <= pos[id]; j++) res += ap[j];
ap[mooo[i]] = 0;
ap[pos[id]] = 1;
}
}
if (fuck.size()) {
sort(fuck.begin(), fuck.end());
for (int i = fuck.size() - 1; i >= 0; i--) {
int id = fuck[i];
id = X[id].second;
id++;
for (int j = pos[id]; j <= fuck[i] - 1; j++) res += ap[j];
ap[fuck[i]] = 0;
ap[pos[id]] = 1;
}
}
cout << res << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &x) {
x = 0;
int c = getchar(), f = 1;
for (; !isdigit(c); c = getchar())
if (c == 45) f = -1;
for (; isdigit(c); c = getchar()) (x *= 10) += f * (c - '0');
}
int n, m;
vector<int> G[3000 + 5];
long long f[3000 + 5][3000 + 5];
long long fac[3000 + 5], inv[3000 + 5];
void init(int n) {
fac[0] = inv[0] = inv[1] = 1;
for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % 1000000007;
for (int i = 2; i <= n; i++)
inv[i] = (1000000007 - 1000000007 / i * inv[1000000007 % i] % 1000000007) %
1000000007;
for (int i = 2; i <= n; i++) inv[i] = inv[i] * inv[i - 1] % 1000000007;
}
void DFS(int u) {
for (int x = 1; x <= n; x++) f[u][x] = 1;
for (int i = 0; i < (int)G[u].size(); i++) {
int v = G[u][i];
DFS(v);
for (int x = 1; x <= n; x++) f[u][x] = f[u][x] * f[v][x] % 1000000007;
}
for (int x = 1; x <= n; x++) f[u][x] = (f[u][x] + f[u][x - 1]) % 1000000007;
}
long long Poly_Inter() {
long long ret = 0;
long long pre[3000 + 5], suc[3000 + 5];
pre[0] = m - n, suc[n] = m;
for (int i = 1; i <= n - 1; i++)
pre[i] = pre[i - 1] * (m - n + i) % 1000000007;
for (int i = n - 1; i >= 1; i--)
suc[i] = suc[i + 1] * (m - n + i) % 1000000007;
for (int i = 0, c = (n & 1) ? -1 : 1; i <= n; i++, c = -c)
ret =
(ret + c * (i < n ? pre[n - i - 1] : 1) * (i > 0 ? suc[n - i + 1] : 1) %
1000000007 * inv[i] % 1000000007 * inv[n - i] % 1000000007 *
f[1][i] % 1000000007) %
1000000007;
return (ret + 1000000007) % 1000000007;
}
int main() {
read(n), read(m), init(n);
for (int i = 2, x; i <= n; i++) read(x), G[x].push_back(i);
DFS(1), printf("%lld\n", Poly_Inter());
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int t, i;
friend bool operator<(const node &a, const node &b) { return a.t < b.t; }
} res[100010];
int total[100010];
int number[100010], x[100010], y[100010], ans[100010], n;
int tt[100010][150];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d %d", &x[i], &y[i]);
for (int i = 1; i <= n; i++) {
if (y[i] == 0)
res[i].t = -1;
else
res[i].t = i - y[i] - 1;
res[i].i = i;
}
sort(res + 1, res + n + 1);
int head = 1;
while (res[head].t <= 0) head++;
for (int i = 1; i <= n; i++) {
int tmp = x[i];
int length = 0, p = (int)sqrt(tmp);
for (int j = 1; j <= p; j++)
if (tmp % j == 0) {
number[++length] = j;
if (j * j != tmp) number[++length] = tmp / j;
}
ans[i] += length;
if (y[i] != 0)
for (int j = 1; j <= length; j++) {
if (total[number[j]]) {
ans[i]--;
if (tt[i][j] < total[number[j]] && tt[i][j] != 0) ans[i]--;
}
total[number[j]]++;
}
else
for (int j = 1; j <= length; j++) total[number[j]]++;
while (res[head].t == i && head <= n) {
tmp = x[res[head].i];
length = 0, p = (int)sqrt(tmp);
for (int j = 1; j <= p; j++)
if (tmp % j == 0) {
number[++length] = j;
if (j * j != tmp) number[++length] = tmp / j;
}
for (int j = 1; j <= length; j++)
if (total[number[j]])
ans[res[head].i]++, tt[res[head].i][j] = total[number[j]];
head++;
}
}
for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n; cin>>n;
int br = 0, cur = 1;
for(int i=1; i<=n; i++)
{
int x; cin>>x;
if(x == cur) cur++;
else br++;
}
if(cur == 1) cout<<"-1\n";
else cout<<br<<"\n";
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, q;
const int MAXN = 100005;
long long s[MAXN], pre[MAXN], gaps[MAXN];
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> s[i];
sort(s, s + n);
for (int i = 1; i < n; i++) gaps[i] = s[i] - s[i - 1];
sort(gaps, gaps + n);
for (int i = 1; i < n; i++) pre[i] = pre[i - 1] + gaps[i];
cin >> q;
for (int i = 0; i < q; i++) {
long long l, r;
cin >> l >> r;
long long g = r - l + 1;
long long p = lower_bound(gaps + 1, gaps + n, g) - gaps;
cout << pre[p - 1] + (n - p + 1) * g << " ";
}
cout << "\n";
return 0;
}
| 4 |
#include <iostream>
using namespace std;
int main(){
while(1){
int n ; cin >> n ; if(!n) break;
int* a=new int[n];
for(int i=0;i<n;i++) cin >> a[i];
int MAX=a[0];
for(int i=0;i<n;i++){
int temp=0;
for(int j=i;j<n;j++){
temp+=a[j];
if(temp>MAX) MAX=temp;
};
};
cout << MAX << '\n' ;
delete[] a;
};
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int INF = 10e9;
int main() {
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<pair<int, int>> g(m);
for (int i = 0; i < m; i++) {
cin >> g[i].first >> g[i].second;
g[i].first--;
g[i].second--;
}
int ans = *max_element(a.begin(), a.end()) - *min_element(a.begin(), a.end());
vector<int> res;
for (int i = 0; i < n; i++) {
vector<int> good;
vector<int> add(n, 0);
for (int j = 0; j < m; j++) {
if (g[j].first > i || g[j].second < i) {
good.push_back(j + 1);
for (int k = g[j].first; k <= g[j].second; k++) {
add[k]--;
}
}
}
int mn = INF;
int mx = -INF;
for (int j = 0; j < n; j++) {
mn = min(mn, a[j] + add[j]);
mx = max(mx, a[j] + add[j]);
}
if (mx - mn > ans) {
ans = mx - mn;
res = good;
}
}
cout << ans << '\n' << res.size() << '\n';
for (int t : res) {
cout << t << ' ';
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct Segment {
int coverTime, coverSize, size;
Segment() {}
Segment(int coverTime, int coverSize, int size)
: coverTime(coverTime), coverSize(coverSize), size(size) {}
Segment operator+(const Segment &rhs) const {
return Segment(0, coverSize + rhs.coverSize, size + rhs.size);
}
};
const int MAXN = 100000;
const int MAXNODE = MAXN << 2;
struct SegmentTree {
int n;
Segment seg[MAXNODE];
static int LEFT(int idx) { return idx << 1; }
static int RIGHT(int idx) { return (idx << 1) | 1; }
void _build(int idx, int lower, int upper) {
seg[idx] = Segment(0, 0, upper - lower + 1);
if (lower == upper) return;
int middle = (lower + upper) >> 1;
_build(LEFT(idx), lower, middle);
_build(RIGHT(idx), middle + 1, upper);
}
void init(int n) {
this->n = n;
_build(1, 0, n - 1);
}
int l, r, v;
void _modify(int idx, int lower, int upper) {
if (l <= lower && upper <= r) {
if (!seg[idx].coverTime) {
seg[idx].coverSize = seg[idx].size;
}
seg[idx].coverTime += v;
if (!seg[idx].coverTime) {
seg[idx] = lower == upper ? Segment(0, 0, 1)
: seg[LEFT(idx)] + seg[RIGHT(idx)];
}
return;
}
int middle = (lower + upper) >> 1;
if (r <= middle) {
_modify(LEFT(idx), lower, middle);
} else if (middle < l) {
_modify(RIGHT(idx), middle + 1, upper);
} else {
_modify(LEFT(idx), lower, middle);
_modify(RIGHT(idx), middle + 1, upper);
}
if (!seg[idx].coverTime) {
seg[idx] = seg[LEFT(idx)] + seg[RIGHT(idx)];
}
}
void modify(int l, int r, int v) {
this->l = l;
this->r = r;
this->v = v;
_modify(1, 0, n - 1);
}
int calc() { return seg[1].coverSize; }
};
int n, m, a[MAXN], b[MAXN], order[MAXN], in[MAXN], out[MAXN], stamp,
answer[MAXN];
vector<int> adj[MAXN], cover[MAXN];
SegmentTree sgt;
void dfsOrder(int u, int parent) {
in[u] = stamp;
order[stamp++] = u;
for (typeof((adj[u]).begin()) it = (adj[u]).begin(); it != (adj[u]).end();
it++) {
int v = *it;
if (parent == v) continue;
dfsOrder(v, u);
}
out[u] = stamp - 1;
}
void dfs(int u, int parent) {
for (typeof((cover[u]).begin()) it = (cover[u]).begin();
it != (cover[u]).end(); it++) {
int x = a[*it], y = b[*it];
sgt.modify(in[x], out[x], 1);
sgt.modify(in[y], out[y], 1);
}
answer[u] = sgt.calc();
for (typeof((adj[u]).begin()) it = (adj[u]).begin(); it != (adj[u]).end();
it++) {
int v = *it;
if (parent == v) continue;
dfs(v, u);
}
for (typeof((cover[u]).begin()) it = (cover[u]).begin();
it != (cover[u]).end(); it++) {
int x = a[*it], y = b[*it];
sgt.modify(in[x], out[x], -1);
sgt.modify(in[y], out[y], -1);
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = (1); i < (n); i++) {
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = (0); i < (m); i++) {
scanf("%d%d", &a[i], &b[i]);
a[i]--;
b[i]--;
cover[a[i]].push_back(i);
cover[b[i]].push_back(i);
}
stamp = 0;
dfsOrder(0, -1);
sgt.init(n);
dfs(0, -1);
for (int i = (0); i < (n); i++) {
printf("%d ", answer[i] ? answer[i] - 1 : 0);
}
putchar('\n');
return 0;
}
| 5 |
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<vector>
#define rep(i,n) for(int i=0;i<int(n);i++)
#define sint(n) scanf("%d",&n);
using namespace std;
int main(){
while(1){
int a,b,c;
sint(a)
sint(b)
sint(c)
if(a==0){break;}
int k;
sint(k)
int buhin[301]={0};
rep(i,301){
buhin[i]=2;
}
int e[1000],r[1000],t[1000],u[1000];
rep(i,k){
scanf("%d%d%d%d",&e[i],&r[i],&t[i],&u[i]);
if(u[i]==1){
buhin[e[i]]=buhin[r[i]]=buhin[t[i]]=1;
}
}
rep(i,k){
if(u[i]==0){
if(buhin[e[i]]==1 && buhin[r[i]]==1){
buhin[t[i]]=0;
}
if(buhin[e[i]]==1 && buhin[t[i]]==1){
buhin[r[i]]=0;
}
if(buhin[t[i]]==1 && buhin[r[i]]==1){
buhin[e[i]]=0;
}
}
}
rep(i,a+b+c){
printf("%d\n",buhin[i+1]);
}
}
}
/*
2 2 2
4
2 4 5 0
2 3 6 0
1 4 5 0
2 3 5 1
*/ | 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T modinv(T a, T n) {
T i = n, v = 0, d = 1;
while (a > 0) {
T t = i / a, x = a;
a = i % x;
i = x;
x = d;
d = v - t * x;
v = x;
}
return (v + n) % n;
}
long long modpow(long long n, long long k, long long mod) {
long long ans = 1;
while (k > 0) {
if (k & 1) ans = (ans * n) % mod;
k >>= 1;
n = (n * n) % mod;
}
return ans % mod;
}
template <class T>
string str(T Number) {
string Result;
ostringstream convert;
convert << Number;
Result = convert.str();
return Result;
}
int StringToNumber(const string &Text) {
istringstream ss(Text);
int result;
return ss >> result ? result : 0;
}
template <class T>
inline vector<pair<T, int> > FACTORISE(T n) {
vector<pair<T, int> > R;
for (T i = 2; n > 1;) {
if (n % i == 0) {
int C = 0;
for (; n % i == 0; C++, n /= i)
;
R.push_back(make_pair(i, C));
}
i++;
if (i > n / i) i = n;
}
if (n > 1) R.push_back(make_pair(n, 1));
return R;
}
template <class T>
inline T TOTIENT(T n) {
vector<pair<T, int> > R = FACTORISE(n);
T r = n;
for (int i = 0; i < R.size(); i++) r = r / R[i].first * (R[i].first - 1);
return r;
}
template <class T>
inline T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
double rnd(float d) { return floor(d + 0.49); }
int main() {
int n, m;
cin >> n >> m;
vector<int> v(n + 1);
int sum = 0;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
v[a] += c;
v[b] -= c;
sum += c;
}
int ans = 0;
for (int i = 0; i <= n; i++) {
ans += max(0, v[i]);
}
ans = min(ans, sum);
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long a[100], x, y;
int q, i, j, k;
int main() {
cin >> q;
while (q--) {
cin >> i >> x;
for (j = 1; j < 61; j++)
if ((1LL << j) > x) break;
if (i == 3) {
for (x = (x + a[j] & ((1LL << j - 1) - 1)) | (1LL << j - 1); x > 1;
x >>= 1, j--)
printf("%I64d ", (x - a[j] & ((1LL << j - 1) - 1)) | (1LL << j - 1));
puts("1");
} else {
scanf("%I64d", &y);
if (i == 1)
k = j;
else
k = 60;
for (; j <= k; j++, y <<= 1) a[j] = a[j] + y & ((1LL << j - 1) - 1);
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 1;
const long long M = 1e9 + 7;
int n, a[N], f[N], cnt[N];
vector<int> vc;
vector<pair<int, int> > fac[N];
bool used[N], vis[N];
void init() {
for (int i = 2; i < N; i++) {
if (vis[i]) continue;
fac[i].push_back(pair<int, int>(i, 1));
for (int j = i + i; j < N; j += i) {
vis[j] = 1;
if (used[j]) {
int c = 0, x = j;
while (x % i == 0) {
c++;
x /= i;
}
fac[j].push_back(pair<int, int>(i, c));
}
}
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
used[a[i] - 1] = 1;
}
init();
sort(a, a + n);
for (int i = n - 1; i >= 0; i--) {
int x = a[i];
if (!f[x])
f[x] = 1, vc.push_back(x);
else {
vc.push_back(x - 1);
for (int j = 0; j < fac[x - 1].size(); j++) {
pair<int, int> p = fac[x - 1][j];
int y = p.first;
f[y] = max(f[y], p.second);
}
}
}
for (int i = 0; i < n; i++) {
int x = vc[i];
for (int j = 0; j < fac[x].size(); j++) {
pair<int, int> p = fac[x][j];
int y = p.first;
if (p.second == f[y]) cnt[y]++;
}
}
bool ok = 0;
for (int i = 0; i < n; i++) {
int x = vc[i];
bool g = 1;
for (int j = 0; j < fac[x].size(); j++) {
pair<int, int> p = fac[x][j];
int y = p.first;
if (p.second == f[y] && cnt[y] == 1) {
g = 0;
break;
}
}
if (g) {
ok = 1;
break;
}
}
long long ans = 1;
for (int i = 2; i < N; i++) {
while (f[i]) {
ans = ans * i % M;
f[i]--;
}
}
if (ok) ans++, ans %= M;
printf("%lld\n", ans);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int dp[2][1009][52][52];
int n, P, L, is1[1009], is2[1009];
int main() {
cin >> n >> P >> L;
int oo = n / L;
if (n % L) oo++;
oo *= 2;
P = min(P, oo);
int aa;
cin >> aa;
while (aa--) {
int x;
cin >> x;
is1[x] = 1;
}
cin >> aa;
while (aa--) {
int x;
cin >> x;
is2[x] = 1;
}
bool cur = 0;
int ans = 0;
for (int pos = n; pos; pos--) {
for (int rem = P; rem >= 0; rem--) {
for (int l1 = 1; l1 <= L; l1++) {
for (int l2 = 1; l2 <= L; l2++) {
dp[cur][rem][l1][l2] =
dp[cur ^ 1][rem][min(L, l1 + 1)][min(L, l2 + 1)];
if ((l1 != L && is1[pos]) || (l2 != L && is2[pos]))
++dp[cur][rem][l1][l2];
int theta = 0;
if (rem) {
if (is1[pos])
theta = max(theta, dp[cur ^ 1][rem - 1][1][min(L, l2 + 1)] + 1);
if (is2[pos])
theta = max(theta, dp[cur ^ 1][rem - 1][min(L, l1 + 1)][1] + 1);
}
dp[cur][rem][l1][l2] = max(dp[cur][rem][l1][l2], theta);
}
}
}
cur ^= 1;
}
cur ^= 1;
for (int j = 0; j <= P; j++) ans = max(ans, dp[cur][j][L][L]);
cout << ans << endl;
}
| 5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.