problem_id
stringlengths
6
6
language
stringclasses
2 values
original_status
stringclasses
3 values
original_src
stringlengths
19
243k
changed_src
stringlengths
19
243k
change
stringclasses
3 values
i1
int64
0
8.44k
i2
int64
0
8.44k
j1
int64
0
8.44k
j2
int64
0
8.44k
error
stringclasses
270 values
stderr
stringlengths
0
226k
p02806
C++
Time Limit Exceeded
#include <iostream> #include <string> #include <vector> using namespace std; int main(void) { int N; cin >> N; vector<pair<string, int>> playlist(N); for (int i = 0; i < N; i++) { cin >> playlist[i].first >> playlist[i].second; } string target; cin >> target; auto i = playlist.begin(); while (i != playlist.end() && (*i).first != target) i++; i++; int ans = 0; while (i != playlist.end()) ans += (*i).second; cout << ans << endl; }
#include <iostream> #include <string> #include <vector> using namespace std; int main(void) { int N; cin >> N; vector<pair<string, int>> playlist(N); for (int i = 0; i < N; i++) { cin >> playlist[i].first >> playlist[i].second; } string target; cin >> target; auto i = playlist.begin(); while (i != playlist.end() && (*i).first != target) i++; i++; int ans = 0; while (i != playlist.end()) ans += (*i++).second; cout << ans << endl; }
replace
20
21
20
21
TLE
p02806
C++
Runtime Error
#include <bits/stdc++.h> //*************** Constraints are always Imaginary,I'm real ************** using namespace std; #define int long long #define MP make_pair #define PB push_back #define PI 3.1415926535897932384626433832795 #define MOD 1000000007 #define fi first #define se second typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<PII> VPII; typedef vector<VI> VVI; typedef map<int, int> MPII; typedef set<int> SETI; typedef multiset<int> MSETI; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; vector<pair<string, int>> pal(n); // unordered_map<string,int> mp; for (int i = 0; i < n; i++) { string str; int a; cin >> str >> a; pal[i] = {str, a}; // mp[str]=a; } string f; cin >> f; int index; for (int i = 0; i < n; i++) { if (pal[i].first == f) { index = i + 1; break; } } int sum = 0; while (index < n) { sum += pal[index].second; index++; } cout << sum; return 0; }
#include <bits/stdc++.h> //*************** Constraints are always Imaginary,I'm real ************** using namespace std; #define int long long #define MP make_pair #define PB push_back #define PI 3.1415926535897932384626433832795 #define MOD 1000000007 #define fi first #define se second typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<PII> VPII; typedef vector<VI> VVI; typedef map<int, int> MPII; typedef set<int> SETI; typedef multiset<int> MSETI; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // #endif int n; cin >> n; vector<pair<string, int>> pal(n); // unordered_map<string,int> mp; for (int i = 0; i < n; i++) { string str; int a; cin >> str >> a; pal[i] = {str, a}; // mp[str]=a; } string f; cin >> f; int index; for (int i = 0; i < n; i++) { if (pal[i].first == f) { index = i + 1; break; } } int sum = 0; while (index < n) { sum += pal[index].second; index++; } cout << sum; return 0; }
replace
20
24
20
24
0
p02806
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define mem(a, b) memset(a, b, sizeof(a)) #define cin(a) scanf("%d", &a) #define pii pair<int, int> #define ll long long #define gcd __gcd const int inf = 0x3f3f3f3f; const int maxn = 201110; const int M = 1e9 + 7; map<string, int> mp; string a[100]; int t[100]; int main() { #ifdef ONLINE_JUDGE #else freopen("data.in", "r", stdin); #endif int n; cin(n); for (int i = 1; i <= n; i++) { cin >> a[i] >> t[i]; mp[a[i]] = i; } string str; cin >> str; int i = mp[str] + 1; int ans = 0; for (; i <= n; i++) { ans += t[i]; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define mem(a, b) memset(a, b, sizeof(a)) #define cin(a) scanf("%d", &a) #define pii pair<int, int> #define ll long long #define gcd __gcd const int inf = 0x3f3f3f3f; const int maxn = 201110; const int M = 1e9 + 7; map<string, int> mp; string a[100]; int t[100]; int main() { int n; cin(n); for (int i = 1; i <= n; i++) { cin >> a[i] >> t[i]; mp[a[i]] = i; } string str; cin >> str; int i = mp[str] + 1; int ans = 0; for (; i <= n; i++) { ans += t[i]; } cout << ans << endl; return 0; }
delete
16
20
16
16
-11
p02806
C++
Runtime Error
#include <bits/stdc++.h> const int N = 50; std::string names[N]; int len[N], n, ans = 0; std::string music; int main() { std::cin >> n; for (int i = 1; i <= n; i++) std::cin >> names[i] >> len[i]; std::cin >> music; bool flag{0}; for (int i = 1; i <= n; i++) { if (flag) ans += len[i]; if (names[i] == music) flag = 1; } std::cout << ans; return 0; }
#include <bits/stdc++.h> const int N = 5000; std::string names[N]; int len[N], n, ans = 0; std::string music; int main() { std::cin >> n; for (int i = 1; i <= n; i++) std::cin >> names[i] >> len[i]; std::cin >> music; bool flag{0}; for (int i = 1; i <= n; i++) { if (flag) ans += len[i]; if (names[i] == music) flag = 1; } std::cout << ans; return 0; }
replace
2
3
2
3
0
p02806
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; for (int i = 0; i < (int)v.size(); i++) { if (i) os << ", "; os << v[i]; } os << "}"; return os; } #ifdef LOCAL #define cerr cout #else #endif #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifndef ONLINE_JUDGE #define debug(x...) \ cerr << "[" << #x << "] = ["; \ _print(x) #else #define debug(x...) #endif #define rep(i, n) for (int i = 0; i < (n); ++i) #define repA(i, a, n) for (int i = a; i <= (n); ++i) #define repD(i, a, n) for (int i = a; i >= (n); --i) #define IOS \ ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0), cin.exceptions(cin.failbit) #define trav(a, x) for (auto &a : x) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) (int)(x).size() #define fill(a, b) memset(a, b, sizeof(a)) #define fi first #define se second #define lb lower_bound #define ub upper_bound #define mp make_pair #define pb push_back #define has_bit(bit_mask, x) ((bit_mask) & (1Uint << (x))) #define turn_on_bit(bit_mask, x) (bit_mask |= (1Uint << (x))) #define turn_off_bit(bit_mask, x) (bit_mask &= (~(1Uint << (x)))) #define smallest_on_bit(bit_mask) (__builtin_ctzint((bit_mask) & (-(bit_mask)))) #define int long long typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; const long long modb = 1000000007; const long long inf = 0x3f3f3f3f3f3f3f3fLL; // Large int const double oo = 1e15; // Large double const double eps = 1e-8; // Small double, used for computational geometry const double pi = acos(-1.0); inline int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } inline int lcm(int a, int b) { return (a * b) / gcd(a, b); } inline bool isPerfectSquare(int x) { int s = sqrt(x); return (s * s == x); } inline bool isFibonacci(int n) { return isPerfectSquare(5 * n * n + 4) || isPerfectSquare(5 * n * n - 4); } inline int add(int a, int b) { a += b; if (a >= modb) return a - modb; return a; } inline int sub(int a, int b) { a -= b; if (a < 0) return a + modb; return a; } inline int mul(int a, int b) { return ((((a % modb) * (b % modb)) % modb) + modb) % modb; } inline int numofdig(int i) { return i > 0 ? (int)log10l((ld)i) + 1 : 1; } int pwr(int a, int b) { a %= modb; int ans = 1; while (b) { if (b & 1) ans = (ans * a) % modb; a = (a * a) % modb; b >>= 1; } return ans; } int32_t main() { IOS; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // ONLINE_JUDGE int t = 1; rep(tc, t) { // cout<<"Case #"<<tc+1<<": "; int n; cin >> n; map<string, int> pref; int a; string prev; rep(i, n) { string s; cin >> s; int tm; cin >> tm; if (i == 0) pref[s] = tm; else pref[s] = pref[prev] + tm; prev = s; if (i == n - 1) a = pref[s]; } string x; cin >> x; cout << a - pref[x] << "\n"; } cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; }
#include "bits/stdc++.h" using namespace std; template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; for (int i = 0; i < (int)v.size(); i++) { if (i) os << ", "; os << v[i]; } os << "}"; return os; } #ifdef LOCAL #define cerr cout #else #endif #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifndef ONLINE_JUDGE #define debug(x...) \ cerr << "[" << #x << "] = ["; \ _print(x) #else #define debug(x...) #endif #define rep(i, n) for (int i = 0; i < (n); ++i) #define repA(i, a, n) for (int i = a; i <= (n); ++i) #define repD(i, a, n) for (int i = a; i >= (n); --i) #define IOS \ ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0), cin.exceptions(cin.failbit) #define trav(a, x) for (auto &a : x) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) (int)(x).size() #define fill(a, b) memset(a, b, sizeof(a)) #define fi first #define se second #define lb lower_bound #define ub upper_bound #define mp make_pair #define pb push_back #define has_bit(bit_mask, x) ((bit_mask) & (1Uint << (x))) #define turn_on_bit(bit_mask, x) (bit_mask |= (1Uint << (x))) #define turn_off_bit(bit_mask, x) (bit_mask &= (~(1Uint << (x)))) #define smallest_on_bit(bit_mask) (__builtin_ctzint((bit_mask) & (-(bit_mask)))) #define int long long typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; const long long modb = 1000000007; const long long inf = 0x3f3f3f3f3f3f3f3fLL; // Large int const double oo = 1e15; // Large double const double eps = 1e-8; // Small double, used for computational geometry const double pi = acos(-1.0); inline int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } inline int lcm(int a, int b) { return (a * b) / gcd(a, b); } inline bool isPerfectSquare(int x) { int s = sqrt(x); return (s * s == x); } inline bool isFibonacci(int n) { return isPerfectSquare(5 * n * n + 4) || isPerfectSquare(5 * n * n - 4); } inline int add(int a, int b) { a += b; if (a >= modb) return a - modb; return a; } inline int sub(int a, int b) { a -= b; if (a < 0) return a + modb; return a; } inline int mul(int a, int b) { return ((((a % modb) * (b % modb)) % modb) + modb) % modb; } inline int numofdig(int i) { return i > 0 ? (int)log10l((ld)i) + 1 : 1; } int pwr(int a, int b) { a %= modb; int ans = 1; while (b) { if (b & 1) ans = (ans * a) % modb; a = (a * a) % modb; b >>= 1; } return ans; } int32_t main() { IOS; int t = 1; rep(tc, t) { // cout<<"Case #"<<tc+1<<": "; int n; cin >> n; map<string, int> pref; int a; string prev; rep(i, n) { string s; cin >> s; int tm; cin >> tm; if (i == 0) pref[s] = tm; else pref[s] = pref[prev] + tm; prev = s; if (i == n - 1) a = pref[s]; } string x; cin >> x; cout << a - pref[x] << "\n"; } cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; }
delete
160
164
160
160
-6
terminate called after throwing an instance of 'std::__ios_failure' what(): basic_ios::clear: iostream error
p02806
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<pair<string, int>> v; long long ans = 0; string x; bool f = 0; for (int i = 0; i < n; i++) { cin >> v[i].first >> v[i].second; } cin >> x; for (int i = 0; i < n; i++) { if (f) { ans += v[i].second; } if (v[i].first == x) f = 1; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<pair<string, int>> v(n); long long ans = 0; string x; bool f = 0; for (int i = 0; i < n; i++) { cin >> v[i].first >> v[i].second; } cin >> x; for (int i = 0; i < n; i++) { if (f) { ans += v[i].second; } if (v[i].first == x) f = 1; } cout << ans << endl; }
replace
5
6
5
6
-11
p02806
C++
Runtime Error
#include <iostream> using namespace std; int main() { int n; string s[n]; int t[n]; string x; cin >> n; for (int i = 0; i < n; i++) cin >> s[i] >> t[i]; cin >> x; int tsum = 0; for (int i = 0; i < n; i++) { if (s[i] == x) { for (int j = i + 1; j < n; j++) { tsum += t[j]; } break; } } cout << tsum << endl; return 0; }
#include <iostream> using namespace std; int main() { int n; string s[55]; int t[55]; string x; cin >> n; for (int i = 0; i < n; i++) cin >> s[i] >> t[i]; cin >> x; int tsum = 0; for (int i = 0; i < n; i++) { if (s[i] == x) { for (int j = i + 1; j < n; j++) { tsum += t[j]; } break; } } cout << tsum << endl; return 0; }
replace
5
7
5
7
0
p02806
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; using ll = long long; using Graph = vector<vector<int>>; using P = pair<int, int>; int main() { int n; cin >> n; vector<string> s; vector<int> t; rep(i, n) cin >> s[i] >> t[i]; string x; cin >> x; int ans = 0; int num = 0; rep(i, n) { if (s[i] == x) num = i; } for (int i = num + 1; i < n; i++) { ans += t[i]; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; using ll = long long; using Graph = vector<vector<int>>; using P = pair<int, int>; int main() { int n; cin >> n; vector<string> s(n); vector<int> t(n); rep(i, n) cin >> s[i] >> t[i]; string x; cin >> x; int ans = 0; int num = 0; rep(i, n) { if (s[i] == x) num = i; } for (int i = num + 1; i < n; i++) { ans += t[i]; } cout << ans << endl; return 0; }
replace
10
12
10
12
-11
p02806
C++
Time Limit Exceeded
#include <algorithm> #include <cstdlib> #include <functional> #include <iostream> #include <list> #include <numeric> #include <stdio.h> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define all(x) (x).begin(), (x).end() using namespace std; int a, b, c; string s, t; int main() { a = 0; cin >> a; vector<string> u(a); vector<int> x(a); rep(i, a) { cin >> u.at(i) >> x.at(i); } cin >> t; int i = 0, sum = 0; while (i < a) { if (u.at(i) == t) { for (int j = i + 1; j < a; j++) sum += x.at(j); break; } } cout << sum << endl; }
#include <algorithm> #include <cstdlib> #include <functional> #include <iostream> #include <list> #include <numeric> #include <stdio.h> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define all(x) (x).begin(), (x).end() using namespace std; int a, b, c; string s, t; int main() { a = 0; cin >> a; vector<string> u(a); vector<int> x(a); rep(i, a) { cin >> u.at(i) >> x.at(i); } cin >> t; int i = 0, sum = 0; while (i < a) { if (u.at(i) == t) { for (int j = i + 1; j < a; j++) sum += x.at(j); break; } i++; } cout << sum << endl; }
insert
30
30
30
31
TLE
p02807
C++
Time Limit Exceeded
/* Author : N_o_o_B Created : January 14 2020 18:43:54 */ #include <bits/stdc++.h> using namespace std; /* #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; */ #define TRACE #ifdef TRACE #define trace(...) \ { \ cerr << "[ "; \ __trace__(#__VA_ARGS__, __VA_ARGS__); \ } #undef endl template <typename Arg1, typename Arg2> ostream &operator<<(ostream &out, const pair<Arg1, Arg2> &x) { return out << "(" << x.first << "," << x.second << ")"; } template <typename Arg1> ostream &operator<<(ostream &out, const vector<Arg1> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1> ostream &operator<<(ostream &out, const set<Arg1> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1, typename Arg2> ostream &operator<<(ostream &out, const map<Arg1, Arg2> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1> void __trace__(const string name, Arg1 &&arg1) { cerr << name << " : " << arg1 << " ] " << endl; } template <typename Arg1, typename... Args> void __trace__(const string names, Arg1 &&arg1, Args &&...args) { const string name = names.substr(0, names.find(',')); cerr << name << " : " << arg1 << " | "; __trace__(names.substr(1 + (int)name.size()), args...); } #else #define trace(args...) #endif typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pair<int, int>> vii; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<pair<ll, ll>> vll; typedef vector<vl> vvl; // typedef tree<pii, null_type, less<pii>, rb_tree_tag, // tree_order_statistics_node_update> oset; #define fori(i, n) for (int i = 0; i < n; i++) #define rep(i, a, b) for (int i = a; i <= b; i++) #define repd(i, a, b) for (int i = a; i >= b; i--) #define ford(i, n) for (int i = n - 1; i >= 0; i--) #define trav(x, a) for (auto &x : a) #define all(x) x.begin(), x.end() #define pb push_back #define endl '\n' #define sz(a) (int)a.size() #define fi first #define se second clock_t time_p = clock(); void time_taken() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } // const ll mod=998244353; const ll mod = 1e9 + 7; const ll INF = 1e18; // credits ---> ksun48 ll modinv(ll a, ll m) { assert(m > 0); if (m == 1) return 0; a %= m; if (a < 0) a += m; assert(a != 0); if (a == 1) return 1; return m - modinv(m, a) * m / a; } template <int MOD_> struct modnum { private: int v; public: static const int MOD = MOD_; modnum() : v(0) {} modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend bool operator==(const modnum &a, const modnum &b) { return a.v == b.v; } friend bool operator!=(const modnum &a, const modnum &b) { return a.v != b.v; } modnum operator~() const { modnum res; res.v = modinv(v, MOD); return res; } modnum &operator+=(const modnum &o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum &operator-=(const modnum &o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum &operator*=(const modnum &o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum &operator/=(const modnum &o) { return *this *= (~o); } friend modnum operator+(const modnum &a, const modnum &b) { return modnum(a) += b; } friend modnum operator-(const modnum &a, const modnum &b) { return modnum(a) -= b; } friend modnum operator*(const modnum &a, const modnum &b) { return modnum(a) *= b; } friend modnum operator/(const modnum &a, const modnum &b) { return modnum(a) /= b; } }; using num = modnum<int(1e9) + 7>; vector<num> fact; vector<num> ifact; void init() { fact = {1}; for (int i = 1; i < 100000; i++) fact.push_back(i * fact[i - 1]); for (num x : fact) ifact.push_back(1 / x); } num ncr(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } num powmod(num x, int a) { if (a == 0) return 1; if (a & 1) return x * powmod(x, a - 1); return powmod(x * x, a / 2); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(12); cout << fixed; init(); int n; cin >> n; vi x(n); fori(i, n) cin >> x[i]; num ans = x[n - 1] - x[n - 2]; ford(i, n - 2) { rep(j, i + 1, n - 2) { ans += num(1) / (j - i + 1) * (x[j] - x[i]) / (j - i); } ans += num(1) / (n - 1 - i) * (x[n - 1] - x[i]); } ans *= fact[n - 1]; cout << int(ans) << endl; time_taken(); return 0; }
/* Author : N_o_o_B Created : January 14 2020 18:43:54 */ #include <bits/stdc++.h> using namespace std; /* #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; */ #define TRACE #ifdef TRACE #define trace(...) \ { \ cerr << "[ "; \ __trace__(#__VA_ARGS__, __VA_ARGS__); \ } #undef endl template <typename Arg1, typename Arg2> ostream &operator<<(ostream &out, const pair<Arg1, Arg2> &x) { return out << "(" << x.first << "," << x.second << ")"; } template <typename Arg1> ostream &operator<<(ostream &out, const vector<Arg1> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1> ostream &operator<<(ostream &out, const set<Arg1> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1, typename Arg2> ostream &operator<<(ostream &out, const map<Arg1, Arg2> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1> void __trace__(const string name, Arg1 &&arg1) { cerr << name << " : " << arg1 << " ] " << endl; } template <typename Arg1, typename... Args> void __trace__(const string names, Arg1 &&arg1, Args &&...args) { const string name = names.substr(0, names.find(',')); cerr << name << " : " << arg1 << " | "; __trace__(names.substr(1 + (int)name.size()), args...); } #else #define trace(args...) #endif typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pair<int, int>> vii; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<pair<ll, ll>> vll; typedef vector<vl> vvl; // typedef tree<pii, null_type, less<pii>, rb_tree_tag, // tree_order_statistics_node_update> oset; #define fori(i, n) for (int i = 0; i < n; i++) #define rep(i, a, b) for (int i = a; i <= b; i++) #define repd(i, a, b) for (int i = a; i >= b; i--) #define ford(i, n) for (int i = n - 1; i >= 0; i--) #define trav(x, a) for (auto &x : a) #define all(x) x.begin(), x.end() #define pb push_back #define endl '\n' #define sz(a) (int)a.size() #define fi first #define se second clock_t time_p = clock(); void time_taken() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } // const ll mod=998244353; const ll mod = 1e9 + 7; const ll INF = 1e18; // credits ---> ksun48 ll modinv(ll a, ll m) { assert(m > 0); if (m == 1) return 0; a %= m; if (a < 0) a += m; assert(a != 0); if (a == 1) return 1; return m - modinv(m, a) * m / a; } template <int MOD_> struct modnum { private: int v; public: static const int MOD = MOD_; modnum() : v(0) {} modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend bool operator==(const modnum &a, const modnum &b) { return a.v == b.v; } friend bool operator!=(const modnum &a, const modnum &b) { return a.v != b.v; } modnum operator~() const { modnum res; res.v = modinv(v, MOD); return res; } modnum &operator+=(const modnum &o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum &operator-=(const modnum &o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum &operator*=(const modnum &o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum &operator/=(const modnum &o) { return *this *= (~o); } friend modnum operator+(const modnum &a, const modnum &b) { return modnum(a) += b; } friend modnum operator-(const modnum &a, const modnum &b) { return modnum(a) -= b; } friend modnum operator*(const modnum &a, const modnum &b) { return modnum(a) *= b; } friend modnum operator/(const modnum &a, const modnum &b) { return modnum(a) /= b; } }; using num = modnum<int(1e9) + 7>; vector<num> fact; vector<num> ifact; void init() { fact = {1}; for (int i = 1; i < 100000; i++) fact.push_back(i * fact[i - 1]); for (num x : fact) ifact.push_back(1 / x); } num ncr(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } num powmod(num x, int a) { if (a == 0) return 1; if (a & 1) return x * powmod(x, a - 1); return powmod(x * x, a / 2); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(12); cout << fixed; init(); int n; cin >> n; vi x(n); fori(i, n) cin >> x[i]; num ans = 0; vector<num> foo(n); foo[0] = 0; rep(i, 1, n - 1) { foo[i] = foo[i - 1] + (num(1) / i) / (i + 1); } ford(i, n - 1) { ans += (foo[i] - foo[n - 2 - i]) * x[i]; ans += num(1) / (n - 1 - i) * (x[n - 1] - x[i]); } ans *= fact[n - 1]; cout << int(ans) << endl; time_taken(); return 0; }
replace
203
208
203
209
TLE
p02807
Python
Time Limit Exceeded
import math n = int(input()) x = list(map(int, input().split())) mod = 10**9 + 7 fac = math.factorial(n - 1) def pow(n, p): res = 1 while p > 0: if p % 2 == 0: n = n**2 % mod p //= 2 else: res = res * n % mod p -= 1 return res % mod res = 0 for i in range(n - 1): d = x[n - 1] - x[i] res += (d * fac * pow(i + 1, mod - 2)) % mod print(res % mod)
import math n = int(input()) x = list(map(int, input().split())) mod = 10**9 + 7 fac = math.factorial(n - 1) % mod def pow(n, p): res = 1 while p > 0: if p % 2 == 0: n = n**2 % mod p //= 2 else: res = res * n % mod p -= 1 return res % mod res = 0 for i in range(n - 1): d = x[n - 1] - x[i] res += (d * fac * pow(i + 1, mod - 2)) % mod print(res % mod)
replace
6
7
6
7
TLE
p02807
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define WHOLE(v) (v).begin(), (v).end() #define REV_WHOLE(v) (v).rbegin(), (v).rend() using i64 = int64_t; using namespace std; template <size_t I, class H, class... T> struct TupleReaderWriter { static tuple<H, T...> r(istream &i) { H v; i >> v; return tuple_cat(tuple<H>(v), TupleReaderWriter<sizeof...(T) - 1, T...>::r(i)); } static void w(ostream &o, tuple<H, T...> &t, string d) { TupleReaderWriter<I - 1, H, T...>::w(o, t, d); o << d << get<I>(t); } }; template <class H, class... T> struct TupleReaderWriter<0, H, T...> { static tuple<H, T...> r(istream &i) { H v; i >> v; return tuple<H>(v); } static void w(ostream &o, tuple<H, T...> &t, string d) { o << get<0>(t); } }; template <class... T> istream &operator>>(istream &i, tuple<T...> &t) { t = TupleReaderWriter<sizeof...(T), T...>::r(i); return i; } template <class... T> ostream &operator<<(ostream &o, tuple<T...> &t) { string delim = " "; TupleReaderWriter<sizeof...(T) - 1, T...>::w(o, t, delim); return o; } template <class T> istream &operator>>(istream &i, vector<T> &v) { for (auto &x : v) i >> x; return i; } template <class T> ostream &operator<<(ostream &o, vector<T> &v) { string delim = ""; for (auto &x : v) o << delim << x, delim = " "; return o; } template <class T> using rev_priority_queue = priority_queue<T, vector<T>, greater<T>>; template <class T> using vector2d = vector<vector<T>>; struct fixprec { int x; fixprec(int d) : x(d) {} }; ostream &operator<<(ostream &o, fixprec f) { return o << fixed << setprecision(f.x); } void R_YESNO(bool p) { cout << (p ? "YES" : "NO") << endl; } void R_YesNo(bool p) { cout << (p ? "Yes" : "No") << endl; } const i64 MOD = 1e9 + 7; int64_t powmod(int64_t a, int64_t p) { int64_t v = 1; for (int64_t b = a; p > 0; p >>= 1, b = (b * b) % MOD) { if (p & 1) v = (v * b) % MOD; } return v; } int64_t invmod(int64_t x) { return powmod(x, MOD - 2); } int main() { int N; cin >> N; vector<i64> x(N), d(N); for (i64 &a : x) cin >> x; for (int i = 0; i < N - 1; i++) { d[i] = (x[i + 1] - x[i]) % MOD; } i64 ans = 0, s = 0; for (int k = 0; k < N - 1; k++) { s += invmod(k + 1); ans = (ans + (s * d[k]) % MOD) % MOD; } for (int i = 1; i < N; i++) { ans = (ans * i) % MOD; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define WHOLE(v) (v).begin(), (v).end() #define REV_WHOLE(v) (v).rbegin(), (v).rend() using i64 = int64_t; using namespace std; template <size_t I, class H, class... T> struct TupleReaderWriter { static tuple<H, T...> r(istream &i) { H v; i >> v; return tuple_cat(tuple<H>(v), TupleReaderWriter<sizeof...(T) - 1, T...>::r(i)); } static void w(ostream &o, tuple<H, T...> &t, string d) { TupleReaderWriter<I - 1, H, T...>::w(o, t, d); o << d << get<I>(t); } }; template <class H, class... T> struct TupleReaderWriter<0, H, T...> { static tuple<H, T...> r(istream &i) { H v; i >> v; return tuple<H>(v); } static void w(ostream &o, tuple<H, T...> &t, string d) { o << get<0>(t); } }; template <class... T> istream &operator>>(istream &i, tuple<T...> &t) { t = TupleReaderWriter<sizeof...(T), T...>::r(i); return i; } template <class... T> ostream &operator<<(ostream &o, tuple<T...> &t) { string delim = " "; TupleReaderWriter<sizeof...(T) - 1, T...>::w(o, t, delim); return o; } template <class T> istream &operator>>(istream &i, vector<T> &v) { for (auto &x : v) i >> x; return i; } template <class T> ostream &operator<<(ostream &o, vector<T> &v) { string delim = ""; for (auto &x : v) o << delim << x, delim = " "; return o; } template <class T> using rev_priority_queue = priority_queue<T, vector<T>, greater<T>>; template <class T> using vector2d = vector<vector<T>>; struct fixprec { int x; fixprec(int d) : x(d) {} }; ostream &operator<<(ostream &o, fixprec f) { return o << fixed << setprecision(f.x); } void R_YESNO(bool p) { cout << (p ? "YES" : "NO") << endl; } void R_YesNo(bool p) { cout << (p ? "Yes" : "No") << endl; } const i64 MOD = 1e9 + 7; int64_t powmod(int64_t a, int64_t p) { int64_t v = 1; for (int64_t b = a; p > 0; p >>= 1, b = (b * b) % MOD) { if (p & 1) v = (v * b) % MOD; } return v; } int64_t invmod(int64_t x) { return powmod(x, MOD - 2); } int main() { int N; cin >> N; vector<i64> x(N), d(N); cin >> x; for (int i = 0; i < N - 1; i++) { d[i] = (x[i + 1] - x[i]) % MOD; } i64 ans = 0, s = 0; for (int k = 0; k < N - 1; k++) { s += invmod(k + 1); ans = (ans + (s * d[k]) % MOD) % MOD; } for (int i = 1; i < N; i++) { ans = (ans * i) % MOD; } cout << ans << endl; return 0; }
replace
75
77
75
76
TLE
p02807
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int max_n = 2005; const int mod = 1e9 + 7; inline int Add(int x, int y) { return (x += y) >= mod ? x - mod : x; } inline int Sub(int x, int y) { return (x -= y) >= 0 ? x : x + mod; } inline int Mul(int x, int y) { return 1ll * x * y % mod; } int n, x[max_n]; int inv[max_n]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", x + i); inv[1] = 1; for (int i = 2; i <= n; i++) inv[i] = Mul(inv[mod % i], mod - mod / i); for (int i = 1; i <= n; i++) inv[i] = Add(inv[i - 1], inv[i]); int ans = 0; for (int i = 1; i < n; i++) { int tmp = Sub(x[i + 1], x[i]); tmp = Mul(tmp, inv[i]); ans = Add(ans, tmp); } for (int i = 1; i < n; i++) ans = Mul(ans, i); printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int max_n = 100005; const int mod = 1e9 + 7; inline int Add(int x, int y) { return (x += y) >= mod ? x - mod : x; } inline int Sub(int x, int y) { return (x -= y) >= 0 ? x : x + mod; } inline int Mul(int x, int y) { return 1ll * x * y % mod; } int n, x[max_n]; int inv[max_n]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", x + i); inv[1] = 1; for (int i = 2; i <= n; i++) inv[i] = Mul(inv[mod % i], mod - mod / i); for (int i = 1; i <= n; i++) inv[i] = Add(inv[i - 1], inv[i]); int ans = 0; for (int i = 1; i < n; i++) { int tmp = Sub(x[i + 1], x[i]); tmp = Mul(tmp, inv[i]); ans = Add(ans, tmp); } for (int i = 1; i < n; i++) ans = Mul(ans, i); printf("%d\n", ans); return 0; }
replace
3
4
3
4
0
p02807
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define mp make_pair #define pb push_back #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define debug(x) cerr << #x << " " << x << '\n' using namespace std; using ll = long long; using pii = pair<int, int>; using pli = pair<ll, int>; const int INF = 0x3f3f3f3f, N = 1e5 + 5; const ll LINF = 1e18 + 5; constexpr int mod = 1e9 + 7, two = (mod + 1) / 2; int n, x[N], d[N]; ll sum[N]; ll fac[N], inv[N]; ll powmod(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans = ans * a % mod; a = a * a % mod; b >>= 1; } return ans; } void init() { fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod; inv[n] = powmod(fac[n], mod - 2); for (int i = n - 1; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % mod; } ll comp(ll a, ll b) { if (a < b || b < 0) return 0; return fac[a] * inv[b] % mod * inv[a - b] % mod; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> x[i]; for (int i = 1; i < n; i++) d[i] = x[i + 1] - x[i]; for (int i = 1; i < n; i++) sum[i] = (sum[i - 1] + d[i]) % mod; init(); ll res = 0; for (int i = 1; i < n - 1; i++) { for (int j = 1; j + i < n; j++) { ll cur = d[j + i] * comp(n - 1, i + 1) % mod * fac[n - i - 2] % mod * fac[i] % mod; // debug(cur); res = (res + cur) % mod; } } res = (res + fac[n - 1] * sum[n - 1] % mod) % mod; res = (res + mod) % mod; cout << res; return 0; }
#include <bits/stdc++.h> #define mp make_pair #define pb push_back #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define debug(x) cerr << #x << " " << x << '\n' using namespace std; using ll = long long; using pii = pair<int, int>; using pli = pair<ll, int>; const int INF = 0x3f3f3f3f, N = 1e5 + 5; const ll LINF = 1e18 + 5; constexpr int mod = 1e9 + 7, two = (mod + 1) / 2; int n, x[N], d[N]; ll sum[N]; ll fac[N], inv[N]; ll powmod(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans = ans * a % mod; a = a * a % mod; b >>= 1; } return ans; } void init() { fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod; inv[n] = powmod(fac[n], mod - 2); for (int i = n - 1; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % mod; } ll comp(ll a, ll b) { if (a < b || b < 0) return 0; return fac[a] * inv[b] % mod * inv[a - b] % mod; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> x[i]; for (int i = 1; i < n; i++) d[i] = x[i + 1] - x[i]; for (int i = 1; i < n; i++) sum[i] = (sum[i - 1] + d[i]) % mod; init(); ll res = 0; for (int i = 1; i < n - 1; i++) { // for(int j=1; j+i<n; j++) // { // ll cur = d[j+i]*comp(n-1, // i+1)%mod*fac[n-i-2]%mod*fac[i]%mod; // //debug(cur); // res = (res+cur)%mod; // } ll cur = comp(n - 1, i + 1) % mod * fac[n - i - 2] % mod * fac[i] % mod; cur = cur * (sum[n - 1] - sum[i]) % mod; res = (res + cur) % mod; } res = (res + fac[n - 1] * sum[n - 1] % mod) % mod; res = (res + mod) % mod; cout << res; return 0; }
replace
52
58
52
62
TLE
p02807
C++
Time Limit Exceeded
#include <bits/stdc++.h> //////////// // ModInt // //////////// // 四則演算の最も左に存在する値がModIntでなければキャストでバグる // 例えばx = mint * 1000;やx = ModInt(1000) * mint;はいいがx = 1000 * // mint;は駄目。 template <int64_t mod_ = 1'000'000'007> class ModInt { private: int64_t integer_; public: constexpr ModInt(const int64_t initial_number = 0) : integer_(initial_number) {} // 四則演算 constexpr ModInt operator+(const ModInt &operand) const { ModInt ret{this->integer_ + operand.integer_}; if (ret.integer_ >= mod_) ret.integer_ -= mod_; return ret; } constexpr ModInt operator-(const ModInt &operand) const { ModInt ret{this->integer_ - operand.integer_}; if (ret.integer_ < 0) ret.integer_ += mod_; return ret; } constexpr ModInt operator*(const ModInt &operand) const { return {this->integer_ * operand.integer_ % mod_}; } constexpr ModInt operator/(const ModInt &operand) const { return *this * (operand ^ (mod_ - 2)); } // 単項演算子 constexpr ModInt &operator++() { if (integer_ + 1 == mod_) integer_ = 0; else integer_++; return *this; } constexpr ModInt &operator--() { if (integer_ == 0) integer_ = mod_ - 1; else integer_--; return *this; } constexpr ModInt operator+() { return *this; } constexpr ModInt operator-() { if (integer_ == 0) return ModInt(0ll); else return ModInt(mod_ - integer_); } // 累乗 constexpr ModInt operator^(const int64_t operand) const { ModInt ret{1}, pow_ope{this->integer_}; for (int64_t pow{operand}; pow > 0; pow >>= 1) { if (pow & 1) ret *= pow_ope; pow_ope *= pow_ope; } return ret; } // 代入 constexpr ModInt &operator=(const ModInt &operand) { this->integer_ = operand.integer_; return *this; } constexpr ModInt &operator+=(const ModInt &operand) { *this = *this + operand; return *this; } constexpr ModInt &operator-=(const ModInt &operand) { *this = *this - operand; return *this; } constexpr ModInt &operator*=(const ModInt &operand) { *this = *this * operand; return *this; } constexpr ModInt &operator/=(const ModInt &operand) { *this = *this / operand; return *this; } // その他 constexpr operator int64_t() { return integer_; } constexpr ModInt getOne() const { return ModInt(1ll); } constexpr ModInt getZero() const { return ModInt(0ll); } static constexpr int64_t mod{mod_}; }; using Mint = ModInt<>; //////////////// // 組み合わせ // /////////////// template <typename T = Mint, int max_ = 200'000> class Combination { public: std::array<Mint, max_ + 1> inv, fact, finv; constexpr Combination() { inv[0] = inv[1] = fact[0] = fact[1] = finv[0] = finv[1] = 1; for (int num{2}; num <= max_; num++) { inv[num] = (T::mod - (int64_t)inv[T::mod % num] * (T::mod / num) % T::mod) % T::mod; fact[num] = num * fact[num - 1] % T::mod; finv[num] = inv[num] * finv[num - 1] % T::mod; } } constexpr Mint getCombi(const int n, const int r) const { if (r < 0 || n < 0 || n - r < 0) return 0; return fact[n] * finv[r] * finv[n - r]; } constexpr Mint getPerm(const int n, const int r) const { if (r < 0 || n < 0 || n - r < 0) return 0; return fact[n] * finv[n - r]; } }; Combination<> combi; int main() { int N; scanf("%d", &N); std::vector<int64_t> x(N); for (auto &e : x) scanf("%lld", &e); std::vector<int64_t> diff(N - 1); for (int i{}; i < N - 1; i++) diff[i] = x[i + 1] - x[i]; Mint ans{}; for (int from{}; from < N - 1; from++) { for (int to{from + 1}; to < N - 1; to++) ans += (Mint(x[to]) - Mint(x[from])) * combi.getCombi(N - 1, to - from + 1) * combi.fact[to - from - 1] * combi.fact[N - 1 - (to - from + 1)]; ans += (Mint(x[N - 1]) - Mint(x[from])) * combi.getCombi(N - 1, N - 1 - from) * combi.fact[N - 1 - from - 1] * combi.fact[N - 1 - (N - 1 - from)]; } std::cout << ans << std::endl; return 0; }
#include <bits/stdc++.h> //////////// // ModInt // //////////// // 四則演算の最も左に存在する値がModIntでなければキャストでバグる // 例えばx = mint * 1000;やx = ModInt(1000) * mint;はいいがx = 1000 * // mint;は駄目。 template <int64_t mod_ = 1'000'000'007> class ModInt { private: int64_t integer_; public: constexpr ModInt(const int64_t initial_number = 0) : integer_(initial_number) {} // 四則演算 constexpr ModInt operator+(const ModInt &operand) const { ModInt ret{this->integer_ + operand.integer_}; if (ret.integer_ >= mod_) ret.integer_ -= mod_; return ret; } constexpr ModInt operator-(const ModInt &operand) const { ModInt ret{this->integer_ - operand.integer_}; if (ret.integer_ < 0) ret.integer_ += mod_; return ret; } constexpr ModInt operator*(const ModInt &operand) const { return {this->integer_ * operand.integer_ % mod_}; } constexpr ModInt operator/(const ModInt &operand) const { return *this * (operand ^ (mod_ - 2)); } // 単項演算子 constexpr ModInt &operator++() { if (integer_ + 1 == mod_) integer_ = 0; else integer_++; return *this; } constexpr ModInt &operator--() { if (integer_ == 0) integer_ = mod_ - 1; else integer_--; return *this; } constexpr ModInt operator+() { return *this; } constexpr ModInt operator-() { if (integer_ == 0) return ModInt(0ll); else return ModInt(mod_ - integer_); } // 累乗 constexpr ModInt operator^(const int64_t operand) const { ModInt ret{1}, pow_ope{this->integer_}; for (int64_t pow{operand}; pow > 0; pow >>= 1) { if (pow & 1) ret *= pow_ope; pow_ope *= pow_ope; } return ret; } // 代入 constexpr ModInt &operator=(const ModInt &operand) { this->integer_ = operand.integer_; return *this; } constexpr ModInt &operator+=(const ModInt &operand) { *this = *this + operand; return *this; } constexpr ModInt &operator-=(const ModInt &operand) { *this = *this - operand; return *this; } constexpr ModInt &operator*=(const ModInt &operand) { *this = *this * operand; return *this; } constexpr ModInt &operator/=(const ModInt &operand) { *this = *this / operand; return *this; } // その他 constexpr operator int64_t() { return integer_; } constexpr ModInt getOne() const { return ModInt(1ll); } constexpr ModInt getZero() const { return ModInt(0ll); } static constexpr int64_t mod{mod_}; }; using Mint = ModInt<>; //////////////// // 組み合わせ // /////////////// template <typename T = Mint, int max_ = 200'000> class Combination { public: std::array<Mint, max_ + 1> inv, fact, finv; constexpr Combination() { inv[0] = inv[1] = fact[0] = fact[1] = finv[0] = finv[1] = 1; for (int num{2}; num <= max_; num++) { inv[num] = (T::mod - (int64_t)inv[T::mod % num] * (T::mod / num) % T::mod) % T::mod; fact[num] = num * fact[num - 1] % T::mod; finv[num] = inv[num] * finv[num - 1] % T::mod; } } constexpr Mint getCombi(const int n, const int r) const { if (r < 0 || n < 0 || n - r < 0) return 0; return fact[n] * finv[r] * finv[n - r]; } constexpr Mint getPerm(const int n, const int r) const { if (r < 0 || n < 0 || n - r < 0) return 0; return fact[n] * finv[n - r]; } }; Combination<> combi; int main() { int N; scanf("%d", &N); std::vector<int64_t> x(N); for (auto &e : x) scanf("%lld", &e); std::vector<int64_t> diff(N - 1); for (int i{}; i < N - 1; i++) diff[i] = x[i + 1] - x[i]; Mint ans{}, inv_sum{}; for (int i{}; i < N - 1; i++) { inv_sum += combi.inv[i + 1]; ans += combi.fact[N - 1] * Mint(diff[i]) * inv_sum; } std::cout << ans << std::endl; return 0; }
replace
148
157
148
152
TLE
p02807
C++
Runtime Error
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author Gosu_Hiroo */ #include <fstream> #include <iostream> #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef int #define VI vector<long long> #define V2I(v, size_1, size_2, value) \ vector<vector<long long>> v(size_1, vector<long long>(size_2, value)) #define V3I(v, size_1, size_2, size_3, value) \ vector<vector<vector<long long>>> v( \ size_1, \ vector<vector<long long>>(size_2, vector<long long>(size_3, value))) #define G(g, size_1) vector<vector<long long>> g(size_1, vector<long long>()) #define SZ(x) ((long long)(x).size()) #define READ \ ({ \ long long t; \ cin >> t; \ t; \ }) #define PII pair<long long, long long> #else #define VI vector<int> #define V2I(v, size_1, size_2, value) \ vector<vector<int>> v(size_1, vector<int>(size_2, value)) #define V3I(v, size_1, size_2, size_3, value) \ vector<vector<vector<int>>> v( \ size_1, vector<vector<int>>(size_2, vector<int>(size_3, value))) #define G(g, size_1) vector<vector<int>> g(size_1, vector<int>()) #define SZ(x) ((int)(x).size()) #define READ \ ({ \ int t; \ cin >> t; \ t; \ }) #define PII pair<int, int> #endif #define V2(v, type, size_1, size_2, value) \ vector<vector<type>> v(size_1, vector<type>(size_2, value)) #define V3(v, type, size_1, size_2, size_3, value) \ vector<vector<vector<type>>> v( \ size_1, vector<vector<type>>(size_2, vector<type>(size_3, value))) #define TR(container, it) \ for (auto it = container.begin(); it != container.end(); it++) #define IN(c, x) ((c).find(x) != (c).end()) // O(log n) #define IN_L(c, x) (find((c).begin(), (c).end(), x) != (c).end()) // O(n) #define FOR(i, _begin, _end) \ for (__typeof(_end) end = _end, begin = _begin, \ i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define REP(i, end) for (__typeof(end) i = 0, _len = (end); i < (_len); i += 1) #define ALL(x) (x).begin(), (x).end() #define F first #define S second #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 #define MOD(x, m) ((((x) % (m)) + (m)) % (m)) #define BIT(n) (1LL << (n)) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define EB emplace_back #define PB push_back #define fcout cout << fixed << setprecision(12) #define fcerr cerr << fixed << setprecision(12) #define print(x) cout << (x) << endl #define fprint(x) cout << fixed << setprecision(12) << (x) << endl #define BYE(a) \ do { \ cout << (a) << endl; \ return; \ } while (false) #ifdef DEBUG #define ERR(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ _err(cerr, _it, args); \ } #define DBG(x) cerr << #x << " is " << x << endl; #else #define DBG(x) {}; #define ERR(args...) {}; #endif void _err(std::ostream &cerr, istream_iterator<string> it) { cerr << endl; } template <typename T, typename... Args> void _err(std::ostream &cerr, istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << " "; _err(cerr, ++it, args...); } const double pi = 2 * acos(.0); const int inf = 0x3f3f3f3f; const ll mod = (ll)(1e9) + 7; // const ll mod = (ll) 998244353 ; const double eps = 1e-10; template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } template <typename T> ostream &operator<<(ostream &os, const vector<T> V) { os << "["; int cnt = 0; T curr; if (!V.empty()) { for (int i = 0; i < V.size() - 1; ++i) { if (V[i] == curr) cnt++; else cnt = 0; if (cnt == 4) os << "... "; if (cnt < 4) os << V[i] << " "; curr = V[i]; } os << V.back(); } os << "]"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> P) { os << "("; os << P.first << "," << P.second; os << ")"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const set<T, U> V) { os << "{"; if (!V.empty()) { auto it = V.begin(); for (int i = 0; i < V.size() - 1; ++i) { os << *it << " "; it++; } os << *it; } os << "}"; return os; } template <typename K, typename H, typename P> ostream &operator<<(ostream &os, const unordered_set<K, H, P> V) { os << "{"; if (!V.empty()) { auto it = V.begin(); for (int i = 0; i < V.size() - 1; ++i) { os << *it << " "; it++; } os << *it; } os << "}"; return os; } template <typename K, typename C> ostream &operator<<(ostream &os, const multiset<K, C> V) { os << "{"; if (!V.empty()) { auto it = V.begin(); for (int i = 0; i < V.size() - 1; ++i) { os << *it << " "; it++; } os << *it; } os << "}"; return os; } template <typename K, typename T, typename C> ostream &operator<<(ostream &os, const map<K, T, C> V) { os << "{"; if (!V.empty()) { auto it = V.begin(); for (int i = 0; i < V.size() - 1; ++i) { os << "("; os << it->first << "," << it->second; os << ") "; it++; } os << "("; os << it->first << "," << it->second; os << ")"; } os << "}"; return os; } template <typename K, typename T, typename C> ostream &operator<<(ostream &os, const unordered_map<K, T, C> V) { os << "{"; if (!V.empty()) { auto it = V.begin(); for (int i = 0; i < V.size() - 1; ++i) { os << "("; os << it->first << "," << it->second; os << ") "; it++; } os << "("; os << it->first << "," << it->second; os << ")"; } os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> V) { os << "["; if (!V.empty()) { for (int i = 0; i < V.size() - 1; ++i) { os << V[i] << "->"; } if (!V.empty()) os << V.back(); } os << "]"; return os; }; template <typename T, typename Cont, typename Comp> ostream &operator<<(ostream &os, const priority_queue<T, Cont, Comp> V) { priority_queue<T, Cont, Comp> _V = V; os << "["; if (!_V.empty()) { while (_V.size() > 1) { os << _V.top() << "->"; _V.pop(); } os << _V.top(); } os << "]"; return os; }; template <class F> struct y_combinator { F f; // the lambda will be stored here // a forwarding operator(): template <class... Args> decltype(auto) operator()(Args &&...args) const { // we pass ourselves to f, then the arguments. // the lambda should take the first argument as `auto&& recurse` or similar. return f(*this, std::forward<Args>(args)...); } }; // helper function that deduces the type of the lambda: template <class F> y_combinator<std::decay_t<F>> recursive(F &&f) { return {std::forward<F>(f)}; } struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; /* struct X{ int x,y,id; bool operator < (const X R)const{ return id < R.id; } friend ostream& operator << (ostream& os, X R){ os << "(" << R.x << "," << R.y << "," << R.id << ")"; } friend bool operator == (const X L, const X R){ return L.id == R.id; } */ template <class T> void Chmod(T &a, const T &m) { a = MOD(a, m); } template <class T> bool Chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool Chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } // 256MB = 2^29(~5.3*10^8)*sizeof(int) // #define int ll //2^31=2.15*10^9, 2^63=9.22*10^18 template <signed M, unsigned T> struct mod_int { constexpr static signed MODULO = M; constexpr static unsigned TABLE_SIZE = T; signed x; mod_int() : x(0) {} mod_int(long long y) : x(static_cast<signed>(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO)) {} mod_int(int y) : x(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO) {} explicit operator int() const { return x; } explicit operator long long() const { return x; } explicit operator double() const { return x; } mod_int &operator+=(const mod_int &rhs) { if ((x += rhs.x) >= MODULO) x -= MODULO; return *this; } mod_int &operator-=(const mod_int &rhs) { if ((x += MODULO - rhs.x) >= MODULO) x -= MODULO; return *this; } mod_int &operator*=(const mod_int &rhs) { x = static_cast<signed>(1LL * x * rhs.x % MODULO); return *this; } mod_int &operator/=(const mod_int &rhs) { x = static_cast<signed>((1LL * x * rhs.inv().x) % MODULO); return *this; } mod_int operator-() const { return mod_int(-x); } mod_int operator+(const mod_int &rhs) const { return mod_int(*this) += rhs; } mod_int operator-(const mod_int &rhs) const { return mod_int(*this) -= rhs; } mod_int operator*(const mod_int &rhs) const { return mod_int(*this) *= rhs; } mod_int operator/(const mod_int &rhs) const { return mod_int(*this) /= rhs; } bool operator<(const mod_int &rhs) const { return x < rhs.x; } mod_int inv() const { assert(x != 0); if (x <= static_cast<signed>(TABLE_SIZE)) { if (_inv[1].x == 0) prepare(); return _inv[x]; } else { signed a = x, b = MODULO, u = 1, v = 0, t; while (b) { t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return mod_int(u); } } mod_int pow(long long t) const { assert(!(x == 0 && t == 0)); mod_int e = *this, res = mod_int(1); for (; t; e *= e, t >>= 1) if (t & 1) res *= e; return res; } mod_int fact() { if (_fact[0].x == 0) prepare(); return _fact[x]; } mod_int inv_fact() { if (_fact[0].x == 0) prepare(); return _inv_fact[x]; } mod_int choose(mod_int y) { assert(y.x <= x); return this->fact() * y.inv_fact() * mod_int(x - y.x).inv_fact(); } static mod_int _inv[TABLE_SIZE + 1]; static mod_int _fact[TABLE_SIZE + 1]; static mod_int _inv_fact[TABLE_SIZE + 1]; static void prepare() { _inv[1] = 1; for (int i = 2; i <= (int)TABLE_SIZE; ++i) { _inv[i] = 1LL * _inv[MODULO % i].x * (MODULO - MODULO / i) % MODULO; } _fact[0] = 1; for (unsigned i = 1; i <= TABLE_SIZE; ++i) { _fact[i] = _fact[i - 1] * int(i); } _inv_fact[TABLE_SIZE] = _fact[TABLE_SIZE].inv(); for (int i = (int)TABLE_SIZE - 1; i >= 0; --i) { _inv_fact[i] = _inv_fact[i + 1] * (i + 1); } } }; template <int M, unsigned F> std::ostream &operator<<(std::ostream &os, const mod_int<M, F> &rhs) { return os << rhs.x; } template <int M, unsigned F> std::istream &operator>>(std::istream &is, mod_int<M, F> &rhs) { long long s; is >> s; rhs = mod_int<M, F>(s); return is; } template <int M, unsigned F> mod_int<M, F> mod_int<M, F>::_inv[TABLE_SIZE + 1]; template <int M, unsigned F> mod_int<M, F> mod_int<M, F>::_fact[TABLE_SIZE + 1]; template <int M, unsigned F> mod_int<M, F> mod_int<M, F>::_inv_fact[TABLE_SIZE + 1]; template <int M, unsigned F> bool operator==(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) { return lhs.x == rhs.x; } template <int M, unsigned F> bool operator!=(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) { return !(lhs == rhs); } const int MF = 1000010; const int MOD = 1000000007; using mint = mod_int<MOD, MF>; mint binom(int n, int r) { return (r < 0 || r > n || n < 0) ? 0 : mint(n).choose(r); } mint fact(int n) { return mint(n).fact(); } mint inv_fact(int n) { return mint(n).inv_fact(); } class BFusingSlimes { public: void solve(std::istream &cin, std::ostream &cout, std::ostream &cerr) { int N; cin >> N; VI x(N); REP(i, N) cin >> x[i]; mint ans = fact(N - 1) * (x[N - 1] - x[0]); vector<mint> a(N + 5); REP(i, N + 5) a[i + 1] = a[i] + mint(1) / mint(i + 2); ERR(a) // FOR(i,1,N-1)FOR(j,1,i+1){ // ERR(i,j,ans) // ans += fact(N-1)/(j+1)*(x[i+1] - x[i]); //// ans += mint(i).choose(j)*fact(N-1-j)*(x[i+1] - x[i]); // } FOR(i, 1, N - 1) ans += fact(N - 1) * (x[i + 1] - x[i]) * a[i]; print(ans); } }; #undef int int main() { BFusingSlimes solver; std::istream &in(std::cin); std::ostream &out(std::cout); std::ostringstream err; in.tie(0); ios::sync_with_stdio(0); // solver.solve(in, out); solver.solve(in, out, err); return 0; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author Gosu_Hiroo */ #include <fstream> #include <iostream> #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef int #define VI vector<long long> #define V2I(v, size_1, size_2, value) \ vector<vector<long long>> v(size_1, vector<long long>(size_2, value)) #define V3I(v, size_1, size_2, size_3, value) \ vector<vector<vector<long long>>> v( \ size_1, \ vector<vector<long long>>(size_2, vector<long long>(size_3, value))) #define G(g, size_1) vector<vector<long long>> g(size_1, vector<long long>()) #define SZ(x) ((long long)(x).size()) #define READ \ ({ \ long long t; \ cin >> t; \ t; \ }) #define PII pair<long long, long long> #else #define VI vector<int> #define V2I(v, size_1, size_2, value) \ vector<vector<int>> v(size_1, vector<int>(size_2, value)) #define V3I(v, size_1, size_2, size_3, value) \ vector<vector<vector<int>>> v( \ size_1, vector<vector<int>>(size_2, vector<int>(size_3, value))) #define G(g, size_1) vector<vector<int>> g(size_1, vector<int>()) #define SZ(x) ((int)(x).size()) #define READ \ ({ \ int t; \ cin >> t; \ t; \ }) #define PII pair<int, int> #endif #define V2(v, type, size_1, size_2, value) \ vector<vector<type>> v(size_1, vector<type>(size_2, value)) #define V3(v, type, size_1, size_2, size_3, value) \ vector<vector<vector<type>>> v( \ size_1, vector<vector<type>>(size_2, vector<type>(size_3, value))) #define TR(container, it) \ for (auto it = container.begin(); it != container.end(); it++) #define IN(c, x) ((c).find(x) != (c).end()) // O(log n) #define IN_L(c, x) (find((c).begin(), (c).end(), x) != (c).end()) // O(n) #define FOR(i, _begin, _end) \ for (__typeof(_end) end = _end, begin = _begin, \ i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define REP(i, end) for (__typeof(end) i = 0, _len = (end); i < (_len); i += 1) #define ALL(x) (x).begin(), (x).end() #define F first #define S second #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 #define MOD(x, m) ((((x) % (m)) + (m)) % (m)) #define BIT(n) (1LL << (n)) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define EB emplace_back #define PB push_back #define fcout cout << fixed << setprecision(12) #define fcerr cerr << fixed << setprecision(12) #define print(x) cout << (x) << endl #define fprint(x) cout << fixed << setprecision(12) << (x) << endl #define BYE(a) \ do { \ cout << (a) << endl; \ return; \ } while (false) #ifdef DEBUG #define ERR(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ _err(cerr, _it, args); \ } #define DBG(x) cerr << #x << " is " << x << endl; #else #define DBG(x) {}; #define ERR(args...) {}; #endif void _err(std::ostream &cerr, istream_iterator<string> it) { cerr << endl; } template <typename T, typename... Args> void _err(std::ostream &cerr, istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << " "; _err(cerr, ++it, args...); } const double pi = 2 * acos(.0); const int inf = 0x3f3f3f3f; const ll mod = (ll)(1e9) + 7; // const ll mod = (ll) 998244353 ; const double eps = 1e-10; template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } template <typename T> ostream &operator<<(ostream &os, const vector<T> V) { os << "["; int cnt = 0; T curr; if (!V.empty()) { for (int i = 0; i < V.size() - 1; ++i) { if (V[i] == curr) cnt++; else cnt = 0; if (cnt == 4) os << "... "; if (cnt < 4) os << V[i] << " "; curr = V[i]; } os << V.back(); } os << "]"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> P) { os << "("; os << P.first << "," << P.second; os << ")"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const set<T, U> V) { os << "{"; if (!V.empty()) { auto it = V.begin(); for (int i = 0; i < V.size() - 1; ++i) { os << *it << " "; it++; } os << *it; } os << "}"; return os; } template <typename K, typename H, typename P> ostream &operator<<(ostream &os, const unordered_set<K, H, P> V) { os << "{"; if (!V.empty()) { auto it = V.begin(); for (int i = 0; i < V.size() - 1; ++i) { os << *it << " "; it++; } os << *it; } os << "}"; return os; } template <typename K, typename C> ostream &operator<<(ostream &os, const multiset<K, C> V) { os << "{"; if (!V.empty()) { auto it = V.begin(); for (int i = 0; i < V.size() - 1; ++i) { os << *it << " "; it++; } os << *it; } os << "}"; return os; } template <typename K, typename T, typename C> ostream &operator<<(ostream &os, const map<K, T, C> V) { os << "{"; if (!V.empty()) { auto it = V.begin(); for (int i = 0; i < V.size() - 1; ++i) { os << "("; os << it->first << "," << it->second; os << ") "; it++; } os << "("; os << it->first << "," << it->second; os << ")"; } os << "}"; return os; } template <typename K, typename T, typename C> ostream &operator<<(ostream &os, const unordered_map<K, T, C> V) { os << "{"; if (!V.empty()) { auto it = V.begin(); for (int i = 0; i < V.size() - 1; ++i) { os << "("; os << it->first << "," << it->second; os << ") "; it++; } os << "("; os << it->first << "," << it->second; os << ")"; } os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> V) { os << "["; if (!V.empty()) { for (int i = 0; i < V.size() - 1; ++i) { os << V[i] << "->"; } if (!V.empty()) os << V.back(); } os << "]"; return os; }; template <typename T, typename Cont, typename Comp> ostream &operator<<(ostream &os, const priority_queue<T, Cont, Comp> V) { priority_queue<T, Cont, Comp> _V = V; os << "["; if (!_V.empty()) { while (_V.size() > 1) { os << _V.top() << "->"; _V.pop(); } os << _V.top(); } os << "]"; return os; }; template <class F> struct y_combinator { F f; // the lambda will be stored here // a forwarding operator(): template <class... Args> decltype(auto) operator()(Args &&...args) const { // we pass ourselves to f, then the arguments. // the lambda should take the first argument as `auto&& recurse` or similar. return f(*this, std::forward<Args>(args)...); } }; // helper function that deduces the type of the lambda: template <class F> y_combinator<std::decay_t<F>> recursive(F &&f) { return {std::forward<F>(f)}; } struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; /* struct X{ int x,y,id; bool operator < (const X R)const{ return id < R.id; } friend ostream& operator << (ostream& os, X R){ os << "(" << R.x << "," << R.y << "," << R.id << ")"; } friend bool operator == (const X L, const X R){ return L.id == R.id; } */ template <class T> void Chmod(T &a, const T &m) { a = MOD(a, m); } template <class T> bool Chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool Chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } // 256MB = 2^29(~5.3*10^8)*sizeof(int) // #define int ll //2^31=2.15*10^9, 2^63=9.22*10^18 template <signed M, unsigned T> struct mod_int { constexpr static signed MODULO = M; constexpr static unsigned TABLE_SIZE = T; signed x; mod_int() : x(0) {} mod_int(long long y) : x(static_cast<signed>(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO)) {} mod_int(int y) : x(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO) {} explicit operator int() const { return x; } explicit operator long long() const { return x; } explicit operator double() const { return x; } mod_int &operator+=(const mod_int &rhs) { if ((x += rhs.x) >= MODULO) x -= MODULO; return *this; } mod_int &operator-=(const mod_int &rhs) { if ((x += MODULO - rhs.x) >= MODULO) x -= MODULO; return *this; } mod_int &operator*=(const mod_int &rhs) { x = static_cast<signed>(1LL * x * rhs.x % MODULO); return *this; } mod_int &operator/=(const mod_int &rhs) { x = static_cast<signed>((1LL * x * rhs.inv().x) % MODULO); return *this; } mod_int operator-() const { return mod_int(-x); } mod_int operator+(const mod_int &rhs) const { return mod_int(*this) += rhs; } mod_int operator-(const mod_int &rhs) const { return mod_int(*this) -= rhs; } mod_int operator*(const mod_int &rhs) const { return mod_int(*this) *= rhs; } mod_int operator/(const mod_int &rhs) const { return mod_int(*this) /= rhs; } bool operator<(const mod_int &rhs) const { return x < rhs.x; } mod_int inv() const { assert(x != 0); if (x <= static_cast<signed>(TABLE_SIZE)) { if (_inv[1].x == 0) prepare(); return _inv[x]; } else { signed a = x, b = MODULO, u = 1, v = 0, t; while (b) { t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return mod_int(u); } } mod_int pow(long long t) const { assert(!(x == 0 && t == 0)); mod_int e = *this, res = mod_int(1); for (; t; e *= e, t >>= 1) if (t & 1) res *= e; return res; } mod_int fact() { if (_fact[0].x == 0) prepare(); return _fact[x]; } mod_int inv_fact() { if (_fact[0].x == 0) prepare(); return _inv_fact[x]; } mod_int choose(mod_int y) { assert(y.x <= x); return this->fact() * y.inv_fact() * mod_int(x - y.x).inv_fact(); } static mod_int _inv[TABLE_SIZE + 1]; static mod_int _fact[TABLE_SIZE + 1]; static mod_int _inv_fact[TABLE_SIZE + 1]; static void prepare() { _inv[1] = 1; for (int i = 2; i <= (int)TABLE_SIZE; ++i) { _inv[i] = 1LL * _inv[MODULO % i].x * (MODULO - MODULO / i) % MODULO; } _fact[0] = 1; for (unsigned i = 1; i <= TABLE_SIZE; ++i) { _fact[i] = _fact[i - 1] * int(i); } _inv_fact[TABLE_SIZE] = _fact[TABLE_SIZE].inv(); for (int i = (int)TABLE_SIZE - 1; i >= 0; --i) { _inv_fact[i] = _inv_fact[i + 1] * (i + 1); } } }; template <int M, unsigned F> std::ostream &operator<<(std::ostream &os, const mod_int<M, F> &rhs) { return os << rhs.x; } template <int M, unsigned F> std::istream &operator>>(std::istream &is, mod_int<M, F> &rhs) { long long s; is >> s; rhs = mod_int<M, F>(s); return is; } template <int M, unsigned F> mod_int<M, F> mod_int<M, F>::_inv[TABLE_SIZE + 1]; template <int M, unsigned F> mod_int<M, F> mod_int<M, F>::_fact[TABLE_SIZE + 1]; template <int M, unsigned F> mod_int<M, F> mod_int<M, F>::_inv_fact[TABLE_SIZE + 1]; template <int M, unsigned F> bool operator==(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) { return lhs.x == rhs.x; } template <int M, unsigned F> bool operator!=(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) { return !(lhs == rhs); } const int MF = 1000010; const int MOD = 1000000007; using mint = mod_int<MOD, MF>; mint binom(int n, int r) { return (r < 0 || r > n || n < 0) ? 0 : mint(n).choose(r); } mint fact(int n) { return mint(n).fact(); } mint inv_fact(int n) { return mint(n).inv_fact(); } class BFusingSlimes { public: void solve(std::istream &cin, std::ostream &cout, std::ostream &cerr) { int N; cin >> N; VI x(N); REP(i, N) cin >> x[i]; mint ans = fact(N - 1) * (x[N - 1] - x[0]); vector<mint> a(N + 5); REP(i, N + 3) a[i + 1] = a[i] + mint(1) / mint(i + 2); ERR(a) // FOR(i,1,N-1)FOR(j,1,i+1){ // ERR(i,j,ans) // ans += fact(N-1)/(j+1)*(x[i+1] - x[i]); //// ans += mint(i).choose(j)*fact(N-1-j)*(x[i+1] - x[i]); // } FOR(i, 1, N - 1) ans += fact(N - 1) * (x[i + 1] - x[i]) * a[i]; print(ans); } }; #undef int int main() { BFusingSlimes solver; std::istream &in(std::cin); std::ostream &out(std::cout); std::ostringstream err; in.tie(0); ios::sync_with_stdio(0); // solver.solve(in, out); solver.solve(in, out, err); return 0; }
replace
482
483
482
483
0
p02807
C++
Time Limit Exceeded
#include <iostream> #include <string> #include <vector> #define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i) #define rrep(i, start, end) for (int i = (int)start - 1; i >= end; --i) #define iter(i, c) for (auto i = (c).begin(); i != (c).end(); ++i) #define riter(i, c) for (auto i = (c).rbegin(); i != (c).rend(); ++i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() using namespace std; using ll = long long; template <typename T, long long MOD_VALUE> class ModInt { static constexpr long long MOD = MOD_VALUE; private: T value_; public: ModInt() {} ModInt(const T &value) : value_(value % MOD) {} ModInt &operator+=(const ModInt &x) { value_ += x.value_; if (value_ >= MOD) value_ -= MOD; return *this; } friend ModInt &operator+=(const T &x, const ModInt &y) { ModInt res(x); res.value_ += x.value_; if (res.value_ >= MOD) res.value_ -= MOD; return res; } ModInt &operator-=(const ModInt &x) { if (value_ < x.value_) value_ += MOD; value_ -= x.value_; return *this; } friend ModInt &operator-=(const T &x, const ModInt &y) { ModInt res(x); if (res.value_ < y.value_) res.value_ += MOD; res.value_ -= y.value_; return res; } ModInt &operator*=(const ModInt &x) { value_ = (value_ * x.value_) % MOD; return *this; } friend ModInt &operator*=(const T &x, const ModInt &y) { ModInt res(x); res.value_ = (res.value_ * y.value_) % MOD; return res; } const ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; } friend const ModInt operator+(const T &x, const ModInt &y) { return ModInt(x) += y; } const ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; } friend const ModInt operator-(const T &x, const ModInt &y) { return ModInt(x) -= y; } const ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; } friend const ModInt operator*(const T &x, const ModInt &y) { return ModInt(x) *= y; } static ModInt modpow(ModInt x, long long y) { ModInt z = 1; while (y > 0) { if (y & 1) { z *= x; } x *= x; y /= 2; } return z; } ModInt &operator/=(const ModInt &x) { return *this *= modpow(x, MOD - 2); } const ModInt operator/(const ModInt &x) const { return ModInt(*this) /= x; } friend const ModInt operator/(const T &x, const ModInt &y) { return ModInt(x) /= y; } ModInt operator++(int) { ModInt tmp(*this); value_ = (value_ + 1 == MOD ? 0 : value_ + 1); return tmp; } ModInt operator--(int) { ModInt tmp(*this); value_ = (value_ - 1 < 0 ? MOD - 1 : value_ - 1); return tmp; } friend istream &operator>>(istream &stream, ModInt &x) { stream >> x.value_; x.value_ %= MOD; return stream; } friend ostream &operator<<(ostream &stream, const ModInt &x) { stream << x.value_; return stream; } }; using mint = ModInt<ll, 1000000007>; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<mint> X(N); rep(i, 0, N) cin >> X[i]; mint ans = 0; rep(i, 0, N - 1) { mint p = 0; rep(j, 0, i + 1) { p += mint(1) / (i - j + 1); } ans += (X[i + 1] - X[i]) * p; } rep(i, 1, N) { ans *= i; } cout << ans << endl; return 0; }
#include <iostream> #include <string> #include <vector> #define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i) #define rrep(i, start, end) for (int i = (int)start - 1; i >= end; --i) #define iter(i, c) for (auto i = (c).begin(); i != (c).end(); ++i) #define riter(i, c) for (auto i = (c).rbegin(); i != (c).rend(); ++i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() using namespace std; using ll = long long; template <typename T, long long MOD_VALUE> class ModInt { static constexpr long long MOD = MOD_VALUE; private: T value_; public: ModInt() {} ModInt(const T &value) : value_(value % MOD) {} ModInt &operator+=(const ModInt &x) { value_ += x.value_; if (value_ >= MOD) value_ -= MOD; return *this; } friend ModInt &operator+=(const T &x, const ModInt &y) { ModInt res(x); res.value_ += x.value_; if (res.value_ >= MOD) res.value_ -= MOD; return res; } ModInt &operator-=(const ModInt &x) { if (value_ < x.value_) value_ += MOD; value_ -= x.value_; return *this; } friend ModInt &operator-=(const T &x, const ModInt &y) { ModInt res(x); if (res.value_ < y.value_) res.value_ += MOD; res.value_ -= y.value_; return res; } ModInt &operator*=(const ModInt &x) { value_ = (value_ * x.value_) % MOD; return *this; } friend ModInt &operator*=(const T &x, const ModInt &y) { ModInt res(x); res.value_ = (res.value_ * y.value_) % MOD; return res; } const ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; } friend const ModInt operator+(const T &x, const ModInt &y) { return ModInt(x) += y; } const ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; } friend const ModInt operator-(const T &x, const ModInt &y) { return ModInt(x) -= y; } const ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; } friend const ModInt operator*(const T &x, const ModInt &y) { return ModInt(x) *= y; } static ModInt modpow(ModInt x, long long y) { ModInt z = 1; while (y > 0) { if (y & 1) { z *= x; } x *= x; y /= 2; } return z; } ModInt &operator/=(const ModInt &x) { return *this *= modpow(x, MOD - 2); } const ModInt operator/(const ModInt &x) const { return ModInt(*this) /= x; } friend const ModInt operator/(const T &x, const ModInt &y) { return ModInt(x) /= y; } ModInt operator++(int) { ModInt tmp(*this); value_ = (value_ + 1 == MOD ? 0 : value_ + 1); return tmp; } ModInt operator--(int) { ModInt tmp(*this); value_ = (value_ - 1 < 0 ? MOD - 1 : value_ - 1); return tmp; } friend istream &operator>>(istream &stream, ModInt &x) { stream >> x.value_; x.value_ %= MOD; return stream; } friend ostream &operator<<(ostream &stream, const ModInt &x) { stream << x.value_; return stream; } }; using mint = ModInt<ll, 1000000007>; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<mint> X(N); rep(i, 0, N) cin >> X[i]; mint ans = 0; vector<mint> cumsum(N + 1, 0); rep(i, 1, N + 1) { cumsum[i] = cumsum[i - 1] + mint(1) / i; } rep(i, 0, N - 1) { ans += (X[i + 1] - X[i]) * cumsum[i + 1]; } rep(i, 1, N) { ans *= i; } cout << ans << endl; return 0; }
replace
115
120
115
118
TLE
p02807
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <algorithm> #include <array> #include <bitset> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <utility> #include <vector> using namespace std; #define INF 0x3f3f3f3f #define INFLL 0x3f3f3f3f3f3f3f3fLL // #define MOD 998244353 #define MOD 1000000007 #define mp make_pair #define mt make_tuple #define pb push_back #define eb emplace_back typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pint; typedef pair<ll, ll> pll; typedef tuple<int, int, int> tint; typedef vector<int> vint; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<pint> vpint; int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1}; int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1}; const int SIZE = 10000; // ↑templete ll mod_pow(ll x, ll n, ll mod) { if (n == 0) return 1; ll res = mod_pow((x * x) % mod, n / 2, mod); if (n & 1) res = (res * x) % mod; return res; } signed main() { ll N; cin >> N; vll A, D; for (int i = 0; i < N; i++) { int a; cin >> a; if (A.size()) D.pb(a - A.back()); A.pb(a); } ll C[SIZE] = {1, 1}; for (int i = 2; i < N; i++) { C[i] = C[i - 1] + mod_pow(i, MOD - 2, MOD); C[i] %= MOD; } ll ans = 0; for (int i = 0; i < N - 1; i++) { ans += D[i] * C[i + 1]; ans %= MOD; } for (int i = 1; i <= N - 1; i++) { ans *= i; ans %= MOD; } cout << ans << endl; return 0; }
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <algorithm> #include <array> #include <bitset> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <utility> #include <vector> using namespace std; #define INF 0x3f3f3f3f #define INFLL 0x3f3f3f3f3f3f3f3fLL // #define MOD 998244353 #define MOD 1000000007 #define mp make_pair #define mt make_tuple #define pb push_back #define eb emplace_back typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pint; typedef pair<ll, ll> pll; typedef tuple<int, int, int> tint; typedef vector<int> vint; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<pint> vpint; int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1}; int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1}; const int SIZE = 100050; // ↑templete ll mod_pow(ll x, ll n, ll mod) { if (n == 0) return 1; ll res = mod_pow((x * x) % mod, n / 2, mod); if (n & 1) res = (res * x) % mod; return res; } signed main() { ll N; cin >> N; vll A, D; for (int i = 0; i < N; i++) { int a; cin >> a; if (A.size()) D.pb(a - A.back()); A.pb(a); } ll C[SIZE] = {1, 1}; for (int i = 2; i < N; i++) { C[i] = C[i - 1] + mod_pow(i, MOD - 2, MOD); C[i] %= MOD; } ll ans = 0; for (int i = 0; i < N - 1; i++) { ans += D[i] * C[i + 1]; ans %= MOD; } for (int i = 1; i <= N - 1; i++) { ans *= i; ans %= MOD; } cout << ans << endl; return 0; }
replace
45
46
45
46
0
p02807
C++
Runtime Error
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (int)(b); i++) #define FORD(i, a, b) for (int i = a; i > (int)(b); i--) #define PPC(x) __builtin_popcount(x) #define pb push_back #define ALL(x) (x).begin(), (x).end() #define ft first #define sd second #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; const int maxN = 1 << 11, mod = 1000000007; template <class T1, class T2> inline void addMod(T1 &a, T2 b) { a = (a + b) % mod; } template <class T1, class T2> inline void multMod(T1 &a, T2 b) { a = a * b % mod; } long long qpow(long long a, long long b) { long long res0 = 1; for (; b != 0; b >>= 1) { if (b & 1) multMod(res0, a); multMod(a, a); } return res0; } long long fact[maxN], tcaf[maxN]; long long cf(int n, int k) { bool static initd = false; if (!initd) { fact[0] = 1; FOR(i, 1, maxN) fact[i] = fact[i - 1] * i % mod; tcaf[maxN - 1] = qpow(fact[maxN - 1], mod - 2); for (int i = maxN - 1; i > 0; i--) tcaf[i - 1] = tcaf[i] * i % mod; initd = true; } if (k < 0 or k > n) return 0; long long res0 = fact[n]; multMod(res0, tcaf[k]); multMod(res0, tcaf[n - k]); return res0; } long long f(int n, int d) { return cf(n + 1, d + 1); } int x[maxN]; long long pref[maxN]; int main() { int n; scanf("%d", &n); cf(n, 0); FOR(i, 0, n) scanf("%d", x + i); FOR(i, 1, n + 1) pref[i] = (pref[i - 1] + qpow(i, mod - 2)) % mod; long long res = 0; FOR(i, 0, n - 1) addMod(res, pref[i + 1] * (x[i + 1] - x[i])); multMod(res, fact[n - 1]); printf("%lld\n", res); return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (int)(b); i++) #define FORD(i, a, b) for (int i = a; i > (int)(b); i--) #define PPC(x) __builtin_popcount(x) #define pb push_back #define ALL(x) (x).begin(), (x).end() #define ft first #define sd second #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; const int maxN = 1 << 20, mod = 1000000007; template <class T1, class T2> inline void addMod(T1 &a, T2 b) { a = (a + b) % mod; } template <class T1, class T2> inline void multMod(T1 &a, T2 b) { a = a * b % mod; } long long qpow(long long a, long long b) { long long res0 = 1; for (; b != 0; b >>= 1) { if (b & 1) multMod(res0, a); multMod(a, a); } return res0; } long long fact[maxN], tcaf[maxN]; long long cf(int n, int k) { bool static initd = false; if (!initd) { fact[0] = 1; FOR(i, 1, maxN) fact[i] = fact[i - 1] * i % mod; tcaf[maxN - 1] = qpow(fact[maxN - 1], mod - 2); for (int i = maxN - 1; i > 0; i--) tcaf[i - 1] = tcaf[i] * i % mod; initd = true; } if (k < 0 or k > n) return 0; long long res0 = fact[n]; multMod(res0, tcaf[k]); multMod(res0, tcaf[n - k]); return res0; } long long f(int n, int d) { return cf(n + 1, d + 1); } int x[maxN]; long long pref[maxN]; int main() { int n; scanf("%d", &n); cf(n, 0); FOR(i, 0, n) scanf("%d", x + i); FOR(i, 1, n + 1) pref[i] = (pref[i - 1] + qpow(i, mod - 2)) % mod; long long res = 0; FOR(i, 0, n - 1) addMod(res, pref[i + 1] * (x[i + 1] - x[i])); multMod(res, fact[n - 1]); printf("%lld\n", res); return 0; }
replace
16
17
16
17
0
p02807
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) { return s << '<' << P.first << ", " << P.second << '>'; } template <class T> ostream &operator<<(ostream &s, vector<T> P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } #define EACH(i, s) \ for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) template <class T> ostream &operator<<(ostream &s, set<T> P) { EACH(it, P) { s << "<" << *it << "> "; } return s << endl; } template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; } template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } template <class T, class V> typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) { t = v; } template <class T, class V> typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) { for (auto &e : t) fill(e, v); } // modint: mod 計算を int を扱うように扱える構造体 template <int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; } constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; } constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; } constexpr Fp &operator+=(const Fp &r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp &operator-=(const Fp &r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp &operator*=(const Fp &r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp &operator/=(const Fp &r) noexcept { long long a = r.val, 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); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator==(const Fp &r) const noexcept { return this->val == r.val; } constexpr bool operator!=(const Fp &r) const noexcept { return this->val != r.val; } friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept { return os << x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; // 二項係数ライブラリ template <class T> struct BiCoef { vector<T> fact_, inv_, finv_; constexpr BiCoef() {} constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) { init(n); } constexpr void init(int n) noexcept { fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1); int MOD = fact_[0].getmod(); for (int i = 2; i < n; i++) { fact_[i] = fact_[i - 1] * i; inv_[i] = -inv_[MOD % i] * (MOD / i); finv_[i] = finv_[i - 1] * inv_[i]; } } constexpr T com(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return fact_[n] * finv_[k] * finv_[n - k]; } constexpr T fact(int n) const noexcept { if (n < 0) return 0; return fact_[n]; } constexpr T inv(int n) const noexcept { if (n < 0) return 0; return inv_[n]; } constexpr T finv(int n) const noexcept { if (n < 0) return 0; return finv_[n]; } }; const int MOD = 1000000007; using mint = Fp<MOD>; BiCoef<mint> bc; int N; vector<long long> x; int main() { ios::sync_with_stdio(false); cin.tie(0); bc.init(210000); while (cin >> N) { x.resize(N); for (int i = 0; i < N; ++i) cin >> x[i]; mint res = 0; /* vector<mint> fac({mint(1)}); for (int i = 0; i < N-2; ++i) { for (int j = 0; j < fac.size(); ++j) { fac[j] *= i+2; } fac.push_back(fac.back() + bc.fact(i+1)); } COUT(fac); */ vector<mint> fac(N - 1); fac[0] = bc.fact(N - 1); for (int i = 0; i < N - 1; ++i) { fac[i + 1] = fac[i] + bc.fact(N - 1) * bc.inv(i + 2); } for (int i = 0; i < N - 1; ++i) { long long d = x[i + 1] - x[i]; res += fac[i] * d; } cout << res << endl; } }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) { return s << '<' << P.first << ", " << P.second << '>'; } template <class T> ostream &operator<<(ostream &s, vector<T> P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } #define EACH(i, s) \ for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) template <class T> ostream &operator<<(ostream &s, set<T> P) { EACH(it, P) { s << "<" << *it << "> "; } return s << endl; } template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; } template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } template <class T, class V> typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) { t = v; } template <class T, class V> typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) { for (auto &e : t) fill(e, v); } // modint: mod 計算を int を扱うように扱える構造体 template <int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; } constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; } constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; } constexpr Fp &operator+=(const Fp &r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp &operator-=(const Fp &r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp &operator*=(const Fp &r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp &operator/=(const Fp &r) noexcept { long long a = r.val, 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); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator==(const Fp &r) const noexcept { return this->val == r.val; } constexpr bool operator!=(const Fp &r) const noexcept { return this->val != r.val; } friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept { return os << x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; // 二項係数ライブラリ template <class T> struct BiCoef { vector<T> fact_, inv_, finv_; constexpr BiCoef() {} constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) { init(n); } constexpr void init(int n) noexcept { fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1); int MOD = fact_[0].getmod(); for (int i = 2; i < n; i++) { fact_[i] = fact_[i - 1] * i; inv_[i] = -inv_[MOD % i] * (MOD / i); finv_[i] = finv_[i - 1] * inv_[i]; } } constexpr T com(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return fact_[n] * finv_[k] * finv_[n - k]; } constexpr T fact(int n) const noexcept { if (n < 0) return 0; return fact_[n]; } constexpr T inv(int n) const noexcept { if (n < 0) return 0; return inv_[n]; } constexpr T finv(int n) const noexcept { if (n < 0) return 0; return finv_[n]; } }; const int MOD = 1000000007; using mint = Fp<MOD>; BiCoef<mint> bc; int N; vector<long long> x; int main() { ios::sync_with_stdio(false); cin.tie(0); bc.init(210000); while (cin >> N) { x.resize(N); for (int i = 0; i < N; ++i) cin >> x[i]; mint res = 0; /* vector<mint> fac({mint(1)}); for (int i = 0; i < N-2; ++i) { for (int j = 0; j < fac.size(); ++j) { fac[j] *= i+2; } fac.push_back(fac.back() + bc.fact(i+1)); } COUT(fac); */ vector<mint> fac(N); fac[0] = bc.fact(N - 1); for (int i = 0; i < N - 1; ++i) { fac[i + 1] = fac[i] + bc.fact(N - 1) * bc.inv(i + 2); } for (int i = 0; i < N - 1; ++i) { long long d = x[i + 1] - x[i]; res += fac[i] * d; } cout << res << endl; } }
replace
214
215
214
215
0
p02807
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define ii pair<int, int> #define app push_back #define all(a) a.begin(), a.end() #define bp __builtin_popcount #define ll long long #define mp make_pair #define f first #define s second #define Time (double)clock() / CLOCKS_PER_SEC const int MOD = 1000 * 1000 * 1000 + 7; int mod(int n) { n %= MOD; if (n < 0) return n + MOD; else return n; } int fp(int a, int p) { int ans = 1, c = a; for (int i = 0; (1ll << i) <= p; ++i) { if ((p >> i) & 1) ans = mod(ans * c); c = mod(c * c); } return ans; } int dv(int a, int b) { return mod(a * fp(b, MOD - 2)); } const int N = 1e5 + 7; int a[N]; int inv[N]; signed main() { #ifdef HOME freopen("input.txt", "r", stdin); #else ios_base::sync_with_stdio(0); cin.tie(0); #endif for (int i = 0; i < N; ++i) inv[i] = fp(i, MOD - 2); int n; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; int ans = 0; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { int len = a[j] - a[j - 1]; ans += len * inv[j - i]; ans %= MOD; } } for (int i = 1; i <= n - 1; ++i) ans = ans * i % MOD; cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; #define int long long #define ii pair<int, int> #define app push_back #define all(a) a.begin(), a.end() #define bp __builtin_popcount #define ll long long #define mp make_pair #define f first #define s second #define Time (double)clock() / CLOCKS_PER_SEC const int MOD = 1000 * 1000 * 1000 + 7; int mod(int n) { n %= MOD; if (n < 0) return n + MOD; else return n; } int fp(int a, int p) { int ans = 1, c = a; for (int i = 0; (1ll << i) <= p; ++i) { if ((p >> i) & 1) ans = mod(ans * c); c = mod(c * c); } return ans; } int dv(int a, int b) { return mod(a * fp(b, MOD - 2)); } const int N = 1e5 + 7; int a[N]; int inv[N]; signed main() { #ifdef HOME freopen("input.txt", "r", stdin); #else ios_base::sync_with_stdio(0); cin.tie(0); #endif for (int i = 0; i < N; ++i) inv[i] = fp(i, MOD - 2); int n; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; int ans = 0, cur = 0; for (int i = 1; i < n; ++i) { cur += inv[i]; cur %= MOD; int len = a[i] - a[i - 1]; ans += len * cur; ans %= MOD; } for (int i = 1; i <= n - 1; ++i) ans = ans * i % MOD; cout << ans << '\n'; }
replace
49
56
49
56
TLE
p02807
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T, class U> using P = pair<T, U>; template <class T> using vec = vector<T>; template <class T> using vvec = vector<vec<T>>; constexpr ll mod = 1e9 + 7; struct mint { ll x; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; class combination { private: vector<mint> fact, inv, finv; public: combination(int N) { fact = inv = finv = vector<mint>(N + 1); fact[0] = fact[1] = 1; inv[0] = inv[1] = 1; finv[0] = finv[1] = 1; for (ll i = 2; i <= N; i++) { fact[i] = fact[i - 1] * i; inv[i] = (mint)mod - inv[mod % i] * (mod / i); finv[i] = finv[i - 1] * inv[i]; } } mint f(int i) { return fact[i]; } mint comb(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fact[n] * finv[k] * finv[n - k]; } mint hcomb(int n, int k) { if (n == 0 && k == 0) return 1; return comb(n + k - 1, k); } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; assert(N <= 2000); vec<ll> X(N); combination C(N); for (int i = 0; i < N; i++) cin >> X[i]; mint ans = 0; vec<mint> S(N + 1, 0); for (int i = 1; i < N; i++) { mint a = i; S[i] = S[i - 1] + a.inv(); ans += S[i] * (X[i] - X[i - 1]); } ans *= C.f(N - 1); cout << ans.x << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T, class U> using P = pair<T, U>; template <class T> using vec = vector<T>; template <class T> using vvec = vector<vec<T>>; constexpr ll mod = 1e9 + 7; struct mint { ll x; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; class combination { private: vector<mint> fact, inv, finv; public: combination(int N) { fact = inv = finv = vector<mint>(N + 1); fact[0] = fact[1] = 1; inv[0] = inv[1] = 1; finv[0] = finv[1] = 1; for (ll i = 2; i <= N; i++) { fact[i] = fact[i - 1] * i; inv[i] = (mint)mod - inv[mod % i] * (mod / i); finv[i] = finv[i - 1] * inv[i]; } } mint f(int i) { return fact[i]; } mint comb(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fact[n] * finv[k] * finv[n - k]; } mint hcomb(int n, int k) { if (n == 0 && k == 0) return 1; return comb(n + k - 1, k); } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vec<ll> X(N); combination C(N); for (int i = 0; i < N; i++) cin >> X[i]; mint ans = 0; vec<mint> S(N + 1, 0); for (int i = 1; i < N; i++) { mint a = i; S[i] = S[i - 1] + a.inv(); ans += S[i] * (X[i] - X[i - 1]); } ans *= C.f(N - 1); cout << ans.x << endl; }
delete
91
92
91
91
0
p02807
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <vector> using namespace std; typedef long long int ll; typedef uint_fast64_t u64; #define bit(n, k) ((n >> k) & 1) /*nのk bit目*/ const int MOD = 1000000007; struct ModInt { u64 a; ModInt() : a(0) {} ModInt(u64 x) : a(x % MOD) {} ModInt operator+=(const ModInt p) { a += p.a; if (a >= MOD) a -= MOD; return *this; } ModInt operator-=(const ModInt p) { a -= p.a; if (a < 0) a += MOD; return *this; } ModInt operator*=(const ModInt p) { a = (a * p.a) % MOD; return *this; } ModInt operator+(const ModInt p) { ModInt x(a); return x += p; } ModInt operator-(const ModInt p) { ModInt x(a); return x -= p; } ModInt operator*(const ModInt p) { ModInt x(a); return x *= p; } ModInt operator=(ll p) { a = p; return *this; } bool operator==(const ModInt p) { return a == p.a; } bool operator!=(const ModInt p) { return a != p.a; } ModInt pow(u64 N) { ModInt ans(1), p(a); while (N > 0) { if (bit(N, 0)) { ans *= p; } p *= p; N >>= 1; } return ans; } ModInt inverse() { return pow(MOD - 2); } }; signed main(void) { int N; cin >> N; ModInt t(1); ModInt i(1); for (; i.a <= N - 1; i.a++) { t *= i; } ModInt S[11000], x[11000]; for (i.a = 1; i.a <= N - 1; i.a++) { S[i.a] += S[i.a - 1] + t * i.inverse(); } for (int a = 1; a <= N; a++) cin >> x[a].a; ModInt ans(0); for (int a = 1; a <= N - 1; a++) { ans += S[a] * (x[a + 1] - x[a]); } cout << ans.a << endl; }
#include <algorithm> #include <bitset> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <vector> using namespace std; typedef long long int ll; typedef uint_fast64_t u64; #define bit(n, k) ((n >> k) & 1) /*nのk bit目*/ const int MOD = 1000000007; struct ModInt { u64 a; ModInt() : a(0) {} ModInt(u64 x) : a(x % MOD) {} ModInt operator+=(const ModInt p) { a += p.a; if (a >= MOD) a -= MOD; return *this; } ModInt operator-=(const ModInt p) { a -= p.a; if (a < 0) a += MOD; return *this; } ModInt operator*=(const ModInt p) { a = (a * p.a) % MOD; return *this; } ModInt operator+(const ModInt p) { ModInt x(a); return x += p; } ModInt operator-(const ModInt p) { ModInt x(a); return x -= p; } ModInt operator*(const ModInt p) { ModInt x(a); return x *= p; } ModInt operator=(ll p) { a = p; return *this; } bool operator==(const ModInt p) { return a == p.a; } bool operator!=(const ModInt p) { return a != p.a; } ModInt pow(u64 N) { ModInt ans(1), p(a); while (N > 0) { if (bit(N, 0)) { ans *= p; } p *= p; N >>= 1; } return ans; } ModInt inverse() { return pow(MOD - 2); } }; signed main(void) { int N; cin >> N; ModInt t(1); ModInt i(1); for (; i.a <= N - 1; i.a++) { t *= i; } ModInt S[110000], x[110000]; for (i.a = 1; i.a <= N - 1; i.a++) { S[i.a] += S[i.a - 1] + t * i.inverse(); } for (int a = 1; a <= N; a++) cin >> x[a].a; ModInt ans(0); for (int a = 1; a <= N - 1; a++) { ans += S[a] * (x[a + 1] - x[a]); } cout << ans.a << endl; }
replace
82
83
82
83
0
p02807
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #include "bits/stdc++.h" #define rep(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, m, n) for (int i = (m); i < (n); ++i) #define rrep(i, n) for (int i = (n)-1; i >= 0; --i) #define rfor(i, m, n) for (int i = (m); i >= (n); --i) #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define mp make_pair #define pb push_back #define eb emplace_back using namespace std; using LL = long long; using VB = vector<bool>; using VVB = vector<VB>; using VI = vector<int>; using VVI = vector<VI>; using VL = vector<LL>; using VVL = vector<VL>; using VS = vector<string>; using VD = vector<double>; using PII = pair<int, int>; using VP = vector<PII>; using PLL = pair<LL, LL>; using VPL = vector<PLL>; template <class T> using pq = priority_queue<T>; template <class T> using pqs = priority_queue<T, vector<T>, greater<T>>; const int inf = (int)1e9; const LL inf_ll = (LL)1e18, MOD = 1000000007; const double PI = acos(-1.0), EPS = 1e-12; template <class T> inline void Sort(T &a) noexcept { sort(all(a)); } template <class T> inline void RSort(T &a) noexcept { sort(rall(a)); } template <class T> inline void Reverse(T &a) noexcept { reverse(all(a)); } template <class T> inline void Unique(T &a) noexcept { a.erase(unique(all(a)), a.end()); } template <class T> inline T Sorted(T a) noexcept { Sort(a); return a; } template <class T> inline T RSorted(T a) noexcept { RSort(a); return a; } template <class T> inline T Reversed(T a) noexcept { Reverse(a); return a; } template <class T> inline T Uniqued(T a) noexcept { Unique(a); return a; } template <class T> inline auto Max(const T &a) noexcept { return *max_element(all(a)); } template <class T> inline auto Min(const T &a) noexcept { return *min_element(all(a)); } template <class T> inline int MaxPos(const T &a) noexcept { return max_element(all(a)) - a.begin(); } template <class T> inline int MinPos(const T &a) noexcept { return min_element(all(a)) - a.begin(); } template <class T, class U> inline int Count(const T &a, const U &v) noexcept { return count(all(a), v); } template <class T, class U> inline int Find(const T &a, const U &v) noexcept { auto pos = find(all(a), v); return pos == a.end() ? -1 : pos - a.begin(); } template <class T, class U> inline U Sum(const T &a, const U &v) noexcept { return accumulate(all(a), v); } template <class T, class U> inline int Lower(const T &a, const U &v) noexcept { return lower_bound(all(a), v) - a.begin(); } template <class T, class U> inline int Upper(const T &a, const U &v) noexcept { return upper_bound(all(a), v) - a.begin(); } template <class T, class P> inline void RemoveIf(T &a, P f) noexcept { a.erase(remove_if(all(a), f), a.end()); } template <class T> inline T Age(T n, T m) noexcept { return (n + m - 1) / m; } template <class T> inline T Age2(T n, T m) noexcept { return Age(n, m) * m; } template <class T> inline T Tri(T n) noexcept { return (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1); } template <class T = long long> inline T BIT(int b) noexcept { return T{1} << b; } template <class T> inline T Gcd(T n, T m) noexcept { return m ? Gcd(m, n % m) : n; } template <class T> inline T Lcm(T n, T m) noexcept { return n / Gcd(n, m) * m; } template <class T> inline T Pow(T a, T n) noexcept { T r = 1; while (n > 0) { if (n & 1) r *= a; a *= a; n /= 2; } return r; } template <class T> inline T Powmod(T a, T n, T m = MOD) noexcept { T r = 1; while (n > 0) { if (n & 1) r = r * a % m, n--; else a = a * a % m, n /= 2; } return r; } template <class T> inline bool chmax(T &a, const T &b) noexcept { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, const T &b) noexcept { if (a > b) { a = b; return true; } return false; } inline string operator*(string s, int n) noexcept { string ret; rep(i, n) ret += s; return ret; } // --- input --- // #if defined(_MSC_VER) || defined(ONLINE_JUDGE) #define getchar_unlocked _getchar_nolock #define putchar_unlocked _putchar_nolock #endif inline int gc() noexcept { return getchar_unlocked(); } template <class T> inline void InputF(T &v) noexcept { cin >> v; } inline void InputF(char &v) noexcept { while (isspace(v = gc())) ; } inline void InputF(string &v) noexcept { char c; for (InputF(c); !isspace(c); c = gc()) v += c; } inline void InputF(int &v) noexcept { bool neg = false; v = 0; char c; InputF(c); if (c == '-') { neg = true; c = gc(); } for (; isdigit(c); c = gc()) v = v * 10 + (c - '0'); if (neg) v = -v; } inline void InputF(long long &v) noexcept { bool neg = false; v = 0; char c; InputF(c); if (c == '-') { neg = true; c = gc(); } for (; isdigit(c); c = gc()) v = v * 10 + (c - '0'); if (neg) v = -v; } inline void InputF(double &v) noexcept { double dp = 1; bool neg = false, adp = false; v = 0; char c; InputF(c); if (c == '-') { neg = true; c = gc(); } for (; isdigit(c) || c == '.'; c = gc()) { if (c == '.') adp = true; else if (adp) v += (c - '0') * (dp *= 0.1); else v = v * 10 + (c - '0'); } if (neg) v = -v; } template <class T, class U> inline void InputF(pair<T, U> &v) noexcept { InputF(v.first); InputF(v.second); } template <class T> inline void InputF(vector<T> &v) noexcept { for (auto &e : v) InputF(e); } struct InputV { int n, m; InputV(int N) : n(N), m(0) {} InputV(pair<int, int> N) : n(N.first), m(N.second) {} template <class T> operator vector<T>() noexcept { vector<T> v(n); InputF(v); return v; } template <class T> operator vector<vector<T>>() noexcept { vector<vector<T>> v(n, vector<T>(m)); InputF(v); return v; } }; struct Input { template <class T> operator T() noexcept { T v; InputF(v); return v; } int operator--(int) { int v; InputF(v); v--; return v; } InputV operator[](int n) noexcept { return InputV(n); } InputV operator[](pair<int, int> n) noexcept { return InputV(n); } template <class T, size_t W> array<vector<T>, W> get(int H) { array<vector<T>, W> ret; rep(i, H) rep(j, W) { T x = *this; ret[j].push_back(x); } return ret; } } in; // --- output --- // struct BoolStr { const char *t, *f; BoolStr(const char *_t, const char *_f) : t(_t), f(_f) {} } Yes("Yes", "No"), yes("yes", "no"), YES("YES", "NO"), Int("1", "0"); struct DivStr { const char *d, *l; DivStr(const char *_d, const char *_l) : d(_d), l(_l) {} } spc(" ", "\n"), no_spc("", "\n"), end_line("\n", "\n"), comma(",", "\n"), no_endl(" ", ""); class Output { BoolStr B{Yes}; DivStr D{spc}; bool isPrint = true; void p(double v) { printf("%.20f", v); } void p(long double v) { printf("%.20Lf", v); } void p(int v) { printf("%d", v); } void p(LL v) { printf("%lld", v); } void p(char v) { putchar(v); } void p(bool v) { printf("%s", v ? B.t : B.f); } template <class T> void p(const T &v) { cout << v; } template <class T, class U> void p(const pair<T, U> &v) { p(v.first); printf("%s", D.d); p(v.second); } template <class T> void p(const vector<T> &v) { rep(i, sz(v)) { if (i) printf("%s", D.d); p(v[i]); } } template <class T> void p(const vector<vector<T>> &v) { rep(i, sz(v)) { if (i) printf("%s", D.l); p(v[i]); } } void p(const BoolStr &v) { B = v; isPrint = false; } void p(const DivStr &v) { D = v; isPrint = false; } public: void operator()() { printf("%s", D.l); } template <class H> void operator()(H &&h) { p(h); if (isPrint) printf("%s", D.l); isPrint = true; B = Yes; D = spc; } template <class H, class... T> void operator()(H &&h, T &&...t) { p(h); if (isPrint) printf("%s", D.d); isPrint = true; operator()(forward<T>(t)...); } template <class... T> void exit(T &&...t) { operator()(forward<T>(t)...); std::exit(EXIT_SUCCESS); } } out; // --- dump --- // #if __has_include("dump.hpp") #include "dump.hpp" #else #define dump(...) (void(0)) #endif // ---------------------------------------------------------------- // template <int MOD> struct modint { using T = long long; T n; constexpr modint(const T x = 0) noexcept : n(x % MOD) { if (n < 0) n += MOD; } constexpr int get_mod() const noexcept { return MOD; } constexpr modint operator+() const noexcept { return *this; } constexpr modint operator-() const noexcept { return n ? MOD - n : 0; } constexpr modint &operator++() noexcept { if (MOD <= ++n) n = 0; return *this; } constexpr modint &operator--() noexcept { if (n <= 0) n = MOD; n--; return *this; } constexpr modint &operator++(int) noexcept { modint t = *this; ++*this; return t; } constexpr modint &operator--(int) noexcept { modint t = *this; --*this; return t; } constexpr modint next() const noexcept { modint t = *this; ++t; return t; } constexpr modint pred() const noexcept { modint t = *this; --t; return t; } constexpr modint operator+(const modint &m) const noexcept { return modint(*this) += m; } constexpr modint operator-(const modint &m) const noexcept { return modint(*this) -= m; } constexpr modint operator*(const modint &m) const noexcept { return modint(*this) *= m; } constexpr modint operator/(const modint &m) const noexcept { return modint(*this) /= m; } constexpr modint &operator+=(const modint &m) noexcept { n += m.n; if (n >= MOD) n -= MOD; return *this; } constexpr modint &operator-=(const modint &m) noexcept { n -= m.n; if (n < 0) n += MOD; return *this; } constexpr modint &operator*=(const modint &m) noexcept { n = n * m.n % MOD; return *this; } constexpr modint &operator/=(const modint &m) noexcept { assert(m.n); T a = m.n, b = MOD, u = 1, v = 0; while (b) { T t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } n = n * u % MOD; if (n < 0) n += MOD; return *this; } constexpr bool operator==(const modint &m) const noexcept { return n == m.n; } constexpr bool operator!=(const modint &m) const noexcept { return n != m.n; } constexpr modint pow(modint m) const noexcept { modint t = n, res = 1; while (m.n > 0) { if (m.n & 1) res *= t; t *= t; m.n >>= 1; } return res; } constexpr modint operator^(modint m) const noexcept { return pow(m); } }; using mint = modint<1000000007>; using VM = vector<mint>; ostream &operator<<(ostream &os, const modint<MOD> &m) noexcept { return os << m.n; } istream &operator>>(istream &is, modint<MOD> &m) noexcept { return is >> m.n; } mint operator""_m(unsigned long long n) { return n; } /* ..|m = { ..| [0] { empty } ..| [1] { 1 } ..| [2] { 2, 3 } ..| [3] { 6, 9, 11 } ..| [4] { 24, 36, 44, 50 } ..| [5] { 120, 180, 220, 250, 274 } ..| [6] { 720, 1080, 1320, 1500, 1644, 1764 } ..| [7] { 5040, 7560, 9240, 10500, 11508, 12348, 13068 } */ int main() { int n = in; VM a = in[n]; mint p = 1; vector<VM> m(n); rep(i, n) m[i].resize(i); m[1][0] = 1; VM x(n), y(n); x[1] = 1; y[0] = 1; y[1] = 1; FOR(i, 2, n) { x[i] = x[i - 1] * i + p; y[i] = y[i - 1] * i; p *= i; } dump(x, y); mint ans = 0; rep(i, n - 1) { ans += (a[i + 1] - a[i]) * x[i + 1] * (y[n - 1] / y[i + 1]); } out(ans); }
#define _CRT_SECURE_NO_WARNINGS #include "bits/stdc++.h" #define rep(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, m, n) for (int i = (m); i < (n); ++i) #define rrep(i, n) for (int i = (n)-1; i >= 0; --i) #define rfor(i, m, n) for (int i = (m); i >= (n); --i) #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define mp make_pair #define pb push_back #define eb emplace_back using namespace std; using LL = long long; using VB = vector<bool>; using VVB = vector<VB>; using VI = vector<int>; using VVI = vector<VI>; using VL = vector<LL>; using VVL = vector<VL>; using VS = vector<string>; using VD = vector<double>; using PII = pair<int, int>; using VP = vector<PII>; using PLL = pair<LL, LL>; using VPL = vector<PLL>; template <class T> using pq = priority_queue<T>; template <class T> using pqs = priority_queue<T, vector<T>, greater<T>>; const int inf = (int)1e9; const LL inf_ll = (LL)1e18, MOD = 1000000007; const double PI = acos(-1.0), EPS = 1e-12; template <class T> inline void Sort(T &a) noexcept { sort(all(a)); } template <class T> inline void RSort(T &a) noexcept { sort(rall(a)); } template <class T> inline void Reverse(T &a) noexcept { reverse(all(a)); } template <class T> inline void Unique(T &a) noexcept { a.erase(unique(all(a)), a.end()); } template <class T> inline T Sorted(T a) noexcept { Sort(a); return a; } template <class T> inline T RSorted(T a) noexcept { RSort(a); return a; } template <class T> inline T Reversed(T a) noexcept { Reverse(a); return a; } template <class T> inline T Uniqued(T a) noexcept { Unique(a); return a; } template <class T> inline auto Max(const T &a) noexcept { return *max_element(all(a)); } template <class T> inline auto Min(const T &a) noexcept { return *min_element(all(a)); } template <class T> inline int MaxPos(const T &a) noexcept { return max_element(all(a)) - a.begin(); } template <class T> inline int MinPos(const T &a) noexcept { return min_element(all(a)) - a.begin(); } template <class T, class U> inline int Count(const T &a, const U &v) noexcept { return count(all(a), v); } template <class T, class U> inline int Find(const T &a, const U &v) noexcept { auto pos = find(all(a), v); return pos == a.end() ? -1 : pos - a.begin(); } template <class T, class U> inline U Sum(const T &a, const U &v) noexcept { return accumulate(all(a), v); } template <class T, class U> inline int Lower(const T &a, const U &v) noexcept { return lower_bound(all(a), v) - a.begin(); } template <class T, class U> inline int Upper(const T &a, const U &v) noexcept { return upper_bound(all(a), v) - a.begin(); } template <class T, class P> inline void RemoveIf(T &a, P f) noexcept { a.erase(remove_if(all(a), f), a.end()); } template <class T> inline T Age(T n, T m) noexcept { return (n + m - 1) / m; } template <class T> inline T Age2(T n, T m) noexcept { return Age(n, m) * m; } template <class T> inline T Tri(T n) noexcept { return (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1); } template <class T = long long> inline T BIT(int b) noexcept { return T{1} << b; } template <class T> inline T Gcd(T n, T m) noexcept { return m ? Gcd(m, n % m) : n; } template <class T> inline T Lcm(T n, T m) noexcept { return n / Gcd(n, m) * m; } template <class T> inline T Pow(T a, T n) noexcept { T r = 1; while (n > 0) { if (n & 1) r *= a; a *= a; n /= 2; } return r; } template <class T> inline T Powmod(T a, T n, T m = MOD) noexcept { T r = 1; while (n > 0) { if (n & 1) r = r * a % m, n--; else a = a * a % m, n /= 2; } return r; } template <class T> inline bool chmax(T &a, const T &b) noexcept { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, const T &b) noexcept { if (a > b) { a = b; return true; } return false; } inline string operator*(string s, int n) noexcept { string ret; rep(i, n) ret += s; return ret; } // --- input --- // #if defined(_MSC_VER) || defined(ONLINE_JUDGE) #define getchar_unlocked _getchar_nolock #define putchar_unlocked _putchar_nolock #endif inline int gc() noexcept { return getchar_unlocked(); } template <class T> inline void InputF(T &v) noexcept { cin >> v; } inline void InputF(char &v) noexcept { while (isspace(v = gc())) ; } inline void InputF(string &v) noexcept { char c; for (InputF(c); !isspace(c); c = gc()) v += c; } inline void InputF(int &v) noexcept { bool neg = false; v = 0; char c; InputF(c); if (c == '-') { neg = true; c = gc(); } for (; isdigit(c); c = gc()) v = v * 10 + (c - '0'); if (neg) v = -v; } inline void InputF(long long &v) noexcept { bool neg = false; v = 0; char c; InputF(c); if (c == '-') { neg = true; c = gc(); } for (; isdigit(c); c = gc()) v = v * 10 + (c - '0'); if (neg) v = -v; } inline void InputF(double &v) noexcept { double dp = 1; bool neg = false, adp = false; v = 0; char c; InputF(c); if (c == '-') { neg = true; c = gc(); } for (; isdigit(c) || c == '.'; c = gc()) { if (c == '.') adp = true; else if (adp) v += (c - '0') * (dp *= 0.1); else v = v * 10 + (c - '0'); } if (neg) v = -v; } template <class T, class U> inline void InputF(pair<T, U> &v) noexcept { InputF(v.first); InputF(v.second); } template <class T> inline void InputF(vector<T> &v) noexcept { for (auto &e : v) InputF(e); } struct InputV { int n, m; InputV(int N) : n(N), m(0) {} InputV(pair<int, int> N) : n(N.first), m(N.second) {} template <class T> operator vector<T>() noexcept { vector<T> v(n); InputF(v); return v; } template <class T> operator vector<vector<T>>() noexcept { vector<vector<T>> v(n, vector<T>(m)); InputF(v); return v; } }; struct Input { template <class T> operator T() noexcept { T v; InputF(v); return v; } int operator--(int) { int v; InputF(v); v--; return v; } InputV operator[](int n) noexcept { return InputV(n); } InputV operator[](pair<int, int> n) noexcept { return InputV(n); } template <class T, size_t W> array<vector<T>, W> get(int H) { array<vector<T>, W> ret; rep(i, H) rep(j, W) { T x = *this; ret[j].push_back(x); } return ret; } } in; // --- output --- // struct BoolStr { const char *t, *f; BoolStr(const char *_t, const char *_f) : t(_t), f(_f) {} } Yes("Yes", "No"), yes("yes", "no"), YES("YES", "NO"), Int("1", "0"); struct DivStr { const char *d, *l; DivStr(const char *_d, const char *_l) : d(_d), l(_l) {} } spc(" ", "\n"), no_spc("", "\n"), end_line("\n", "\n"), comma(",", "\n"), no_endl(" ", ""); class Output { BoolStr B{Yes}; DivStr D{spc}; bool isPrint = true; void p(double v) { printf("%.20f", v); } void p(long double v) { printf("%.20Lf", v); } void p(int v) { printf("%d", v); } void p(LL v) { printf("%lld", v); } void p(char v) { putchar(v); } void p(bool v) { printf("%s", v ? B.t : B.f); } template <class T> void p(const T &v) { cout << v; } template <class T, class U> void p(const pair<T, U> &v) { p(v.first); printf("%s", D.d); p(v.second); } template <class T> void p(const vector<T> &v) { rep(i, sz(v)) { if (i) printf("%s", D.d); p(v[i]); } } template <class T> void p(const vector<vector<T>> &v) { rep(i, sz(v)) { if (i) printf("%s", D.l); p(v[i]); } } void p(const BoolStr &v) { B = v; isPrint = false; } void p(const DivStr &v) { D = v; isPrint = false; } public: void operator()() { printf("%s", D.l); } template <class H> void operator()(H &&h) { p(h); if (isPrint) printf("%s", D.l); isPrint = true; B = Yes; D = spc; } template <class H, class... T> void operator()(H &&h, T &&...t) { p(h); if (isPrint) printf("%s", D.d); isPrint = true; operator()(forward<T>(t)...); } template <class... T> void exit(T &&...t) { operator()(forward<T>(t)...); std::exit(EXIT_SUCCESS); } } out; // --- dump --- // #if __has_include("dump.hpp") #include "dump.hpp" #else #define dump(...) (void(0)) #endif // ---------------------------------------------------------------- // template <int MOD> struct modint { using T = long long; T n; constexpr modint(const T x = 0) noexcept : n(x % MOD) { if (n < 0) n += MOD; } constexpr int get_mod() const noexcept { return MOD; } constexpr modint operator+() const noexcept { return *this; } constexpr modint operator-() const noexcept { return n ? MOD - n : 0; } constexpr modint &operator++() noexcept { if (MOD <= ++n) n = 0; return *this; } constexpr modint &operator--() noexcept { if (n <= 0) n = MOD; n--; return *this; } constexpr modint &operator++(int) noexcept { modint t = *this; ++*this; return t; } constexpr modint &operator--(int) noexcept { modint t = *this; --*this; return t; } constexpr modint next() const noexcept { modint t = *this; ++t; return t; } constexpr modint pred() const noexcept { modint t = *this; --t; return t; } constexpr modint operator+(const modint &m) const noexcept { return modint(*this) += m; } constexpr modint operator-(const modint &m) const noexcept { return modint(*this) -= m; } constexpr modint operator*(const modint &m) const noexcept { return modint(*this) *= m; } constexpr modint operator/(const modint &m) const noexcept { return modint(*this) /= m; } constexpr modint &operator+=(const modint &m) noexcept { n += m.n; if (n >= MOD) n -= MOD; return *this; } constexpr modint &operator-=(const modint &m) noexcept { n -= m.n; if (n < 0) n += MOD; return *this; } constexpr modint &operator*=(const modint &m) noexcept { n = n * m.n % MOD; return *this; } constexpr modint &operator/=(const modint &m) noexcept { assert(m.n); T a = m.n, b = MOD, u = 1, v = 0; while (b) { T t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } n = n * u % MOD; if (n < 0) n += MOD; return *this; } constexpr bool operator==(const modint &m) const noexcept { return n == m.n; } constexpr bool operator!=(const modint &m) const noexcept { return n != m.n; } constexpr modint pow(modint m) const noexcept { modint t = n, res = 1; while (m.n > 0) { if (m.n & 1) res *= t; t *= t; m.n >>= 1; } return res; } constexpr modint operator^(modint m) const noexcept { return pow(m); } }; using mint = modint<1000000007>; using VM = vector<mint>; ostream &operator<<(ostream &os, const modint<MOD> &m) noexcept { return os << m.n; } istream &operator>>(istream &is, modint<MOD> &m) noexcept { return is >> m.n; } mint operator""_m(unsigned long long n) { return n; } /* ..|m = { ..| [0] { empty } ..| [1] { 1 } ..| [2] { 2, 3 } ..| [3] { 6, 9, 11 } ..| [4] { 24, 36, 44, 50 } ..| [5] { 120, 180, 220, 250, 274 } ..| [6] { 720, 1080, 1320, 1500, 1644, 1764 } ..| [7] { 5040, 7560, 9240, 10500, 11508, 12348, 13068 } */ int main() { int n = in; VM a = in[n]; mint p = 1; VM x(n), y(n); x[1] = 1; y[0] = 1; y[1] = 1; FOR(i, 2, n) { x[i] = x[i - 1] * i + p; y[i] = y[i - 1] * i; p *= i; } dump(x, y); mint ans = 0; rep(i, n - 1) { ans += (a[i + 1] - a[i]) * x[i + 1] * (y[n - 1] / y[i + 1]); } out(ans); }
delete
455
459
455
455
0
p02807
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long const int p = (1e9 + 7); int po(int a, int b) { if (b == 0) { return 1; } if (b == 1) { return a; } if (b % 2 == 0) { int u = po(a, b / 2); return (u * u) % p; } int u = po(a, b - 1); return (a * u) % p; } int inv(int x) { return po(x, p - 2); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } int ans = 0; for (int i = (n - 1); i >= 0; i--) { for (int j = (i + 1); j < n; ++j) { if (j != (n - 1)) ans += (inv((j - i) * (j - i + 1)) * (a[j] - a[i])); else ans += inv(j - i) * (a[j] - a[i]); ans %= p; } } for (int i = 1; i <= (n - 1); ++i) { ans *= i; ans %= p; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long const int p = (1e9 + 7); int po(int a, int b) { if (b == 0) { return 1; } if (b == 1) { return a; } if (b % 2 == 0) { int u = po(a, b / 2); return (u * u) % p; } int u = po(a, b - 1); return (a * u) % p; } int inv(int x) { return po(x, p - 2); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } int ans = 0; int d[n + 2]; d[0] = 0; for (int i = 1; i <= n; ++i) { d[i] = d[i - 1] + inv(i); d[i] %= p; } for (int i = 0; i < (n - 1); ++i) { ans += (a[i + 1] - a[i]) * d[i + 1]; ans %= p; } for (int i = 1; i <= (n - 1); ++i) { ans *= i; ans %= p; } cout << ans; return 0; }
replace
31
39
31
40
TLE
p02807
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define ff first #define ss second // #define endl "\n" #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x.size()) #define PI 3.1415926535897932384626433832795 int MOD = 1e9 + 7; int powmod(int a, int l, int md) { int res = 1; while (l) { if (l & 1) res = res * a % md; l /= 2; a = a * a % md; } return res; } int binpow(int a, int l) { int res = 1; while (l) { if (l & 1) res = res * a; l /= 2; a = a * a; } return res; } typedef long long ll; typedef vector<ll> vi; typedef pair<ll, ll> ii; typedef vector<ii> vii; #define pb push_back int __set(int b, int i) { return b | (1 << i); } // set ith bit int __unset(int b, int i) { return b & (~(1UL << i)); } int __check(int b, int i) { return b & (1 << i); } // returns 0 if ith bit is 0 int mulmod(int a, int b, int md) { return ((a % md) * (b % md)) % md; } int addmod(int a, int b, int md) { return (a + b) % md; } int submod(int a, int b, int md) { return (((a - b) % md) + md) % md; } int divmod(int a, int b, int md) { return mulmod(a, powmod(b, md - 2, md), md); } // if md is prime; const ll inf = 0xFFFFFFFFFFFFFFFL; // very large number priority_queue<int, vector<int>, greater<int>> pq; // for min priority_queue #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); clock_t time_p = clock(); void time() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int getRand(int l, int r) { uniform_int_distribution<int> uid(l, r); return uid(rng); } int md = 1e9 + 7; void manacher(vi &d1, vi &d2, int n, string &s) { for (int i = 0, l = 0, r = -1; i < n; i++) { int k = (i > r) ? 1 : min(d1[l + r - i], r - i + 1); while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) k++; d1[i] = k--; if (i + k > r) { l = i - k; r = i + k; } } for (int i = 0, l = 0, r = -1; i < n; i++) { int k = (i > r) ? 0 : min(d2[l + r - i + 1], r - i + 1); while (0 <= i - k - 1 && i + k < n && s[i - k - 1] == s[i + k]) k++; d2[i] = k--; if (i + k > r) { l = i - k - 1; r = i + k; } } } #define arr (int)(4e5 + 10) int f[arr]; int rf[arr]; int C(int l, int k, int md) { if (k < 0 || k > l) { return 0; } return f[l] * rf[k] % md * rf[l - k] % md; } void precfac() { f[0] = 1; for (int i = 1; i < arr; i++) { f[i] = f[i - 1] * i % MOD; } rf[arr - 1] = powmod(f[arr - 1], MOD - 2, MOD); for (int i = arr - 2; i >= 0; i--) { rf[i] = rf[i + 1] * (i + 1) % MOD; } } signed main(void) { IOS; precfac(); int n; cin >> n; vi x(n); int ans = 0; for (int i = 0; i < n; i++) cin >> x[i]; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (j == n - 1) { // int tp = // ((((x[j]-x[i])*(C(n-1,j-i,md)*(f[j-i-1]))%MOD)%MOD)*f[n-1-(j-i)])%MOD; int tp = ((((x[j] - x[i]) * C(n - 1, j - i, md)) % MOD) * ((f[j - i - 1] * f[n - (j - i) - 1]) % MOD)) % MOD; ans = (ans + tp) % MOD; } else { int tp = ((((x[j] - x[i]) * C(n - 1, j - i + 1, md)) % MOD) * ((f[j - i - 1] * f[n - (j - i + 1) - 1]) % MOD)) % MOD; // int tp = // ((((x[j]-x[i])*(C(n-1,j-i+1,md)*(f[j-i-1]))%MOD)%MOD)*f[n-1-(j-i+1)])%MOD; ans = (ans + tp) % MOD; } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define ff first #define ss second // #define endl "\n" #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x.size()) #define PI 3.1415926535897932384626433832795 int MOD = 1e9 + 7; int powmod(int a, int l, int md) { int res = 1; while (l) { if (l & 1) res = res * a % md; l /= 2; a = a * a % md; } return res; } int binpow(int a, int l) { int res = 1; while (l) { if (l & 1) res = res * a; l /= 2; a = a * a; } return res; } typedef long long ll; typedef vector<ll> vi; typedef pair<ll, ll> ii; typedef vector<ii> vii; #define pb push_back int __set(int b, int i) { return b | (1 << i); } // set ith bit int __unset(int b, int i) { return b & (~(1UL << i)); } int __check(int b, int i) { return b & (1 << i); } // returns 0 if ith bit is 0 int mulmod(int a, int b, int md) { return ((a % md) * (b % md)) % md; } int addmod(int a, int b, int md) { return (a + b) % md; } int submod(int a, int b, int md) { return (((a - b) % md) + md) % md; } int divmod(int a, int b, int md) { return mulmod(a, powmod(b, md - 2, md), md); } // if md is prime; const ll inf = 0xFFFFFFFFFFFFFFFL; // very large number priority_queue<int, vector<int>, greater<int>> pq; // for min priority_queue #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); clock_t time_p = clock(); void time() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int getRand(int l, int r) { uniform_int_distribution<int> uid(l, r); return uid(rng); } int md = 1e9 + 7; void manacher(vi &d1, vi &d2, int n, string &s) { for (int i = 0, l = 0, r = -1; i < n; i++) { int k = (i > r) ? 1 : min(d1[l + r - i], r - i + 1); while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) k++; d1[i] = k--; if (i + k > r) { l = i - k; r = i + k; } } for (int i = 0, l = 0, r = -1; i < n; i++) { int k = (i > r) ? 0 : min(d2[l + r - i + 1], r - i + 1); while (0 <= i - k - 1 && i + k < n && s[i - k - 1] == s[i + k]) k++; d2[i] = k--; if (i + k > r) { l = i - k - 1; r = i + k; } } } #define arr (int)(4e5 + 10) int f[arr]; int rf[arr]; int C(int l, int k, int md) { if (k < 0 || k > l) { return 0; } return f[l] * rf[k] % md * rf[l - k] % md; } void precfac() { f[0] = 1; for (int i = 1; i < arr; i++) { f[i] = f[i - 1] * i % MOD; } rf[arr - 1] = powmod(f[arr - 1], MOD - 2, MOD); for (int i = arr - 2; i >= 0; i--) { rf[i] = rf[i + 1] * (i + 1) % MOD; } } signed main(void) { IOS; precfac(); int n; cin >> n; vi x(n); int ans = 0; for (int i = 0; i < n; i++) cin >> x[i]; for (int i = 0; i < n - 1; i++) { int j = n - 1; int tp = ((((x[j] - x[i]) * C(n - 1, j - i, md)) % MOD) * ((f[j - i - 1] * f[n - (j - i) - 1]) % MOD)) % MOD; ans = (ans + tp) % MOD; } // cout<<ans<<endl; int pre[n - 1]; int suf[n - 1]; suf[n - 2] = x[n - 2]; pre[0] = x[0]; for (int i = 1; i < n - 1; i++) pre[i] = pre[i - 1] + x[i]; for (int i = n - 3; i >= 0; i--) suf[i] = suf[i + 1] + x[i]; for (int p = 1; p < n - 1; p++) { int i = 0, j = p; int p1 = (suf[n - 1 - p] - pre[p - 1]) % MOD; int p2 = (((C(n - 1, j - i + 1, md) * (f[j - i - 1])) % MOD) * f[n - 1 - (j - i + 1)]) % MOD; int tp = (p1 * p2) % MOD; ans = (ans + tp) % MOD; } cout << ans << endl; return 0; }
replace
120
138
120
144
TLE
p02807
C++
Runtime Error
// 高知能系Vtuberの高井茅乃です。 // Twitter: https://twitter.com/takaichino // YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw #include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF INT_MAX #define LLINF LLONG_MAX #define REP(i, n) for (int i = 0; i < n; i++) #define REP1(i, n) for (int i = 1; i <= n; i++) #define MODA 1000000007 template <typename T> std::istream &operator>>(std::istream &is, std::vector<T> &vec) { for (T &x : vec) { is >> x; } return is; } // つねに10億7などの剰余をとる構造体 // 参考: https://www.youtube.com/watch?v=L8grWxBlIZ4&t=9858 // 参考: // https://qiita.com/drken/items/3b4fdf0a78e7a138cd9a#4-%E7%B4%AF%E4%B9%97-an int mod = MODA; struct modint { ll x; modint(ll x) : x(x % mod) {} modint &operator+=(const modint a) { (x += a.x) %= mod; return *this; } modint &operator-=(const modint a) { (x -= a.x) %= mod; if (x < 0) x += mod; return *this; } modint &operator*=(const modint a) { (x *= a.x) %= mod; return *this; } modint & operator/=(modint a) { // 除算のみO(log // mod)なので注意。割る数が小さいならnCrのinvを使うこと ll exp = mod - 2; while (exp > 0) { if (exp & 1) *this *= a.x; a *= a.x; exp >>= 1; } return *this; } modint operator+(const modint a) const { modint res(*this); return res += a; } modint operator-(const modint a) const { modint res(*this); return res -= a; } modint operator*(const modint a) const { modint res(*this); return res *= a; } modint operator/(const modint a) const { modint res(*this); return res /= a; } }; modint modpow(modint x, ll n) { modint res = 1; while (n > 0) { if (n & 1) res = res * x.x; x = x * x; n >>= 1; } return res; } // さらにnCrのために、x!とその逆元を配列で持つ const int NMAX = 500000; // 10^7くらいまではいけそう ll fact[NMAX], finv[NMAX], inv[NMAX]; void ncr_init() { // はじめにinitすること fact[0] = fact[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < NMAX; i++) { fact[i] = fact[i - 1] * i % MODA; inv[i] = MODA - inv[MODA % i] * (MODA / i) % MODA; finv[i] = finv[i - 1] * inv[i] % MODA; } } ll ncr(int n, int r) { if (n < r) return 0; if (n < 0 || r < 0) return 0; return fact[n] * (finv[n - r] * finv[r] % MODA) % MODA; } // ll sigpr[NMAX]; ll sig_init() { sigpr[0] = 0; REP1(i, NMAX - 1) { sigpr[i] = (sigpr[i - 1] + inv[i]) % MODA; } } int main() { modint ans = 0; ll tmp; int n; cin >> n; vector<ll> x(n); cin >> x; ncr_init(); sig_init(); REP(i, n - 1) ans += (x[i + 1] - x[i]) * sigpr[i + 1]; ans *= fact[n - 1]; cout << ans.x << endl; }
// 高知能系Vtuberの高井茅乃です。 // Twitter: https://twitter.com/takaichino // YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw #include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF INT_MAX #define LLINF LLONG_MAX #define REP(i, n) for (int i = 0; i < n; i++) #define REP1(i, n) for (int i = 1; i <= n; i++) #define MODA 1000000007 template <typename T> std::istream &operator>>(std::istream &is, std::vector<T> &vec) { for (T &x : vec) { is >> x; } return is; } // つねに10億7などの剰余をとる構造体 // 参考: https://www.youtube.com/watch?v=L8grWxBlIZ4&t=9858 // 参考: // https://qiita.com/drken/items/3b4fdf0a78e7a138cd9a#4-%E7%B4%AF%E4%B9%97-an int mod = MODA; struct modint { ll x; modint(ll x) : x(x % mod) {} modint &operator+=(const modint a) { (x += a.x) %= mod; return *this; } modint &operator-=(const modint a) { (x -= a.x) %= mod; if (x < 0) x += mod; return *this; } modint &operator*=(const modint a) { (x *= a.x) %= mod; return *this; } modint & operator/=(modint a) { // 除算のみO(log // mod)なので注意。割る数が小さいならnCrのinvを使うこと ll exp = mod - 2; while (exp > 0) { if (exp & 1) *this *= a.x; a *= a.x; exp >>= 1; } return *this; } modint operator+(const modint a) const { modint res(*this); return res += a; } modint operator-(const modint a) const { modint res(*this); return res -= a; } modint operator*(const modint a) const { modint res(*this); return res *= a; } modint operator/(const modint a) const { modint res(*this); return res /= a; } }; modint modpow(modint x, ll n) { modint res = 1; while (n > 0) { if (n & 1) res = res * x.x; x = x * x; n >>= 1; } return res; } // さらにnCrのために、x!とその逆元を配列で持つ const int NMAX = 500000; // 10^7くらいまではいけそう ll fact[NMAX], finv[NMAX], inv[NMAX]; void ncr_init() { // はじめにinitすること fact[0] = fact[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < NMAX; i++) { fact[i] = fact[i - 1] * i % MODA; inv[i] = MODA - inv[MODA % i] * (MODA / i) % MODA; finv[i] = finv[i - 1] * inv[i] % MODA; } } ll ncr(int n, int r) { if (n < r) return 0; if (n < 0 || r < 0) return 0; return fact[n] * (finv[n - r] * finv[r] % MODA) % MODA; } // ll sigpr[NMAX]; void sig_init() { sigpr[0] = sigpr[1] = 1; for (int i = 2; i < NMAX; i++) { sigpr[i] = (sigpr[i - 1] + inv[i]) % MODA; } } int main() { modint ans = 0; ll tmp; int n; cin >> n; vector<ll> x(n); cin >> x; ncr_init(); sig_init(); REP(i, n - 1) ans += (x[i + 1] - x[i]) * sigpr[i + 1]; ans *= fact[n - 1]; cout << ans.x << endl; }
replace
107
110
107
112
0
p02807
C++
Time Limit Exceeded
#define DEBUG 1 #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vll>; using pll = pair<ll, ll>; using vpll = vector<pll>; using vvpll = vector<vpll>; using tll = tuple<ll, ll, ll>; using vtll = vector<tll>; using vvtll = vector<vtll>; #define all(v) (v).begin(), (v).end() #define for1(i, n) for (ll i = 0; i < (n); i++) #define for2(i, m, n) for (ll i = (m); i < (n); i++) #define for3(i, m, n, d) for (ll i = (m); i < (n); i += (d)) #define rfor2(i, m, n) for (ll i = (m); i > (n); i--) #define rfor3(i, m, n, d) for (ll i = (m); i > (n); i += (d)) #define PI 3.1415926535897932384626433832795028841971693993751L #define INF 1111111111111111111LL #define print(...) print_1(__VA_ARGS__) #define in(...) in_1(__VA_ARGS__) #if DEBUG #define dump(...) dump_1(#__VA_ARGS__, __VA_ARGS__) #define dumpa(...) dumpa_1(#__VA_ARGS__, __VA_ARGS__) #else #define dump(...) #define dumpa(...) #endif template <typename Head> void dump_1(const char *str, Head &&h) { cerr << str << ": " << h << '\n'; } template <typename Head, typename... Tail> void dump_1(const char *str, Head &&h, Tail &&...t) { while (*str != ',') { cerr << *str++; } cerr << ": " << h << ' '; dump_1(str + 1, t...); } template <typename T> void dumpa_1(const char *str, const T v[], const ll size) { while (*str != ',') { cerr << *str++; } cerr << ": "; for1(i, size) { if (i != 0) { cerr << ' '; } cerr << v[i]; } cerr << '\n'; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &v) { os << v.first << ' ' << v.second; return os; } template <typename T1, typename T2, typename T3> ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &v) { os << get<0>(v) << ' ' << get<1>(v) << ' ' << get<2>(v); return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &v) { os << '{'; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ", "; } os << it->first << ':' << it->second; } os << '}'; return os; } void Yes(void) { cout << "Yes\n"; } void No(void) { cout << "No\n"; } void YES(void) { cout << "YES\n"; } void NO(void) { cout << "NO\n"; } template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> void vin(vector<T> &v, ll len) { for1(i, len) { cin >> v[i]; } } template <typename Head> void in_1(Head &h) { cin >> h; } template <typename Head, typename... Tail> void in_1(Head &h, Tail &...t) { cin >> h; in_1(t...); } template <typename Head> void print_1(Head &&h) { cout << h << '\n'; } template <typename Head, typename... Tail> void print_1(Head &&h, Tail &&...t) { cout << h << ' '; print_1(t...); } //--------------------------------------------------------- const ll mod = 1000000007LL; // 10**9 + 7 struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } friend ostream &operator<<(ostream &os, const mint &v) { os << v.x; return os; } }; //--------------------------------------------------------- struct mintcomb { vector<mint> fact, ifact; mintcomb(int n) : fact(n + 1), ifact(n + 1) { assert(n < mod); fact[0] = 1; for (int i = 1; i <= n; ++i) { fact[i] = fact[i - 1] * i; } ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) { ifact[i - 1] = ifact[i] * i; } } mint permutation(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[n - k]; } mint combination(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } }; //--------------------------------------------------------- mint dp[2][100005]; void solve() { ll N; in(N); vll X(N); vin(X, N); mint ans = 0; mint two = 2; mint a = two.pow(N - 2); mint r = two.pow(N - 3); vector<mint> A(N + 5); vector<mint> B(N + 5); mintcomb comb(N); ll i0 = 1; ll i1 = 0; dp[i1][1] = 2; dp[i1][2] = 3; for2(i, 3, N) { i0 ^= 1; i1 ^= 1; dp[i1][1] = comb.fact[i]; for2(j, 2, i + 1) { // dump(i0, j - 1, dp[i0][j - 1], (j), dp[i0][j], (i - j), // comb.fact[i - 1]); dp[i1][j] = dp[i0][j - 1] * (j) + dp[i0][j] * (i - j) + comb.fact[i - 1]; } // dump(i); // dumpa(dp[i1], N + 1); } for1(i, N - 1) { ans += dp[i1][i + 1] * (X[i + 1] - X[i]); } // dump(ans); // mint b = comb.permutation(N - 2, N - 2); // ans *= b; print(ans); } //--------------------------------------------------------- int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(16); cerr << fixed << setprecision(16); solve(); }
#define DEBUG 1 #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vll>; using pll = pair<ll, ll>; using vpll = vector<pll>; using vvpll = vector<vpll>; using tll = tuple<ll, ll, ll>; using vtll = vector<tll>; using vvtll = vector<vtll>; #define all(v) (v).begin(), (v).end() #define for1(i, n) for (ll i = 0; i < (n); i++) #define for2(i, m, n) for (ll i = (m); i < (n); i++) #define for3(i, m, n, d) for (ll i = (m); i < (n); i += (d)) #define rfor2(i, m, n) for (ll i = (m); i > (n); i--) #define rfor3(i, m, n, d) for (ll i = (m); i > (n); i += (d)) #define PI 3.1415926535897932384626433832795028841971693993751L #define INF 1111111111111111111LL #define print(...) print_1(__VA_ARGS__) #define in(...) in_1(__VA_ARGS__) #if DEBUG #define dump(...) dump_1(#__VA_ARGS__, __VA_ARGS__) #define dumpa(...) dumpa_1(#__VA_ARGS__, __VA_ARGS__) #else #define dump(...) #define dumpa(...) #endif template <typename Head> void dump_1(const char *str, Head &&h) { cerr << str << ": " << h << '\n'; } template <typename Head, typename... Tail> void dump_1(const char *str, Head &&h, Tail &&...t) { while (*str != ',') { cerr << *str++; } cerr << ": " << h << ' '; dump_1(str + 1, t...); } template <typename T> void dumpa_1(const char *str, const T v[], const ll size) { while (*str != ',') { cerr << *str++; } cerr << ": "; for1(i, size) { if (i != 0) { cerr << ' '; } cerr << v[i]; } cerr << '\n'; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &v) { os << v.first << ' ' << v.second; return os; } template <typename T1, typename T2, typename T3> ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &v) { os << get<0>(v) << ' ' << get<1>(v) << ' ' << get<2>(v); return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &v) { os << '{'; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ", "; } os << it->first << ':' << it->second; } os << '}'; return os; } void Yes(void) { cout << "Yes\n"; } void No(void) { cout << "No\n"; } void YES(void) { cout << "YES\n"; } void NO(void) { cout << "NO\n"; } template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> void vin(vector<T> &v, ll len) { for1(i, len) { cin >> v[i]; } } template <typename Head> void in_1(Head &h) { cin >> h; } template <typename Head, typename... Tail> void in_1(Head &h, Tail &...t) { cin >> h; in_1(t...); } template <typename Head> void print_1(Head &&h) { cout << h << '\n'; } template <typename Head, typename... Tail> void print_1(Head &&h, Tail &&...t) { cout << h << ' '; print_1(t...); } //--------------------------------------------------------- const ll mod = 1000000007LL; // 10**9 + 7 struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } friend ostream &operator<<(ostream &os, const mint &v) { os << v.x; return os; } }; //--------------------------------------------------------- struct mintcomb { vector<mint> fact, ifact; mintcomb(int n) : fact(n + 1), ifact(n + 1) { assert(n < mod); fact[0] = 1; for (int i = 1; i <= n; ++i) { fact[i] = fact[i - 1] * i; } ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) { ifact[i - 1] = ifact[i] * i; } } mint permutation(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[n - k]; } mint combination(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } }; //--------------------------------------------------------- mint dp[2][100005]; void solve() { ll N; in(N); vll X(N); vin(X, N); mint ans = 0; mint two = 2; mint a = two.pow(N - 2); mint r = two.pow(N - 3); vector<mint> A(N + 5); vector<mint> B(N + 5); mintcomb comb(N); ll i0 = 1; ll i1 = 0; dp[i1][1] = 2; dp[i1][2] = 3; /* for2 (i, 3, N) { i0 ^= 1; i1 ^= 1; dp[i1][1] = comb.fact[i]; for2 (j, 2, i + 1) { // dump(i0, j - 1, dp[i0][j - 1], (j), dp[i0][j], (i - j), // comb.fact[i - 1]); dp[i1][j] = dp[i0][j - 1] * (j) + dp[i0][j] * (i - j) + comb.fact[i - 1]; } dump(i); dumpa(dp[i1], N + 1); for1 (i, N) { cout << dp[i1][1] / (dp[i1][i + 1] - dp[i1][i]) << " "; } cout << endl; } */ dp[i1][1] = comb.fact[N - 1]; for2(i, 2, N) { dp[i1][i] = dp[i1][i - 1] + dp[i1][1] / i; } // dumpa(dp[i1], N + 1); for1(i, N - 1) { ans += dp[i1][i + 1] * (X[i + 1] - X[i]); } // dump(ans); // mint b = comb.permutation(N - 2, N - 2); // ans *= b; print(ans); } //--------------------------------------------------------- int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(16); cerr << fixed << setprecision(16); solve(); }
replace
248
260
248
270
TLE
p02807
C++
Runtime Error
#include <cassert> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <algorithm> #include <cmath> #include <functional> #include <bitset> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> #define TEST \ { IS_TEST = true; } #define fi first #define se second #define pb(x) \ { push_back(x); } using namespace std; using ll = int_fast64_t; using v_b = vector<bool>; using v_ll = vector<ll>; using str = string; using v_str = vector<string>; using p_ll = pair<ll, ll>; using vv_b = vector<v_b>; using vv_ll = vector<v_ll>; using vp_ll = vector<p_ll>; using vvv_ll = vector<vv_ll>; using vvp_ll = vector<vp_ll>; using ld = long double; using v_ld = vector<ld>; using vv_ld = vector<v_ld>; bool IS_TEST = false; ll ll_min64 = 1LL << 63; ll ll_max64 = ~ll_min64; ll ll_min32 = 1LL << 31; ll ll_max32 = ~ll_min32; ll MOD = 1000000007; /*displaying functions for debug*/ template <class T> void show2(const T &x) { cout << x; } template <class T1, class T2> void show2(const pair<T1, T2> &x) { cout << "{" << show2(x.first) << "," << show2(x.second) << "}"; } template <class T> void show(const T &x) { if (!IS_TEST) return; show2(x); cout << endl; } template <class T> void v_show(const T &v, ll n = -1) { if (!IS_TEST) return; auto itr = v.begin(); ll m = n; while (itr != v.end() && m != 0) { show2(*itr); cout << " "; itr++; m--; } cout << endl; } template <class T> void vv_show(const T &v, ll n = -1) { if (!IS_TEST) return; cout << "--------------------------------\n"; auto itr = v.begin(); ll m = n; while (itr != v.end() && m != 0) { v_show(*itr, n); itr++; m--; } cout << "--------------------------------" << endl; } /*--------------------------------*/ /*loading integers*/ void load(ll &x1) { cin >> x1; } void load(ll &x1, ll &x2) { cin >> x1 >> x2; } void load(ll &x1, ll &x2, ll &x3) { cin >> x1 >> x2 >> x3; } void load(ll &x1, ll &x2, ll &x3, ll &x4) { cin >> x1 >> x2 >> x3 >> x4; } void v_load(ll n, v_ll &v1, ll head = 0, ll tail = 0, ll init = 0) { ll m = n + head + tail; v1.assign(m, init); for (ll i = 0; i < n; i++) { cin >> v1[i + head]; } } void v_load(ll n, v_ll &v1, v_ll &v2, ll head = 0, ll tail = 0, ll init = 0) { ll m = n + head + tail; v1.assign(m, init); v2.assign(m, init); for (ll i = 0; i < n; i++) { cin >> v1[i + head] >> v2[i + head]; } } void v_load(ll n, v_ll &v1, v_ll &v2, v_ll &v3, ll head = 0, ll tail = 0, ll init = 0) { ll m = n + head + tail; v1.assign(m, init); v2.assign(m, init); v3.assign(m, init); for (ll i = 0; i < n; i++) { cin >> v1[i + head] >> v2[i + head] >> v3[i + head]; } } void v_load(ll n, v_ll &v1, v_ll &v2, v_ll &v3, v_ll &v4, ll head = 0, ll tail = 0, ll init = 0) { ll m = n + head + tail; v1.assign(m, init); v2.assign(m, init); v3.assign(m, init); v4.assign(m, init); for (ll i = 0; i < n; i++) { cin >> v1[i + head] >> v2[i + head] >> v3[i + head] >> v4[i + head]; } } /*--------------------------------*/ v_ll ll_mg(ll x1 = ll_max64, ll x2 = ll_max64, ll x3 = ll_max64, ll x4 = ll_max64) { v_ll x{x1, x2, x3, x4}; sort(x.begin(), x.end()); return x; } template <class T> void ch_max(T &x, const T &y) { if (x >= y) return; x = y; } template <class T> void ch_min(T &x, const T &y) { if (x <= y) return; x = y; } template <class T, class S> void ch_max(T &x, S &xx, const T &y, const S &yy) { if (x >= y) return; x = y; xx = yy; } template <class T, class S> void ch_min(T &x, S &xx, const T &y, const S &yy) { if (x <= y) return; x = y; xx = yy; } template <class T> void quit(T x) { cout << x << endl; exit(0); } ll rem(ll x, ll y) { ll z = x % y; return z >= 0 ? z : z + y; } /*modint*/ class ModInt { public: static ll modulus; ll val; ModInt(ll n = 0) { val = n % modulus; if (val < 0) val += modulus; } ModInt operator+(const ModInt &rhs) const { return val + rhs.val; } ModInt operator-(const ModInt &rhs) const { return val - rhs.val; } ModInt operator*(const ModInt &rhs) const { return val * rhs.val; } ModInt &operator+=(const ModInt &rhs) { val += rhs.val; if (val >= modulus) val -= modulus; return *this; } ModInt &operator-=(const ModInt &rhs) { val -= rhs.val; if (val < modulus) val += modulus; return *this; } ModInt &operator*=(const ModInt &rhs) { val *= rhs.val; val %= modulus; return *this; } ModInt pow(ll a) const { a %= (modulus - 1); if (a < 0) a += (modulus - 1); ModInt ret(1); ModInt vv(val); while (a != 0) { if (a & 1) ret *= vv; a >>= 1; vv *= vv; } return ret; } ModInt inv() const { return pow(-1); } ModInt operator/(const ModInt &rhs) const { return *this * rhs.pow(-1); } ModInt &operator/=(const ModInt &rhs) { *this *= rhs.pow(-1); return *this; } }; istream &operator>>(istream &istr, ModInt &rhs) { istr >> (rhs.val); return istr; }; ostream &operator<<(ostream &ostr, const ModInt &rhs) { ostr << (rhs.val); return ostr; }; vector<ModInt> mod_nck(ll n) { vector<ModInt> v(n + 1, 1); for (ll i = 1; i <= n; i++) v[i] = v[i - 1] * (n + 1 - i) / i; return v; } vector<ModInt> mod_npk(ll n) { vector<ModInt> v(n + 1, 1); for (ll i = 1; i <= n; i++) v[i] = v[i - 1] * (n + 1 - i); return v; } vector<ModInt> mod_fact(ll n) { vector<ModInt> v(n + 1, 1); for (ll i = 1; i <= n; i++) v[i] = v[i - 1] * i; return v; } vector<ModInt> mod_nck_2(ll n, ll k) { vector<ModInt> v(n + 1, 1); for (ll i = k + 1; i <= n; i++) v[i] = v[i - 1] * i / (i - k); return v; } ll ModInt::modulus = 1000000007; ll N; v_ll X, Y; int main() { load(N); v_load(N, X); Y.assign(N - 1, 0); for (ll i = 0; i < N; i++) { Y[i] = ModInt(i + 1).inv().val; } for (ll i = 1; i < N; i++) { Y[i] = (Y[i - 1] + Y[i]) % MOD; } ModInt ans(0); for (ll i = 0; i + 1 < N; i++) { ans += (X[i + 1] - X[i]) * Y[i]; } for (ll i = 1; i < N; i++) { ans *= i; } cout << ans.val << endl; }
#include <cassert> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <algorithm> #include <cmath> #include <functional> #include <bitset> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> #define TEST \ { IS_TEST = true; } #define fi first #define se second #define pb(x) \ { push_back(x); } using namespace std; using ll = int_fast64_t; using v_b = vector<bool>; using v_ll = vector<ll>; using str = string; using v_str = vector<string>; using p_ll = pair<ll, ll>; using vv_b = vector<v_b>; using vv_ll = vector<v_ll>; using vp_ll = vector<p_ll>; using vvv_ll = vector<vv_ll>; using vvp_ll = vector<vp_ll>; using ld = long double; using v_ld = vector<ld>; using vv_ld = vector<v_ld>; bool IS_TEST = false; ll ll_min64 = 1LL << 63; ll ll_max64 = ~ll_min64; ll ll_min32 = 1LL << 31; ll ll_max32 = ~ll_min32; ll MOD = 1000000007; /*displaying functions for debug*/ template <class T> void show2(const T &x) { cout << x; } template <class T1, class T2> void show2(const pair<T1, T2> &x) { cout << "{" << show2(x.first) << "," << show2(x.second) << "}"; } template <class T> void show(const T &x) { if (!IS_TEST) return; show2(x); cout << endl; } template <class T> void v_show(const T &v, ll n = -1) { if (!IS_TEST) return; auto itr = v.begin(); ll m = n; while (itr != v.end() && m != 0) { show2(*itr); cout << " "; itr++; m--; } cout << endl; } template <class T> void vv_show(const T &v, ll n = -1) { if (!IS_TEST) return; cout << "--------------------------------\n"; auto itr = v.begin(); ll m = n; while (itr != v.end() && m != 0) { v_show(*itr, n); itr++; m--; } cout << "--------------------------------" << endl; } /*--------------------------------*/ /*loading integers*/ void load(ll &x1) { cin >> x1; } void load(ll &x1, ll &x2) { cin >> x1 >> x2; } void load(ll &x1, ll &x2, ll &x3) { cin >> x1 >> x2 >> x3; } void load(ll &x1, ll &x2, ll &x3, ll &x4) { cin >> x1 >> x2 >> x3 >> x4; } void v_load(ll n, v_ll &v1, ll head = 0, ll tail = 0, ll init = 0) { ll m = n + head + tail; v1.assign(m, init); for (ll i = 0; i < n; i++) { cin >> v1[i + head]; } } void v_load(ll n, v_ll &v1, v_ll &v2, ll head = 0, ll tail = 0, ll init = 0) { ll m = n + head + tail; v1.assign(m, init); v2.assign(m, init); for (ll i = 0; i < n; i++) { cin >> v1[i + head] >> v2[i + head]; } } void v_load(ll n, v_ll &v1, v_ll &v2, v_ll &v3, ll head = 0, ll tail = 0, ll init = 0) { ll m = n + head + tail; v1.assign(m, init); v2.assign(m, init); v3.assign(m, init); for (ll i = 0; i < n; i++) { cin >> v1[i + head] >> v2[i + head] >> v3[i + head]; } } void v_load(ll n, v_ll &v1, v_ll &v2, v_ll &v3, v_ll &v4, ll head = 0, ll tail = 0, ll init = 0) { ll m = n + head + tail; v1.assign(m, init); v2.assign(m, init); v3.assign(m, init); v4.assign(m, init); for (ll i = 0; i < n; i++) { cin >> v1[i + head] >> v2[i + head] >> v3[i + head] >> v4[i + head]; } } /*--------------------------------*/ v_ll ll_mg(ll x1 = ll_max64, ll x2 = ll_max64, ll x3 = ll_max64, ll x4 = ll_max64) { v_ll x{x1, x2, x3, x4}; sort(x.begin(), x.end()); return x; } template <class T> void ch_max(T &x, const T &y) { if (x >= y) return; x = y; } template <class T> void ch_min(T &x, const T &y) { if (x <= y) return; x = y; } template <class T, class S> void ch_max(T &x, S &xx, const T &y, const S &yy) { if (x >= y) return; x = y; xx = yy; } template <class T, class S> void ch_min(T &x, S &xx, const T &y, const S &yy) { if (x <= y) return; x = y; xx = yy; } template <class T> void quit(T x) { cout << x << endl; exit(0); } ll rem(ll x, ll y) { ll z = x % y; return z >= 0 ? z : z + y; } /*modint*/ class ModInt { public: static ll modulus; ll val; ModInt(ll n = 0) { val = n % modulus; if (val < 0) val += modulus; } ModInt operator+(const ModInt &rhs) const { return val + rhs.val; } ModInt operator-(const ModInt &rhs) const { return val - rhs.val; } ModInt operator*(const ModInt &rhs) const { return val * rhs.val; } ModInt &operator+=(const ModInt &rhs) { val += rhs.val; if (val >= modulus) val -= modulus; return *this; } ModInt &operator-=(const ModInt &rhs) { val -= rhs.val; if (val < modulus) val += modulus; return *this; } ModInt &operator*=(const ModInt &rhs) { val *= rhs.val; val %= modulus; return *this; } ModInt pow(ll a) const { a %= (modulus - 1); if (a < 0) a += (modulus - 1); ModInt ret(1); ModInt vv(val); while (a != 0) { if (a & 1) ret *= vv; a >>= 1; vv *= vv; } return ret; } ModInt inv() const { return pow(-1); } ModInt operator/(const ModInt &rhs) const { return *this * rhs.pow(-1); } ModInt &operator/=(const ModInt &rhs) { *this *= rhs.pow(-1); return *this; } }; istream &operator>>(istream &istr, ModInt &rhs) { istr >> (rhs.val); return istr; }; ostream &operator<<(ostream &ostr, const ModInt &rhs) { ostr << (rhs.val); return ostr; }; vector<ModInt> mod_nck(ll n) { vector<ModInt> v(n + 1, 1); for (ll i = 1; i <= n; i++) v[i] = v[i - 1] * (n + 1 - i) / i; return v; } vector<ModInt> mod_npk(ll n) { vector<ModInt> v(n + 1, 1); for (ll i = 1; i <= n; i++) v[i] = v[i - 1] * (n + 1 - i); return v; } vector<ModInt> mod_fact(ll n) { vector<ModInt> v(n + 1, 1); for (ll i = 1; i <= n; i++) v[i] = v[i - 1] * i; return v; } vector<ModInt> mod_nck_2(ll n, ll k) { vector<ModInt> v(n + 1, 1); for (ll i = k + 1; i <= n; i++) v[i] = v[i - 1] * i / (i - k); return v; } ll ModInt::modulus = 1000000007; ll N; v_ll X, Y; int main() { load(N); v_load(N, X); Y.assign(N + 100, 0); for (ll i = 0; i < N; i++) { Y[i] = ModInt(i + 1).inv().val; } for (ll i = 1; i < N; i++) { Y[i] = (Y[i - 1] + Y[i]) % MOD; } ModInt ans(0); for (ll i = 0; i + 1 < N; i++) { ans += (X[i + 1] - X[i]) * Y[i]; } for (ll i = 1; i < N; i++) { ans *= i; } cout << ans.val << endl; }
replace
266
267
266
267
0
p02807
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n); i >= 0; i--) #define FOR(i, m, n) for (int i = (m); i < (n); i++) #define ALL(obj) begin(obj), end(obj) using namespace std; using ll = long long; using ull = unsigned long long; const int INF = 2100100100; const int MOD = 1e9 + 7; // 多次元 vector 生成 template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <int mod> struct ModInt { int val; ModInt() : val(0) {} ModInt(long long x) : val(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((val += p.val) >= mod) { val -= mod; } return *this; } ModInt &operator-=(const ModInt &p) { if ((val += mod - p.val) >= mod) { val -= mod; } return *this; } ModInt &operator*=(const ModInt &p) { val = (int)(1LL * val * p.val % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-val); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return val == p.val; } bool operator!=(const ModInt &p) const { return val != p.val; } ModInt inverse() const { int a = val, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(long long n) const { ModInt ret(1), mul(val); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.val; } friend istream &operator>>(istream &is, ModInt &a) { long long t; is >> t; a = ModInt<mod>(t); return (is); } static int get_mod() { return mod; } }; using modint = ModInt<MOD>; int main() { // cin.tie(0); // ios::sync_with_stdio(false); int N; cin >> N; vector<int> x(N); vector<modint> a(N), c(N), fib(N); REP(i, N) { cin >> x.at(i); } REP(i, N - 1) { a[i + 1] = modint(x[i + 1] - x[i]); } fib[0] = modint(1); REP(i, N) { fib[i + 1] = fib[i] * modint(i + 1); } c[0] = modint(0); REP(i, N - 1) { c[i + 1] = c[i] + modint(1) / modint(i + 1); } modint ans = modint(0); REP(i, N - 1) { ans += a[i + 1] * c[i + 1]; } cout << fib[N - 1] * ans << endl; return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n); i >= 0; i--) #define FOR(i, m, n) for (int i = (m); i < (n); i++) #define ALL(obj) begin(obj), end(obj) using namespace std; using ll = long long; using ull = unsigned long long; const int INF = 2100100100; const int MOD = 1e9 + 7; // 多次元 vector 生成 template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <int mod> struct ModInt { int val; ModInt() : val(0) {} ModInt(long long x) : val(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((val += p.val) >= mod) { val -= mod; } return *this; } ModInt &operator-=(const ModInt &p) { if ((val += mod - p.val) >= mod) { val -= mod; } return *this; } ModInt &operator*=(const ModInt &p) { val = (int)(1LL * val * p.val % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-val); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return val == p.val; } bool operator!=(const ModInt &p) const { return val != p.val; } ModInt inverse() const { int a = val, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(long long n) const { ModInt ret(1), mul(val); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.val; } friend istream &operator>>(istream &is, ModInt &a) { long long t; is >> t; a = ModInt<mod>(t); return (is); } static int get_mod() { return mod; } }; using modint = ModInt<MOD>; int main() { // cin.tie(0); // ios::sync_with_stdio(false); int N; cin >> N; vector<int> x(N); vector<modint> a(N), c(N), fib(N); REP(i, N) { cin >> x.at(i); } REP(i, N - 1) { a[i + 1] = modint(x[i + 1] - x[i]); } fib[0] = modint(1); REP(i, N - 1) { fib[i + 1] = fib[i] * modint(i + 1); } c[0] = modint(0); REP(i, N - 1) { c[i + 1] = c[i] + modint(1) / modint(i + 1); } modint ans = modint(0); REP(i, N - 1) { ans += a[i + 1] * c[i + 1]; } cout << fib[N - 1] * ans << endl; return 0; }
replace
107
108
107
108
0
p02807
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cstdio> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; #define rep(i, x) for (ll i = 0; i < (ll)(x); i++) #define pb push_back #define eb emplace_back #define debug(x) cerr << #x << ": " << (x) << "\n"; #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef pair<int, int> P; typedef pair<ll, ll> Pll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<vector<ll>>> vvvl; const ll INF = numeric_limits<ll>::max() / 4; const ll MOD = 1e9 + 7; const int n_max = 1e5 + 10; void print() { cout << endl; } template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) { cout << head; if (sizeof...(tail) != 0) cout << ' '; print(forward<Tail>(tail)...); } template <class T> void print(vector<T> &vec) { for (auto &a : vec) { cout << a; if (&a != &vec.back()) cout << ' '; } cout << endl; } template <class T> void print(vector<vector<T>> &df) { for (auto &vec : df) { print(vec); } } const ll MAX = 1e5 + 10; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } ll modinv(ll a, ll m) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int main() { ll n; cin >> n; vector<ll> x(n); rep(i, n) cin >> x[i]; ll ans = 0; assert(n <= 2000); vector<ll> dp(n + 10, 0); dp[0] = 1; rep(i, n) dp[i + 1] = dp[i] * (i + 1) % MOD; // print(dp); // rep(i,n-1){ // ll dis = x[i+1]- x[i]; // ll temp = dp[n-1]; // temp = (temp * dp[n-1] / (n-i-1)); // ans += temp * dis; // ans %= MOD; // } COMinit(); // rep(i,n)rep(j,n){ // if(i >= j)continue; // ll dis = x[j] - x[i]; // ll temp; // if(j == n-1){ // temp = dp[n-1] * modinv((j-i), MOD); // } // else{ // temp = (COM(n-1, j-i+1) * dp[j-i-1]) % MOD; // temp = (temp * dp[n-j+i-2]) % MOD; // } // temp %= MOD; // // debug(i);debug(j); // // debug(temp); // ans = (ans + (temp * dis) % MOD) % MOD; // } vector<ll> dp2(n + 10); dp2[0] = 0; rep(i, n + 1) { dp2[i + 1] = (dp2[i] * (i + 1)) % MOD + (dp[i + 1] * modinv((i + 1), MOD)) % MOD; dp2[i + 1] %= MOD; } // print(dp2); rep(i, n - 1) { ll dis = x[i + 1] - x[i]; ll temp = dp2[i + 1]; temp *= (dp[n - 1] * modinv(dp[i + 1], MOD)) % MOD; temp %= MOD; ans = (ans + (temp * dis) % MOD) % MOD; } cout << ans << endl; }
#include <algorithm> #include <bits/stdc++.h> #include <cstdio> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; #define rep(i, x) for (ll i = 0; i < (ll)(x); i++) #define pb push_back #define eb emplace_back #define debug(x) cerr << #x << ": " << (x) << "\n"; #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef pair<int, int> P; typedef pair<ll, ll> Pll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<vector<ll>>> vvvl; const ll INF = numeric_limits<ll>::max() / 4; const ll MOD = 1e9 + 7; const int n_max = 1e5 + 10; void print() { cout << endl; } template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) { cout << head; if (sizeof...(tail) != 0) cout << ' '; print(forward<Tail>(tail)...); } template <class T> void print(vector<T> &vec) { for (auto &a : vec) { cout << a; if (&a != &vec.back()) cout << ' '; } cout << endl; } template <class T> void print(vector<vector<T>> &df) { for (auto &vec : df) { print(vec); } } const ll MAX = 1e5 + 10; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } ll modinv(ll a, ll m) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int main() { ll n; cin >> n; vector<ll> x(n); rep(i, n) cin >> x[i]; ll ans = 0; // assert(n <= 2000); vector<ll> dp(n + 10, 0); dp[0] = 1; rep(i, n) dp[i + 1] = dp[i] * (i + 1) % MOD; // print(dp); // rep(i,n-1){ // ll dis = x[i+1]- x[i]; // ll temp = dp[n-1]; // temp = (temp * dp[n-1] / (n-i-1)); // ans += temp * dis; // ans %= MOD; // } COMinit(); // rep(i,n)rep(j,n){ // if(i >= j)continue; // ll dis = x[j] - x[i]; // ll temp; // if(j == n-1){ // temp = dp[n-1] * modinv((j-i), MOD); // } // else{ // temp = (COM(n-1, j-i+1) * dp[j-i-1]) % MOD; // temp = (temp * dp[n-j+i-2]) % MOD; // } // temp %= MOD; // // debug(i);debug(j); // // debug(temp); // ans = (ans + (temp * dis) % MOD) % MOD; // } vector<ll> dp2(n + 10); dp2[0] = 0; rep(i, n + 1) { dp2[i + 1] = (dp2[i] * (i + 1)) % MOD + (dp[i + 1] * modinv((i + 1), MOD)) % MOD; dp2[i + 1] %= MOD; } // print(dp2); rep(i, n - 1) { ll dis = x[i + 1] - x[i]; ll temp = dp2[i + 1]; temp *= (dp[n - 1] * modinv(dp[i + 1], MOD)) % MOD; temp %= MOD; ans = (ans + (temp * dis) % MOD) % MOD; } cout << ans << endl; }
replace
93
94
93
94
0
p02808
C++
Runtime Error
#line 1 "main.cpp" #include <bits/stdc++.h> #line 2 "/home/ubuntu/Library/utils/macros.hpp" #define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i)) #define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i)) #define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i)) #define ALL(x) std::begin(x), std::end(x) #line 2 "/home/ubuntu/Library/modulus/mint.hpp" #include <algorithm> #include <cassert> #include <iostream> #line 2 "/home/ubuntu/Library/modulus/modpow.hpp" #include <cassert> inline constexpr int32_t modpow(uint_fast64_t x, uint64_t k, int32_t MOD) { assert(0 <= x and x < MOD); uint_fast64_t y = 1; for (; k; k >>= 1) { if (k & 1) (y *= x) %= MOD; (x *= x) %= MOD; } assert(0 <= y and y < MOD); return y; } #line 2 "/home/ubuntu/Library/modulus/modinv.hpp" #include <algorithm> #include <cassert> inline int32_t modinv_nocheck(int32_t value, int32_t MOD) { assert(0 <= value and value < MOD); if (value == 0) return -1; int64_t a = value, b = MOD; int64_t x = 0, y = 1; for (int64_t u = 1, v = 0; a;) { int64_t q = b / a; x -= q * u; std::swap(x, u); y -= q * v; std::swap(y, v); b -= q * a; std::swap(b, a); } if (not(value * x + MOD * y == b and b == 1)) return -1; if (x < 0) x += MOD; assert(0 <= x and x < MOD); return x; } inline int32_t modinv(int32_t x, int32_t MOD) { int32_t y = modinv_nocheck(x, MOD); assert(y != -1); return y; } #line 7 "/home/ubuntu/Library/modulus/mint.hpp" template <int32_t MOD> struct mint { int32_t value; mint() : value() {} mint(int64_t value_) : value(value_ < 0 ? value_ % MOD + MOD : value_ >= MOD ? value_ % MOD : value_) {} mint(int32_t value_, std::nullptr_t) : value(value_) {} explicit operator bool() const { return value; } inline constexpr mint<MOD> operator+(mint<MOD> other) const { return mint<MOD>(*this) += other; } inline constexpr mint<MOD> operator-(mint<MOD> other) const { return mint<MOD>(*this) -= other; } inline constexpr mint<MOD> operator*(mint<MOD> other) const { return mint<MOD>(*this) *= other; } inline constexpr mint<MOD> &operator+=(mint<MOD> other) { this->value += other.value; if (this->value >= MOD) this->value -= MOD; return *this; } inline constexpr mint<MOD> &operator-=(mint<MOD> other) { this->value -= other.value; if (this->value < 0) this->value += MOD; return *this; } inline constexpr mint<MOD> &operator*=(mint<MOD> other) { this->value = (uint_fast64_t)this->value * other.value % MOD; return *this; } inline constexpr mint<MOD> operator-() const { return mint<MOD>(this->value ? MOD - this->value : 0, nullptr); } inline constexpr mint<MOD> pow(uint64_t k) const { return mint<MOD>(modpow(value, k, MOD), nullptr); } inline mint<MOD> inv() const { return mint<MOD>(modinv(value, MOD), nullptr); } inline constexpr mint<MOD> operator/(mint<MOD> other) const { return *this * other.inv(); } inline constexpr mint<MOD> operator/=(mint<MOD> other) { return *this *= other.inv(); } inline constexpr bool operator==(mint<MOD> other) const { return value == other.value; } inline constexpr bool operator!=(mint<MOD> other) const { return value != other.value; } }; template <int32_t MOD> mint<MOD> operator*(int64_t value, mint<MOD> n) { return mint<MOD>(value) * n; } template <int32_t MOD> std::istream &operator>>(std::istream &in, mint<MOD> &n) { int64_t value; in >> value; n = value; return in; } template <int32_t MOD> std::ostream &operator<<(std::ostream &out, mint<MOD> n) { return out << n.value; } #line 2 "/home/ubuntu/Library/modulus/choose.hpp" #include <cassert> #line 2 "/home/ubuntu/Library/modulus/factorial.hpp" #include <vector> #line 4 "/home/ubuntu/Library/modulus/factorial.hpp" template <int32_t MOD> mint<MOD> fact(int n) { static std::vector<mint<MOD>> memo(1, 1); while (n >= memo.size()) { memo.push_back(memo.back() * mint<MOD>(memo.size())); } return memo[n]; } template <int32_t MOD> mint<MOD> inv_fact(int n) { static std::vector<mint<MOD>> memo; if (memo.size() <= n) { int l = memo.size(); int r = n * 1.3 + 100; memo.resize(r); memo[r - 1] = fact<MOD>(r - 1).inv(); for (int i = r - 2; i >= l; --i) { memo[i] = memo[i + 1] * (i + 1); } } return memo[n]; } #line 6 "/home/ubuntu/Library/modulus/choose.hpp" /** * @note O(n log n) at first time, otherwise O(1) */ template <int32_t MOD> mint<MOD> choose(int n, int r) { assert(0 <= r and r <= n); return fact<MOD>(n) * inv_fact<MOD>(n - r) * inv_fact<MOD>(r); } template <int32_t MOD> mint<MOD> permute(int n, int r) { assert(0 <= r and r <= n); return fact<MOD>(n) * inv_fact<MOD>(n - r); } template <int32_t MOD> mint<MOD> multichoose(int n, int r) { assert(0 <= n and 0 <= r); if (n == 0 and r == 0) return 1; return choose<MOD>(n + r - 1, r); } #line 5 "main.cpp" using namespace std; template <typename T> istream &operator>>(istream &in, vector<T> &xs) { REP(i, xs.size()) { in >> xs[i]; } return in; } constexpr int MOD = 1e9 + 7; mint<MOD> solve(int n, int k, const vector<int> &a) { vector<mint<MOD>> cur(n + 1), prv; cur[0] += 1; REP(i, k) { cur.swap(prv); cur.assign(n + 1, 0); REP(x, n + 1) { REP(y, min(a[i] + 1, n + 1 - x)) { int z = a[i] - y; cur[x + y] += prv[x] * choose<MOD>(n - x, y) * choose<MOD>(n - y, z); } } } return cur[n]; } int main() { int n, k; cin >> n >> k; vector<int> a(n); cin >> a; cout << solve(n, k, a) << endl; return 0; }
#line 1 "main.cpp" #include <bits/stdc++.h> #line 2 "/home/ubuntu/Library/utils/macros.hpp" #define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i)) #define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i)) #define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i)) #define ALL(x) std::begin(x), std::end(x) #line 2 "/home/ubuntu/Library/modulus/mint.hpp" #include <algorithm> #include <cassert> #include <iostream> #line 2 "/home/ubuntu/Library/modulus/modpow.hpp" #include <cassert> inline constexpr int32_t modpow(uint_fast64_t x, uint64_t k, int32_t MOD) { assert(0 <= x and x < MOD); uint_fast64_t y = 1; for (; k; k >>= 1) { if (k & 1) (y *= x) %= MOD; (x *= x) %= MOD; } assert(0 <= y and y < MOD); return y; } #line 2 "/home/ubuntu/Library/modulus/modinv.hpp" #include <algorithm> #include <cassert> inline int32_t modinv_nocheck(int32_t value, int32_t MOD) { assert(0 <= value and value < MOD); if (value == 0) return -1; int64_t a = value, b = MOD; int64_t x = 0, y = 1; for (int64_t u = 1, v = 0; a;) { int64_t q = b / a; x -= q * u; std::swap(x, u); y -= q * v; std::swap(y, v); b -= q * a; std::swap(b, a); } if (not(value * x + MOD * y == b and b == 1)) return -1; if (x < 0) x += MOD; assert(0 <= x and x < MOD); return x; } inline int32_t modinv(int32_t x, int32_t MOD) { int32_t y = modinv_nocheck(x, MOD); assert(y != -1); return y; } #line 7 "/home/ubuntu/Library/modulus/mint.hpp" template <int32_t MOD> struct mint { int32_t value; mint() : value() {} mint(int64_t value_) : value(value_ < 0 ? value_ % MOD + MOD : value_ >= MOD ? value_ % MOD : value_) {} mint(int32_t value_, std::nullptr_t) : value(value_) {} explicit operator bool() const { return value; } inline constexpr mint<MOD> operator+(mint<MOD> other) const { return mint<MOD>(*this) += other; } inline constexpr mint<MOD> operator-(mint<MOD> other) const { return mint<MOD>(*this) -= other; } inline constexpr mint<MOD> operator*(mint<MOD> other) const { return mint<MOD>(*this) *= other; } inline constexpr mint<MOD> &operator+=(mint<MOD> other) { this->value += other.value; if (this->value >= MOD) this->value -= MOD; return *this; } inline constexpr mint<MOD> &operator-=(mint<MOD> other) { this->value -= other.value; if (this->value < 0) this->value += MOD; return *this; } inline constexpr mint<MOD> &operator*=(mint<MOD> other) { this->value = (uint_fast64_t)this->value * other.value % MOD; return *this; } inline constexpr mint<MOD> operator-() const { return mint<MOD>(this->value ? MOD - this->value : 0, nullptr); } inline constexpr mint<MOD> pow(uint64_t k) const { return mint<MOD>(modpow(value, k, MOD), nullptr); } inline mint<MOD> inv() const { return mint<MOD>(modinv(value, MOD), nullptr); } inline constexpr mint<MOD> operator/(mint<MOD> other) const { return *this * other.inv(); } inline constexpr mint<MOD> operator/=(mint<MOD> other) { return *this *= other.inv(); } inline constexpr bool operator==(mint<MOD> other) const { return value == other.value; } inline constexpr bool operator!=(mint<MOD> other) const { return value != other.value; } }; template <int32_t MOD> mint<MOD> operator*(int64_t value, mint<MOD> n) { return mint<MOD>(value) * n; } template <int32_t MOD> std::istream &operator>>(std::istream &in, mint<MOD> &n) { int64_t value; in >> value; n = value; return in; } template <int32_t MOD> std::ostream &operator<<(std::ostream &out, mint<MOD> n) { return out << n.value; } #line 2 "/home/ubuntu/Library/modulus/choose.hpp" #include <cassert> #line 2 "/home/ubuntu/Library/modulus/factorial.hpp" #include <vector> #line 4 "/home/ubuntu/Library/modulus/factorial.hpp" template <int32_t MOD> mint<MOD> fact(int n) { static std::vector<mint<MOD>> memo(1, 1); while (n >= memo.size()) { memo.push_back(memo.back() * mint<MOD>(memo.size())); } return memo[n]; } template <int32_t MOD> mint<MOD> inv_fact(int n) { static std::vector<mint<MOD>> memo; if (memo.size() <= n) { int l = memo.size(); int r = n * 1.3 + 100; memo.resize(r); memo[r - 1] = fact<MOD>(r - 1).inv(); for (int i = r - 2; i >= l; --i) { memo[i] = memo[i + 1] * (i + 1); } } return memo[n]; } #line 6 "/home/ubuntu/Library/modulus/choose.hpp" /** * @note O(n log n) at first time, otherwise O(1) */ template <int32_t MOD> mint<MOD> choose(int n, int r) { assert(0 <= r and r <= n); return fact<MOD>(n) * inv_fact<MOD>(n - r) * inv_fact<MOD>(r); } template <int32_t MOD> mint<MOD> permute(int n, int r) { assert(0 <= r and r <= n); return fact<MOD>(n) * inv_fact<MOD>(n - r); } template <int32_t MOD> mint<MOD> multichoose(int n, int r) { assert(0 <= n and 0 <= r); if (n == 0 and r == 0) return 1; return choose<MOD>(n + r - 1, r); } #line 5 "main.cpp" using namespace std; template <typename T> istream &operator>>(istream &in, vector<T> &xs) { REP(i, xs.size()) { in >> xs[i]; } return in; } constexpr int MOD = 1e9 + 7; mint<MOD> solve(int n, int k, const vector<int> &a) { vector<mint<MOD>> cur(n + 1), prv; cur[0] += 1; REP(i, k) { cur.swap(prv); cur.assign(n + 1, 0); REP(x, n + 1) { REP(y, min(a[i] + 1, n + 1 - x)) { int z = a[i] - y; cur[x + y] += prv[x] * choose<MOD>(n - x, y) * choose<MOD>(n - y, z); } } } return cur[n]; } int main() { int n, k; cin >> n >> k; vector<int> a(k); cin >> a; cout << solve(n, k, a) << endl; return 0; }
replace
202
203
202
203
0
p02808
C++
Runtime Error
#include <cstring> #include <iostream> using namespace std; const int M = 1000000007; int main() { int n, k; cin >> n >> k; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long dp[k + 1][n + 1], comb[n + 1][n + 1]; memset(comb, 0, sizeof comb); comb[0][0] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j <= i; j++) comb[i][j] = ((j > 0 ? comb[i - 1][j - 1] : 0) + comb[i - 1][j]) % M; for (int i = 0; i <= n; i++) dp[0][i] = i == 0; for (int i = 0; i < k; i++) { for (int j = 0; j <= n; j++) { long long sum = 0; for (int l = 0; l <= min(a[i], j); l++) { sum += dp[i][j - l] * comb[n - j + l][l] % M * comb[n - l][a[i] - l] % M; } dp[i + 1][j] = sum % M; } } cout << dp[k][n] << endl; }
#include <cstring> #include <iostream> using namespace std; const int M = 1000000007; int main() { int n, k; cin >> n >> k; int a[k]; for (int i = 0; i < k; i++) cin >> a[i]; long long dp[k + 1][n + 1], comb[n + 1][n + 1]; memset(comb, 0, sizeof comb); comb[0][0] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j <= i; j++) comb[i][j] = ((j > 0 ? comb[i - 1][j - 1] : 0) + comb[i - 1][j]) % M; for (int i = 0; i <= n; i++) dp[0][i] = i == 0; for (int i = 0; i < k; i++) { for (int j = 0; j <= n; j++) { long long sum = 0; for (int l = 0; l <= min(a[i], j); l++) { sum += dp[i][j - l] * comb[n - j + l][l] % M * comb[n - l][a[i] - l] % M; } dp[i + 1][j] = sum % M; } } cout << dp[k][n] << endl; }
replace
9
11
9
11
0
p02808
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define MAX_N 200010 long long inv[MAX_N]; long long factorial[MAX_N]; long long inv_factorial[MAX_N]; void GetInv() { for (int i = 1; i < MAX_N; i++) { if (i == 1) inv[i] = 1; else { inv[i] = (mod - (mod / i) * inv[mod % i]) % mod; if (inv[i] < 0) inv[i] += mod; } } } void GetFactorial() { factorial[0] = 1; inv_factorial[0] = 1; for (int i = 1; i < MAX_N; i++) { factorial[i] = factorial[i - 1] * i; factorial[i] %= mod; inv_factorial[i] = inv_factorial[i - 1] * inv[i]; inv_factorial[i] %= mod; } } long long combination(int n, int r) { if (n < 0 || r < 0 || n < r) return 0; long long ret = factorial[n] * inv_factorial[r]; ret %= mod; ret *= inv_factorial[n - r]; return ret % mod; } long long modpow(int n, int r) { long long ret = 1; long long tmp = (long long)n; while (r != 0) { if (r % 2) ret *= tmp; tmp *= tmp; tmp %= mod; ret %= mod; r /= 2; } return ret; } long long dp[21][1010]; int main() { GetInv(); GetFactorial(); cin.tie(nullptr); ios::sync_with_stdio(false); int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < k; i++) cin >> a[i]; dp[0][n] = 1; for (int i = 0; i < k; i++) { for (int j = 0; j <= n; j++) { for (int k = 0; k <= j; k++) { dp[i + 1][j - k] += dp[i][j] * combination(n - k, a[i] - k) % mod * inv_factorial[k] % mod; dp[i + 1][j - k] %= mod; } } } cout << dp[k][0] * factorial[n] % mod << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define MAX_N 200010 long long inv[MAX_N]; long long factorial[MAX_N]; long long inv_factorial[MAX_N]; void GetInv() { for (int i = 1; i < MAX_N; i++) { if (i == 1) inv[i] = 1; else { inv[i] = (mod - (mod / i) * inv[mod % i]) % mod; if (inv[i] < 0) inv[i] += mod; } } } void GetFactorial() { factorial[0] = 1; inv_factorial[0] = 1; for (int i = 1; i < MAX_N; i++) { factorial[i] = factorial[i - 1] * i; factorial[i] %= mod; inv_factorial[i] = inv_factorial[i - 1] * inv[i]; inv_factorial[i] %= mod; } } long long combination(int n, int r) { if (n < 0 || r < 0 || n < r) return 0; long long ret = factorial[n] * inv_factorial[r]; ret %= mod; ret *= inv_factorial[n - r]; return ret % mod; } long long modpow(int n, int r) { long long ret = 1; long long tmp = (long long)n; while (r != 0) { if (r % 2) ret *= tmp; tmp *= tmp; tmp %= mod; ret %= mod; r /= 2; } return ret; } long long dp[21][1010]; int main() { GetInv(); GetFactorial(); cin.tie(nullptr); ios::sync_with_stdio(false); int n, k; cin >> n >> k; vector<int> a(k); for (int i = 0; i < k; i++) cin >> a[i]; dp[0][n] = 1; for (int i = 0; i < k; i++) { for (int j = 0; j <= n; j++) { for (int k = 0; k <= j; k++) { dp[i + 1][j - k] += dp[i][j] * combination(n - k, a[i] - k) % mod * inv_factorial[k] % mod; dp[i + 1][j - k] %= mod; } } } cout << dp[k][0] * factorial[n] % mod << "\n"; return 0; }
replace
63
64
63
64
0
p02808
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define LLI long long int #define FOR(v, a, b) for (LLI v = (a); v < (b); ++v) #define FORE(v, a, b) for (LLI v = (a); v <= (b); ++v) #define REP(v, n) FOR(v, 0, n) #define REPE(v, n) FORE(v, 0, n) #define REV(v, a, b) for (LLI v = (a); v >= (b); --v) #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() #define ITR(it, c) for (auto it = (c).begin(); it != (c).end(); ++it) #define RITR(it, c) for (auto it = (c).rbegin(); it != (c).rend(); ++it) #define EXIST(c, x) ((c).find(x) != (c).end()) #define fst first #define snd second #define popcount __builtin_popcount #define UNIQ(v) (v).erase(unique(ALL(v)), (v).end()) #define bit(i) (1LL << (i)) #ifdef DEBUG #include <misc/C++/Debug.cpp> #else #define dump(...) ((void)0) #endif #define gcd __gcd using namespace std; template <class T> constexpr T lcm(T m, T n) { return m / gcd(m, n) * n; } template <typename I> void join(ostream &ost, I s, I t, string d = " ") { for (auto i = s; i != t; ++i) { if (i != s) ost << d; ost << *i; } ost << endl; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &a : v) is >> a; return is; } template <typename T, typename U> bool chmin(T &a, const U &b) { return (a > b ? a = b, true : false); } template <typename T, typename U> bool chmax(T &a, const U &b) { return (a < b ? a = b, true : false); } template <typename T, size_t N, typename U> void fill_array(T (&a)[N], const U &v) { fill((U *)a, (U *)(a + N), v); } struct Init { Init() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(12); cerr << fixed << setprecision(12); } } init; template <std::uint32_t M> class ModInt { public: std::uint64_t val; ModInt() : val(0) {} ModInt(std::int64_t n) { if (n >= M) val = n % M; else if (n < 0) val = n % M + M; else val = n; } inline constexpr ModInt operator+(const ModInt &a) const { return ModInt((val + a.val) % M); } inline constexpr ModInt operator-(const ModInt &a) const { return ModInt((val - a.val + M) % M); } inline constexpr ModInt operator*(const ModInt &a) const { return ModInt((val * a.val) % M); } inline constexpr ModInt operator/(const ModInt &a) const { return ModInt((val * a.inv().val) % M); } inline constexpr ModInt &operator=(const ModInt &a) { val = a.val; return *this; } inline constexpr ModInt &operator+=(const ModInt &a) { if ((val += a.val) >= M) val -= M; return *this; } inline constexpr ModInt &operator-=(const ModInt &a) { if (val < a.val) val += M; val -= a.val; return *this; } inline constexpr ModInt &operator*=(const ModInt &a) { (val *= a.val) %= M; return *this; } inline constexpr ModInt &operator/=(const ModInt &a) { (val *= a.inv().val) %= M; return *this; } inline constexpr bool operator==(const ModInt &a) const { return val == a.val; } inline constexpr bool operator!=(const ModInt &a) const { return val != a.val; } inline constexpr ModInt &operator++() { *this += 1; return *this; } inline constexpr ModInt &operator--() { *this -= 1; return *this; } inline constexpr ModInt operator++(int) { ModInt t = *this; *this += 1; return t; } inline constexpr ModInt operator--(int) { ModInt t = *this; *this -= 1; return t; } inline constexpr static ModInt frac(std::int64_t a, std::int64_t b) { return ModInt(a) / ModInt(b); } inline constexpr static ModInt power(std::int64_t n, std::int64_t p) { if (p < 0) return power(n, -p).inv(); ModInt ret = 1, e = n; for (; p; e *= e, p >>= 1) if (p & 1) ret *= e; return ret; } inline constexpr ModInt power(std::int64_t p) const { return power(val, p); } inline constexpr ModInt inv() const { std::int64_t a = val, b = M, u = 1, v = 0; while (b) { std::int64_t t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } u %= M; if (u < 0) u += M; return u; } }; template <std::uint32_t M> ModInt<M> operator-(const ModInt<M> &a) { return M - a.val; } template <std::uint32_t M> ModInt<M> operator+(std::int64_t a, const ModInt<M> &b) { return ModInt<M>(ModInt<M>(a) + b.val); } template <std::uint32_t M> ModInt<M> operator-(std::int64_t a, const ModInt<M> &b) { return ModInt<M>(ModInt<M>(a) - b.val); } template <std::uint32_t M> ModInt<M> operator*(std::int64_t a, const ModInt<M> &b) { return ModInt<M>(ModInt<M>(a) * b.val); } template <std::uint32_t M> ModInt<M> operator/(std::int64_t a, const ModInt<M> &b) { return ModInt<M>(ModInt<M>(a) / b.val); } template <std::uint32_t M> std::istream &operator>>(std::istream &is, ModInt<M> &a) { is >> a.val; return is; } template <std::uint32_t M> std::ostream &operator<<(std::ostream &os, const ModInt<M> &a) { os << a.val; return os; } /** * @attention 使用前にinit関数を呼び出す */ template <typename T> class Combinatorics { public: static std::vector<T> facto; static std::vector<T> ifacto; static void init(int N) { facto.assign(N + 1, 1); ifacto.assign(N + 1, 1); for (int i = 1; i <= N; ++i) { facto[i] = facto[i - 1] * i; } ifacto[N] = facto[N].inv(); for (int i = N - 1; i >= 0; --i) { ifacto[i] = ifacto[i + 1] * (i + 1); } } static T f(int64_t i) { assert(i < facto.size()); return facto[i]; } static T finv(int64_t i) { assert(i < ifacto.size()); return ifacto[i]; } static T P(int64_t n, int64_t k); static T C(int64_t n, int64_t k); static T H(int64_t n, int64_t k); static T stirling_number(int64_t n, int64_t k); static T bell_number(int64_t n, int64_t k); static std::vector<T> bernoulli_number(int64_t n); static T catalan_number(int64_t n); }; template <typename T> std::vector<T> Combinatorics<T>::facto = std::vector<T>(); template <typename T> std::vector<T> Combinatorics<T>::ifacto = std::vector<T>(); template <typename T> T Combinatorics<T>::P(int64_t n, int64_t k) { if (n < k or n < 0 or k < 0) return 0; return f(n) * finv(n - k); } template <typename T> T Combinatorics<T>::C(int64_t n, int64_t k) { if (n < k or n < 0 or k < 0) return 0; return P(n, k) * finv(k); } template <typename T> T Combinatorics<T>::H(int64_t n, int64_t k) { if (n == 0 and k == 0) return 1; return C(n + k - 1, k); } using mint = ModInt<1000000007>; using C = Combinatorics<mint>; int main() { C::init(100000); int N, K; while (cin >> N >> K) { vector<int> a(K); cin >> a; dump(a); vector<vector<mint>> dp(K + 1, vector<mint>(N + 1, 0)); dp[0][0] = 1; REP(i, K) { REPE(j, N) { REPE(x, N) { if (j + x <= N) dp[i + 1][j + x] += dp[i][j] * C::C(N - x, a[i] - x) / C::f(x); } } } mint ans = dp[K][N] * C::f(N); cout << ans << endl; } return 0; }
#include <bits/stdc++.h> #define LLI long long int #define FOR(v, a, b) for (LLI v = (a); v < (b); ++v) #define FORE(v, a, b) for (LLI v = (a); v <= (b); ++v) #define REP(v, n) FOR(v, 0, n) #define REPE(v, n) FORE(v, 0, n) #define REV(v, a, b) for (LLI v = (a); v >= (b); --v) #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() #define ITR(it, c) for (auto it = (c).begin(); it != (c).end(); ++it) #define RITR(it, c) for (auto it = (c).rbegin(); it != (c).rend(); ++it) #define EXIST(c, x) ((c).find(x) != (c).end()) #define fst first #define snd second #define popcount __builtin_popcount #define UNIQ(v) (v).erase(unique(ALL(v)), (v).end()) #define bit(i) (1LL << (i)) #ifdef DEBUG #include <misc/C++/Debug.cpp> #else #define dump(...) ((void)0) #endif #define gcd __gcd using namespace std; template <class T> constexpr T lcm(T m, T n) { return m / gcd(m, n) * n; } template <typename I> void join(ostream &ost, I s, I t, string d = " ") { for (auto i = s; i != t; ++i) { if (i != s) ost << d; ost << *i; } ost << endl; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &a : v) is >> a; return is; } template <typename T, typename U> bool chmin(T &a, const U &b) { return (a > b ? a = b, true : false); } template <typename T, typename U> bool chmax(T &a, const U &b) { return (a < b ? a = b, true : false); } template <typename T, size_t N, typename U> void fill_array(T (&a)[N], const U &v) { fill((U *)a, (U *)(a + N), v); } struct Init { Init() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(12); cerr << fixed << setprecision(12); } } init; template <std::uint32_t M> class ModInt { public: std::uint64_t val; ModInt() : val(0) {} ModInt(std::int64_t n) { if (n >= M) val = n % M; else if (n < 0) val = n % M + M; else val = n; } inline constexpr ModInt operator+(const ModInt &a) const { return ModInt((val + a.val) % M); } inline constexpr ModInt operator-(const ModInt &a) const { return ModInt((val - a.val + M) % M); } inline constexpr ModInt operator*(const ModInt &a) const { return ModInt((val * a.val) % M); } inline constexpr ModInt operator/(const ModInt &a) const { return ModInt((val * a.inv().val) % M); } inline constexpr ModInt &operator=(const ModInt &a) { val = a.val; return *this; } inline constexpr ModInt &operator+=(const ModInt &a) { if ((val += a.val) >= M) val -= M; return *this; } inline constexpr ModInt &operator-=(const ModInt &a) { if (val < a.val) val += M; val -= a.val; return *this; } inline constexpr ModInt &operator*=(const ModInt &a) { (val *= a.val) %= M; return *this; } inline constexpr ModInt &operator/=(const ModInt &a) { (val *= a.inv().val) %= M; return *this; } inline constexpr bool operator==(const ModInt &a) const { return val == a.val; } inline constexpr bool operator!=(const ModInt &a) const { return val != a.val; } inline constexpr ModInt &operator++() { *this += 1; return *this; } inline constexpr ModInt &operator--() { *this -= 1; return *this; } inline constexpr ModInt operator++(int) { ModInt t = *this; *this += 1; return t; } inline constexpr ModInt operator--(int) { ModInt t = *this; *this -= 1; return t; } inline constexpr static ModInt frac(std::int64_t a, std::int64_t b) { return ModInt(a) / ModInt(b); } inline constexpr static ModInt power(std::int64_t n, std::int64_t p) { if (p < 0) return power(n, -p).inv(); ModInt ret = 1, e = n; for (; p; e *= e, p >>= 1) if (p & 1) ret *= e; return ret; } inline constexpr ModInt power(std::int64_t p) const { return power(val, p); } inline constexpr ModInt inv() const { std::int64_t a = val, b = M, u = 1, v = 0; while (b) { std::int64_t t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } u %= M; if (u < 0) u += M; return u; } }; template <std::uint32_t M> ModInt<M> operator-(const ModInt<M> &a) { return M - a.val; } template <std::uint32_t M> ModInt<M> operator+(std::int64_t a, const ModInt<M> &b) { return ModInt<M>(ModInt<M>(a) + b.val); } template <std::uint32_t M> ModInt<M> operator-(std::int64_t a, const ModInt<M> &b) { return ModInt<M>(ModInt<M>(a) - b.val); } template <std::uint32_t M> ModInt<M> operator*(std::int64_t a, const ModInt<M> &b) { return ModInt<M>(ModInt<M>(a) * b.val); } template <std::uint32_t M> ModInt<M> operator/(std::int64_t a, const ModInt<M> &b) { return ModInt<M>(ModInt<M>(a) / b.val); } template <std::uint32_t M> std::istream &operator>>(std::istream &is, ModInt<M> &a) { is >> a.val; return is; } template <std::uint32_t M> std::ostream &operator<<(std::ostream &os, const ModInt<M> &a) { os << a.val; return os; } /** * @attention 使用前にinit関数を呼び出す */ template <typename T> class Combinatorics { public: static std::vector<T> facto; static std::vector<T> ifacto; static void init(int N) { facto.assign(N + 1, 1); ifacto.assign(N + 1, 1); for (int i = 1; i <= N; ++i) { facto[i] = facto[i - 1] * i; } ifacto[N] = facto[N].inv(); for (int i = N - 1; i >= 0; --i) { ifacto[i] = ifacto[i + 1] * (i + 1); } } static T f(int64_t i) { assert(i < facto.size()); return facto[i]; } static T finv(int64_t i) { assert(i < ifacto.size()); return ifacto[i]; } static T P(int64_t n, int64_t k); static T C(int64_t n, int64_t k); static T H(int64_t n, int64_t k); static T stirling_number(int64_t n, int64_t k); static T bell_number(int64_t n, int64_t k); static std::vector<T> bernoulli_number(int64_t n); static T catalan_number(int64_t n); }; template <typename T> std::vector<T> Combinatorics<T>::facto = std::vector<T>(); template <typename T> std::vector<T> Combinatorics<T>::ifacto = std::vector<T>(); template <typename T> T Combinatorics<T>::P(int64_t n, int64_t k) { if (n < k or n < 0 or k < 0) return 0; return f(n) * finv(n - k); } template <typename T> T Combinatorics<T>::C(int64_t n, int64_t k) { if (n < k or n < 0 or k < 0) return 0; return P(n, k) * finv(k); } template <typename T> T Combinatorics<T>::H(int64_t n, int64_t k) { if (n == 0 and k == 0) return 1; return C(n + k - 1, k); } using mint = ModInt<1000000007>; using C = Combinatorics<mint>; int main() { C::init(100000); int N, K; while (cin >> N >> K) { vector<int> a(K); cin >> a; dump(a); vector<vector<mint>> dp(K + 1, vector<mint>(N + 1, 0)); dp[0][0] = 1; REP(i, K) { REPE(j, N) { REPE(x, N) { if (j + x > N) break; dp[i + 1][j + x] += dp[i][j] * C::C(N - x, a[i] - x) * C::finv(x); } } } mint ans = dp[K][N] * C::f(N); cout << ans << endl; } return 0; }
replace
289
291
289
292
TLE
p02808
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) { return s << '<' << P.first << ", " << P.second << '>'; } template <class T> ostream &operator<<(ostream &s, vector<T> P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } #define EACH(i, s) \ for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) template <class T> ostream &operator<<(ostream &s, set<T> P) { EACH(it, P) { s << "<" << *it << "> "; } return s << endl; } template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; } template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } template <class T, class V> typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) { t = v; } template <class T, class V> typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) { for (auto &e : t) fill(e, v); } template <int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; } constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; } constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; } constexpr Fp &operator+=(const Fp &r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp &operator-=(const Fp &r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp &operator*=(const Fp &r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp &operator/=(const Fp &r) noexcept { long long a = r.val, 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); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator==(const Fp &r) const noexcept { return this->val == r.val; } constexpr bool operator!=(const Fp &r) const noexcept { return this->val != r.val; } friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept { return os << x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; // 二項係数ライブラリ template <class T> struct BiCoef { vector<T> fact_, inv_, finv_; constexpr BiCoef() {} constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) { init(n); } constexpr void init(int n) noexcept { fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1); int MOD = fact_[0].getmod(); for (int i = 2; i < n; i++) { fact_[i] = fact_[i - 1] * i; inv_[i] = -inv_[MOD % i] * (MOD / i); finv_[i] = finv_[i - 1] * inv_[i]; } } constexpr T com(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return fact_[n] * finv_[k] * finv_[n - k]; } constexpr T fact(int n) const noexcept { if (n < 0) return 0; return fact_[n]; } constexpr T inv(int n) const noexcept { if (n < 0) return 0; return inv_[n]; } constexpr T finv(int n) const noexcept { if (n < 0) return 0; return finv_[n]; } }; const int MOD = 1000000007; using mint = Fp<MOD>; BiCoef<mint> bc; int N, K; vector<int> a; mint solve() { vector<vector<mint>> dp(K + 1, vector<mint>(N + 1, 0)); dp[0][0] = 1; for (int i = 0; i < K; ++i) { for (int j = 0; j <= N; ++j) { for (int nj = 0; nj <= a[i]; ++nj) { if (j + nj > N) continue; dp[i + 1][j + nj] += dp[i][j] * bc.com(N - j, nj) * bc.com(N - nj, a[i] - nj); } } } return dp[K][N]; } int main() { ios::sync_with_stdio(false); cin.tie(0); bc.init(110000); while (cin >> N >> K) { a.resize(N); for (int i = 0; i < K; ++i) cin >> a[i]; cout << solve() << endl; } }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) { return s << '<' << P.first << ", " << P.second << '>'; } template <class T> ostream &operator<<(ostream &s, vector<T> P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } #define EACH(i, s) \ for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) template <class T> ostream &operator<<(ostream &s, set<T> P) { EACH(it, P) { s << "<" << *it << "> "; } return s << endl; } template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; } template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } template <class T, class V> typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) { t = v; } template <class T, class V> typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) { for (auto &e : t) fill(e, v); } template <int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; } constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; } constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; } constexpr Fp &operator+=(const Fp &r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp &operator-=(const Fp &r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp &operator*=(const Fp &r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp &operator/=(const Fp &r) noexcept { long long a = r.val, 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); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator==(const Fp &r) const noexcept { return this->val == r.val; } constexpr bool operator!=(const Fp &r) const noexcept { return this->val != r.val; } friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept { return os << x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; // 二項係数ライブラリ template <class T> struct BiCoef { vector<T> fact_, inv_, finv_; constexpr BiCoef() {} constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) { init(n); } constexpr void init(int n) noexcept { fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1); int MOD = fact_[0].getmod(); for (int i = 2; i < n; i++) { fact_[i] = fact_[i - 1] * i; inv_[i] = -inv_[MOD % i] * (MOD / i); finv_[i] = finv_[i - 1] * inv_[i]; } } constexpr T com(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return fact_[n] * finv_[k] * finv_[n - k]; } constexpr T fact(int n) const noexcept { if (n < 0) return 0; return fact_[n]; } constexpr T inv(int n) const noexcept { if (n < 0) return 0; return inv_[n]; } constexpr T finv(int n) const noexcept { if (n < 0) return 0; return finv_[n]; } }; const int MOD = 1000000007; using mint = Fp<MOD>; BiCoef<mint> bc; int N, K; vector<int> a; mint solve() { vector<vector<mint>> dp(K + 1, vector<mint>(N + 1, 0)); dp[0][0] = 1; for (int i = 0; i < K; ++i) { for (int j = 0; j <= N; ++j) { for (int nj = 0; nj <= a[i]; ++nj) { if (j + nj > N) continue; dp[i + 1][j + nj] += dp[i][j] * bc.com(N - j, nj) * bc.com(N - nj, a[i] - nj); } } } return dp[K][N]; } int main() { ios::sync_with_stdio(false); cin.tie(0); bc.init(110000); while (cin >> N >> K) { a.resize(K); for (int i = 0; i < K; ++i) cin >> a[i]; cout << solve() << endl; } }
replace
212
213
212
213
0
p02808
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define mod 1000000007 using namespace std; int n, k, a[21]; int dp[21][1005]; int C[1005][1005]; int main() { cin >> n >> k; for (int i = 1; i <= k; i++) cin >> a[i]; C[0][0] = 1; for (int i = 1; i <= n; i++) { C[i][0] = 1; for (int j = 1; j <= i; j++) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod; } dp[0][0] = 1; for (int i = 1; i <= k; i++) { for (int j = 0; j <= n; j++) { if (!dp[i - 1][j]) continue; for (int l = 0; l <= j; l++) { for (int r = 0; r <= a[i] - l; r++) { (dp[i][j + r] += 1ll * C[j][l] * C[n - j][a[i] - l] % mod * C[a[i] - l][r] % mod * dp[i - 1][j] % mod) %= mod; } } } } printf("%d\n", dp[k][n]); return 0; }
#include <bits/stdc++.h> #define mod 1000000007 using namespace std; int n, k, a[21]; int dp[21][1005]; int C[1005][1005]; int main() { cin >> n >> k; for (int i = 1; i <= k; i++) cin >> a[i]; C[0][0] = 1; for (int i = 1; i <= n; i++) { C[i][0] = 1; for (int j = 1; j <= i; j++) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod; } dp[0][0] = 1; for (int i = 1; i <= k; i++) { for (int j = 0; j <= n; j++) { if (!dp[i - 1][j]) continue; for (int l = 0; l <= n - j && l <= a[i]; l++) { (dp[i][j + l] += 1ll * C[n - j][l] * C[n - l][a[i] - l] % mod * dp[i - 1][j] % mod) %= mod; } } } printf("%d\n", dp[k][n]); return 0; }
replace
22
27
22
25
TLE
p02808
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef vector<int> vi; const int maxn = 1005; const int mod = 1e9 + 7; int dp[22][maxn]; int C[maxn][maxn]; void init() { for (int i = 0; i < maxn; 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 main() { init(); int n, k; cin >> n >> k; vi a(n); int tot = 0; for (int i = 1; i <= k; i++) cin >> a[i], tot += a[i]; dp[0][0] = 1; for (int i = 1; i <= k; i++) for (int j = 0; j <= n; j++) for (int l = 0; l <= a[i] && j + l <= n; l++) dp[i][j + l] = (dp[i][j + l] + 1LL * dp[i - 1][j] * C[n - j][l] % mod * C[n - l][a[i] - l] % mod) % mod; cout << dp[k][n] << '\n'; }
#include <bits/stdc++.h> using namespace std; typedef vector<int> vi; const int maxn = 1005; const int mod = 1e9 + 7; int dp[22][maxn]; int C[maxn][maxn]; void init() { for (int i = 0; i < maxn; 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 main() { init(); int n, k; cin >> n >> k; vi a(k + 1); int tot = 0; for (int i = 1; i <= k; i++) cin >> a[i], tot += a[i]; dp[0][0] = 1; for (int i = 1; i <= k; i++) for (int j = 0; j <= n; j++) for (int l = 0; l <= a[i] && j + l <= n; l++) dp[i][j + l] = (dp[i][j + l] + 1LL * dp[i - 1][j] * C[n - j][l] % mod * C[n - l][a[i] - l] % mod) % mod; cout << dp[k][n] << '\n'; }
replace
18
19
18
19
0
p02809
C++
Runtime Error
#include <iostream> #include <set> #include <vector> using namespace std; using ll = long long; template <class T> class RangeMin { private: vector<T> seg; int n; public: RangeMin(const vector<T> &v) : n(v.size()), seg(2 * v.size()) { for (int i = 0; i < n; ++i) seg[i + n] = v[i]; for (int i = n - 1; i > 0; --i) seg[i] = min(seg[2 * i], seg[2 * i + 1]); } T get(int a, int b) const { T res = seg[a + n]; // Change to INF to allow a > b for (a += n, b += n + 1; a < b; a = (a + 1) / 2, b /= 2) { if (a & 1) res = min(res, seg[a]); if (b & 1) res = min(res, seg[b - 1]); } return res; } void set(int i, T v) { seg[i + n] = v; for (i += n; i > 1; i /= 2) seg[i / 2] = min(seg[i], seg[i ^ 1]); } }; bool comp(const vector<int> &a, const vector<int> &b) { if (a.size() != b.size()) return a.size() > b.size(); for (int i = 0; i < a.size(); ++i) { if (a[i] != b[i]) return a[i] < b[i]; } return false; } vector<int> tryAll(vector<int> act, int b, const vector<int> &ban) { vector<int> res = {}; for (auto x : act) { if (b == x) continue; vector<int> off = {x}; vector<int> rem; for (auto v : act) { if (v != x) rem.push_back(v); } vector<int> app = tryAll(rem, ban[x], ban); for (auto v : app) off.push_back(v); if (!comp(res, off)) res = off; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<pair<int, int>> ins(n); for (int i = 0; i < n; ++i) ins[i] = {0, i}; vector<int> ban(n); for (int &p : ban) { cin >> p; --p; --ins[p].first; } RangeMin<pair<int, int>> rm(ins); if (n == 2) { cout << -1 << '\n'; } else { // Lexicographically smallest such arrangement // If some card is banned by all other remaining cards, it must be next // Otherwise, all three-card setups are possible set<int> act; for (int i = 0; i < n; ++i) act.insert(i); vector<int> res(n - 3); for (int i = 0; i < n - 3; ++i) { int j = rm.get(0, n - 1).second; if (-ins[j].first < n - 1 - i) { auto it = act.begin(); if (i > 0 && ban[res[i - 1]] == *it) ++it; j = *it; act.erase(it); } else { act.erase(act.find(j)); } ++ins[ban[j]].first; rm.set(ins[ban[j]].second, ins[ban[j]]); rm.set(j, {n, -1}); res[i] = j; } // Handle last three vector<int> rem; for (auto v : act) rem.push_back(v); int b = (n == 3 ? -1 : ban[res[n - 4]]); vector<int> lst = tryAll(rem, b, ban); for (auto i : res) cout << i + 1 << ' '; for (auto i : lst) cout << i + 1 << ' '; cout << '\n'; } }
#include <iostream> #include <set> #include <vector> using namespace std; using ll = long long; template <class T> class RangeMin { private: vector<T> seg; int n; public: RangeMin(const vector<T> &v) : n(v.size()), seg(2 * v.size()) { for (int i = 0; i < n; ++i) seg[i + n] = v[i]; for (int i = n - 1; i > 0; --i) seg[i] = min(seg[2 * i], seg[2 * i + 1]); } T get(int a, int b) const { T res = seg[a + n]; // Change to INF to allow a > b for (a += n, b += n + 1; a < b; a = (a + 1) / 2, b /= 2) { if (a & 1) res = min(res, seg[a]); if (b & 1) res = min(res, seg[b - 1]); } return res; } void set(int i, T v) { seg[i + n] = v; for (i += n; i > 1; i /= 2) seg[i / 2] = min(seg[i], seg[i ^ 1]); } }; bool comp(const vector<int> &a, const vector<int> &b) { if (a.size() != b.size()) return a.size() > b.size(); for (int i = 0; i < a.size(); ++i) { if (a[i] != b[i]) return a[i] < b[i]; } return false; } vector<int> tryAll(vector<int> act, int b, const vector<int> &ban) { vector<int> res = {}; for (auto x : act) { if (b == x) continue; vector<int> off = {x}; vector<int> rem; for (auto v : act) { if (v != x) rem.push_back(v); } vector<int> app = tryAll(rem, ban[x], ban); for (auto v : app) off.push_back(v); if (!comp(res, off)) res = off; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<pair<int, int>> ins(n); for (int i = 0; i < n; ++i) ins[i] = {0, i}; vector<int> ban(n); for (int &p : ban) { cin >> p; --p; --ins[p].first; } RangeMin<pair<int, int>> rm(ins); if (n == 2) { cout << -1 << '\n'; } else { // Lexicographically smallest such arrangement // If some card is banned by all other remaining cards, it must be next // Otherwise, all three-card setups are possible set<int> act; for (int i = 0; i < n; ++i) act.insert(i); vector<int> res(n - 3); for (int i = 0; i < n - 3; ++i) { int j = rm.get(0, n - 1).second; if (-ins[j].first < n - 1 - i) { auto it = act.begin(); if (i > 0 && ban[res[i - 1]] == *it) ++it; j = *it; act.erase(it); } else { act.erase(act.find(j)); } ++ins[ban[j]].first; if (act.find(ban[j]) != act.end()) { rm.set(ins[ban[j]].second, ins[ban[j]]); } rm.set(j, {n, -1}); res[i] = j; } // Handle last three vector<int> rem; for (auto v : act) rem.push_back(v); int b = (n == 3 ? -1 : ban[res[n - 4]]); vector<int> lst = tryAll(rem, b, ban); for (auto i : res) cout << i + 1 << ' '; for (auto i : lst) cout << i + 1 << ' '; cout << '\n'; } }
replace
110
111
110
113
0
p02809
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; template <class G> class Fenwick_tree { vector<G> a; public: Fenwick_tree(int n) : a(n) {} void add(int i, G val) { for (i++; i <= a.size(); i += i & -i) a[i - 1] += val; } G sum(int l, int r) const { if (l == 0) { G res{}; for (; r > 0; r -= r & -r) res += a[r - 1]; return res; } return sum(0, r) - sum(0, l); } int lower_bound(G val) const { if (val <= G{}) return 0; int x = 0, k; for (k = 1; k <= a.size(); k <<= 1) ; for (k >>= 1; k > 0; k >>= 1) if (x + k <= a.size() && a[x + k - 1] < val) val -= a[x + k - 1], x += k; return x; } int upper_bound(G val) const { if (val < G{}) return 0; int x = 0, k; for (k = 1; k <= a.size(); k <<= 1) ; for (k >>= 1; k > 0; k >>= 1) if (x + k <= a.size() && a[x + k - 1] <= val) val -= a[x + k - 1], x += k; return x; } }; int n, bad[100000]; vector<pair<int, int>> V; Fenwick_tree<int> F(0); int ans[100000]; bool dfs(int i) { if (i == n) return true; // printf("i = %d\n",i); int next = -1; for (auto p : V) { int deg = p.first, x = p.second; if (deg == n - 1 && F.sum(x, x + 1) == 1) { if (next != -1) return false; next = x; } } if (next != -1) { // printf("next = %d\n",next); int x = next; if (i == 0 || bad[ans[i - 1]] != x) { F.add(x, -1); ans[i] = x; for (auto &p : V) if (bad[x] != p.second) p.first++; if (dfs(i + 1)) return true; for (auto &p : V) if (bad[x] != p.second) p.first--; F.add(x, 1); } return false; } for (int p = 1;; p++) { int x = F.lower_bound(p); if (x == n) break; if (i == 0 || bad[ans[i - 1]] != x) { F.add(x, -1); ans[i] = x; for (auto &p : V) if (bad[x] != p.second) p.first++; if (dfs(i + 1)) return true; for (auto &p : V) if (bad[x] != p.second) p.first--; F.add(x, 1); } } return false; } int main() { scanf("%d", &n); rep(i, n) scanf("%d", &bad[i]), bad[i]--; if (n == 2) return puts("-1"), 0; F = Fenwick_tree<int>(n); rep(i, n) F.add(i, 1); vector<int> deg(n); rep(i, n) deg[bad[i]]++; vector<pair<int, int>> p(n); rep(i, n) p[i] = {-deg[i], i}; sort(p.begin(), p.end()); rep(i, min(500, n)) V.emplace_back(-p[i].first, p[i].second); // for(auto q:V) printf("%d %d\n",q.first,q.second); bool ok = dfs(0); if (ok) { rep(i, n) printf("%d%c", ans[i] + 1, i < n - 1 ? ' ' : '\n'); } else { puts("-1"); } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; template <class G> class Fenwick_tree { vector<G> a; public: Fenwick_tree(int n) : a(n) {} void add(int i, G val) { for (i++; i <= a.size(); i += i & -i) a[i - 1] += val; } G sum(int l, int r) const { if (l == 0) { G res{}; for (; r > 0; r -= r & -r) res += a[r - 1]; return res; } return sum(0, r) - sum(0, l); } int lower_bound(G val) const { if (val <= G{}) return 0; int x = 0, k; for (k = 1; k <= a.size(); k <<= 1) ; for (k >>= 1; k > 0; k >>= 1) if (x + k <= a.size() && a[x + k - 1] < val) val -= a[x + k - 1], x += k; return x; } int upper_bound(G val) const { if (val < G{}) return 0; int x = 0, k; for (k = 1; k <= a.size(); k <<= 1) ; for (k >>= 1; k > 0; k >>= 1) if (x + k <= a.size() && a[x + k - 1] <= val) val -= a[x + k - 1], x += k; return x; } }; int n, bad[100000]; vector<pair<int, int>> V; Fenwick_tree<int> F(0); int ans[100000]; bool dfs(int i) { if (i == n) return true; // printf("i = %d\n",i); int next = -1; for (auto p : V) { int deg = p.first, x = p.second; if (deg == n - 1 && F.sum(x, x + 1) == 1) { if (next != -1) return false; next = x; } } if (next != -1) { // printf("next = %d\n",next); int x = next; if (i == 0 || bad[ans[i - 1]] != x) { F.add(x, -1); ans[i] = x; for (auto &p : V) if (bad[x] != p.second) p.first++; if (dfs(i + 1)) return true; for (auto &p : V) if (bad[x] != p.second) p.first--; F.add(x, 1); } return false; } for (int p = 1;; p++) { int x = F.lower_bound(p); if (x == n) break; if (i == 0 || bad[ans[i - 1]] != x) { F.add(x, -1); ans[i] = x; for (auto &p : V) if (bad[x] != p.second) p.first++; if (dfs(i + 1)) return true; for (auto &p : V) if (bad[x] != p.second) p.first--; F.add(x, 1); } } return false; } int main() { scanf("%d", &n); rep(i, n) scanf("%d", &bad[i]), bad[i]--; if (n == 2) return puts("-1"), 0; F = Fenwick_tree<int>(n); rep(i, n) F.add(i, 1); vector<int> deg(n); rep(i, n) deg[bad[i]]++; vector<pair<int, int>> p(n); rep(i, n) p[i] = {-deg[i], i}; sort(p.begin(), p.end()); rep(i, min(300, n)) V.emplace_back(-p[i].first, p[i].second); // for(auto q:V) printf("%d %d\n",q.first,q.second); bool ok = dfs(0); if (ok) { rep(i, n) printf("%d%c", ans[i] + 1, i < n - 1 ? ' ' : '\n'); } else { puts("-1"); } return 0; }
replace
125
126
125
126
TLE
p02809
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define vv(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c)) #define vvi vector<vector<int>> #define vvl vector<vector<ll>> #define vll vector<ll> #define rep(c, a, b) for (ll c = a; c < b; c++) #define re(c, b) for (ll c = 0; c < b; c++) #define all(obj) (obj).begin(), (obj).end() typedef long long int ll; typedef long double ld; using namespace std; //------------------------------------------------------------------- struct segtree_in { ll M = 1; vector<vll> dat; segtree_in(ll N, vll num) { while (M < N) M *= 2; for (int i = 0; i < M * 2 - 1; i++) dat.push_back(num); } void update(vll x, int k, int l = 0, int r = -1) { if (r == -1) r = M; k += M - 1; dat[k] = x; while (k > 0) { k = (k - 1) / 2; dat[k] = (dat[k * 2 + 1][0] > dat[k * 2 + 2][0] ? dat[k * 2 + 1] : dat[k * 2 + 2]); } } vll query(int a, int b = -1, int k = 0, int l = 0, int r = -1) { if (r == -1) r = M; if (b == -1) b = M; if (r <= a || b <= l) return vll{-1000000000, -1}; if (a <= l && r <= b) return dat[k]; vll A = query(a, b, k * 2 + 1, l, (l + r) / 2); vll B = query(a, b, k * 2 + 2, (l + r) / 2, r); return (A[0] > B[0] ? A : B); } }; //------------------------------------------------------------------- int main(int argc, char const *argv[]) { ll n; std::cin >> n; vll a(n + 1), uke(n + 1, 0), ans; re(i, n) { std::cin >> a[i + 1]; uke[a[i + 1]]++; } segtree_in segin(n + 1, vll{-1000000000, -1}); set<ll> s; rep(i, 1, n + 1) { segin.update(vll{uke[i], i}, i); s.insert(i); } if (n == 2) { std::cout << -1 << '\n'; return 0; } for (int i = 0; i < n; i++) { vll k = segin.query(1, n + 1); ll p; if (k[0] == (n - 1 - i) && (i == 0 || a[ans[i - 1]] != k[1])) p = k[1]; else { auto itr = s.begin(); if (i != 0 && a[ans[i - 1]] == *itr) itr++; if (itr == s.end()) while (true) n++; p = *itr; } segin.update(vll{-1000000000, -1}, p); if (i > 0) { ll to = a[ans[i - 1]]; vll g = segin.query(to, to + 1); segin.update(vll{g[0] - 1, to}, to); } ans.push_back(p); s.erase(p); } re(i, n) std::cout << ans[i] << (i == n - 1 ? "\n" : " "); return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define vv(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c)) #define vvi vector<vector<int>> #define vvl vector<vector<ll>> #define vll vector<ll> #define rep(c, a, b) for (ll c = a; c < b; c++) #define re(c, b) for (ll c = 0; c < b; c++) #define all(obj) (obj).begin(), (obj).end() typedef long long int ll; typedef long double ld; using namespace std; //------------------------------------------------------------------- struct segtree_in { ll M = 1; vector<vll> dat; segtree_in(ll N, vll num) { while (M < N) M *= 2; for (int i = 0; i < M * 2 - 1; i++) dat.push_back(num); } void update(vll x, int k, int l = 0, int r = -1) { if (r == -1) r = M; k += M - 1; dat[k] = x; while (k > 0) { k = (k - 1) / 2; dat[k] = (dat[k * 2 + 1][0] > dat[k * 2 + 2][0] ? dat[k * 2 + 1] : dat[k * 2 + 2]); } } vll query(int a, int b = -1, int k = 0, int l = 0, int r = -1) { if (r == -1) r = M; if (b == -1) b = M; if (r <= a || b <= l) return vll{-1000000000, -1}; if (a <= l && r <= b) return dat[k]; vll A = query(a, b, k * 2 + 1, l, (l + r) / 2); vll B = query(a, b, k * 2 + 2, (l + r) / 2, r); return (A[0] > B[0] ? A : B); } }; //------------------------------------------------------------------- int main(int argc, char const *argv[]) { ll n; std::cin >> n; vll a(n + 1), uke(n + 1, 0), ans; re(i, n) { std::cin >> a[i + 1]; uke[a[i + 1]]++; } segtree_in segin(n + 1, vll{-1000000000, -1}); set<ll> s; rep(i, 1, n + 1) { segin.update(vll{uke[i], i}, i); s.insert(i); } if (n == 2) { std::cout << -1 << '\n'; return 0; } for (int i = 0; i < n; i++) { vll k = segin.query(1, n + 1); ll p; if (k[0] == (n - 1 - i) && (i == 0 || a[ans[i - 1]] != k[1])) p = k[1]; else { if (s.size() == 3) { for (auto t = s.begin(); t != s.end(); t++) { p = *t; ll A = 10000000000, B = 0; for (auto x = s.begin(); x != s.end(); x++) if (x != t) A = min(A, *x), B = max(B, *x); if ((a[A] == B && a[B] == A) || (i != 0 && a[ans[i - 1]] == p)) continue; break; } } else { auto itr = s.begin(); if (i != 0 && a[ans[i - 1]] == *itr) itr++; p = *itr; } } segin.update(vll{-1000000000, -1}, p); if (i > 0) { ll to = a[ans[i - 1]]; vll g = segin.query(to, to + 1); segin.update(vll{g[0] - 1, to}, to); } ans.push_back(p); s.erase(p); } re(i, n) std::cout << ans[i] << (i == n - 1 ? "\n" : " "); return 0; }
replace
84
91
84
101
TLE
p02809
Python
Runtime Error
import sys from collections import defaultdict from heapq import heappop, heappush from itertools import permutations from operator import itemgetter # ある条件に当てはまらない限り、i番目に置く数字 xi は、 # それまで使ってない中で最も小さい数字か、 # またはその次に小さい数字(x[i-1]の右に最も小さい数字を置けない場合) # # ある条件: 以下の条件を満たす、未使用の数 k がある # 残っているk以外の全ての数字が、kを共通して右側に置けない数として指定している # =kを先頭に持ってこない限り、kを置ける機会が無い # # ただし残りが少なく(3以下)なってくると例外的なものが出てくるため、それ以降は全探索 def fill_remainings(ans, aaa, x, remainings): """ xを先頭にして残りを昇順に追加 ただしxの次の要素のみ、aaa[x]で禁止されていた場合はその次と入れ替える remainingsにはxを含め3要素以上残っていることが前提 """ ans.append(x) i = len(ans) while remainings: k = heappop(remainings) if k != x: ans.append(k) if aaa[x] == ans[i]: ans[i], ans[i + 1] = ans[i + 1], ans[i] def solve(n, aaa): if n == 2: return [-1] in_degrees = defaultdict(lambda: 0) for i, a in enumerate(aaa, start=1): in_degrees[a] += 1 in_degrees = dict(in_degrees) # 少なくとも残り個数がこれ+1になるまでは「ある条件」には当てはまらない # ただし減少することはあるため、直前に再チェック必要 curr_max = max(in_degrees.values()) remainings = list(range(1, n + 1)) aaa.insert(0, 0) ans = [] banned = -1 for i in range(n - 3): if curr_max == n - i - 1: curr_x, curr_max = max(in_degrees.items(), key=itemgetter(1)) if curr_max == n - i - 1: fill_remainings(ans, aaa, curr_x, remainings) return ans top = heappop(remainings) if top == banned: ans.append(heappop(remainings)) heappush(remainings, top) else: ans.append(top) banned = aaa[ans[-1]] # 確定した数字の入り次数を削減 if in_degrees[banned] == 1: del in_degrees[banned] else: in_degrees[banned] -= 1 in_degrees.pop(ans[-1], 0) remainings.sort() for i, j, k in permutations(remainings): if i != banned and j != aaa[i] and k != aaa[j]: ans += [i, j, k] break return ans n, *aaa = map(int, sys.stdin.buffer.read().split()) print(*solve(n, aaa))
import sys from collections import defaultdict from heapq import heappop, heappush from itertools import permutations from operator import itemgetter # ある条件に当てはまらない限り、i番目に置く数字 xi は、 # それまで使ってない中で最も小さい数字か、 # またはその次に小さい数字(x[i-1]の右に最も小さい数字を置けない場合) # # ある条件: 以下の条件を満たす、未使用の数 k がある # 残っているk以外の全ての数字が、kを共通して右側に置けない数として指定している # =kを先頭に持ってこない限り、kを置ける機会が無い # # ただし残りが少なく(3以下)なってくると例外的なものが出てくるため、それ以降は全探索 def fill_remainings(ans, aaa, x, remainings): """ xを先頭にして残りを昇順に追加 ただしxの次の要素のみ、aaa[x]で禁止されていた場合はその次と入れ替える remainingsにはxを含め3要素以上残っていることが前提 """ ans.append(x) i = len(ans) while remainings: k = heappop(remainings) if k != x: ans.append(k) if aaa[x] == ans[i]: ans[i], ans[i + 1] = ans[i + 1], ans[i] def solve(n, aaa): if n == 2: return [-1] in_degrees = defaultdict(lambda: 0) for i, a in enumerate(aaa, start=1): in_degrees[a] += 1 in_degrees = dict(in_degrees) # 少なくとも残り個数がこれ+1になるまでは「ある条件」には当てはまらない # ただし減少することはあるため、直前に再チェック必要 curr_max = max(in_degrees.values()) remainings = list(range(1, n + 1)) aaa.insert(0, 0) ans = [] banned = -1 for i in range(n - 3): if curr_max == n - i - 1: curr_x, curr_max = max(in_degrees.items(), key=itemgetter(1)) if curr_max == n - i - 1: fill_remainings(ans, aaa, curr_x, remainings) return ans top = heappop(remainings) if top == banned: ans.append(heappop(remainings)) heappush(remainings, top) else: ans.append(top) banned = aaa[ans[-1]] # 確定した数字の入り次数を削減 if banned in in_degrees: if in_degrees[banned] == 1: del in_degrees[banned] else: in_degrees[banned] -= 1 in_degrees.pop(ans[-1], 0) remainings.sort() for i, j, k in permutations(remainings): if i != banned and j != aaa[i] and k != aaa[j]: ans += [i, j, k] break return ans n, *aaa = map(int, sys.stdin.buffer.read().split()) print(*solve(n, aaa))
replace
67
71
67
72
0
p02809
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long #define f(i, x, n) for (int i = x; i < (int)(n); ++i) int const N = 100000; int a[N + 1], an[N + 1], fr[N + 1], n; set<pair<int, int>> st; set<int> lft; void rm(int t) { lft.erase(t); st.erase(make_pair(fr[a[t]], a[t])); --fr[a[t]]; st.insert(make_pair(fr[a[t]], a[t])); } void ad(int t) { lft.insert(t); st.erase(make_pair(fr[a[t]], a[t])); ++fr[a[t]]; st.insert(make_pair(fr[a[t]], a[t])); } bool go(int i = 1, int bad = 0) { if (i == n) { int t = *lft.begin(); if (t == bad) return false; an[i] = t; return true; } int m = n - i + 1; int t = st.begin()->second; if (fr[t] == m - 1) { an[i] = t; rm(t); bool ok = go(i + 1, a[t]); ad(t); return ok; } vector<int> v; int z = 5; for (auto it = lft.begin(); it != lft.end() && z; ++it, --z) v.push_back(*it); for (int u : v) if (bad != u) { rm(u); an[i] = u; bool ok = go(i + 1, a[u]); ad(u); if (ok) return true; } return false; } int main() { scanf("%d", &n); if (n == 2) { printf("-1\n"); return 0; } f(i, 1, n + 1) scanf("%d", a + i), ++fr[a[i]]; f(i, 1, n + 1) st.insert(make_pair(fr[i], i)); f(i, 1, n + 1) lft.insert(i); go(); printf("%d", an[1]); f(i, 2, n + 1) printf(" %d", an[i]); printf("\n"); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define f(i, x, n) for (int i = x; i < (int)(n); ++i) int const N = 100000; int a[N + 1], an[N + 1], fr[N + 1], n; set<pair<int, int>> st; set<int> lft; void rm(int t) { lft.erase(t); st.erase(make_pair(fr[a[t]], a[t])); --fr[a[t]]; st.insert(make_pair(fr[a[t]], a[t])); } void ad(int t) { lft.insert(t); st.erase(make_pair(fr[a[t]], a[t])); ++fr[a[t]]; st.insert(make_pair(fr[a[t]], a[t])); } bool go(int i = 1, int bad = 0) { if (i == n) { int t = *lft.begin(); if (t == bad) return false; an[i] = t; return true; } int m = n - i + 1; int t = st.rbegin()->second; if (fr[t] == m - 1 && lft.find(t) != lft.end()) { an[i] = t; rm(t); bool ok = go(i + 1, a[t]); ad(t); return ok; } vector<int> v; int z = 5; for (auto it = lft.begin(); it != lft.end() && z; ++it, --z) v.push_back(*it); for (int u : v) if (bad != u) { rm(u); an[i] = u; bool ok = go(i + 1, a[u]); ad(u); if (ok) return true; } return false; } int main() { scanf("%d", &n); if (n == 2) { printf("-1\n"); return 0; } f(i, 1, n + 1) scanf("%d", a + i), ++fr[a[i]]; f(i, 1, n + 1) st.insert(make_pair(fr[i], i)); f(i, 1, n + 1) lft.insert(i); go(); printf("%d", an[1]); f(i, 2, n + 1) printf(" %d", an[i]); printf("\n"); }
replace
33
35
33
35
TLE
p02809
C++
Runtime Error
#include <bits/stdc++.h> #define XX first #define YY second #define pb emplace_back #define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i)) #define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i)) #define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X)) #define REP rep #define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X)) #define all(X) (X).begin(), (X).end() #define eb emplace_back using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef ll LL; typedef pii PII; typedef pll PLL; const ll MOD = 1e9 + 7; #define rall(X) (X).rbegin(), (X).rend() #define UNIQUE(X) (X).erase(unique(all(X)), (X).end()) #define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X)) #define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X)) template <class T> inline bool MX(T &l, const T &r) { return l < r ? l = r, 1 : 0; } template <class T> inline bool MN(T &l, const T &r) { return l > r ? l = r, 1 : 0; } int N; vector<int> as; int cnt[114514]; set<int> ls[114514]; signed main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(0); cin >> N; rep(i, N) { int a; cin >> a; --a; as.eb(a); cnt[a]++; } if (N == 2) { cout << "-1\n"; return 0; } set<int> cand; rep(i, N) { cand.insert(i); ls[cnt[i]].insert(i); } vector<int> ans; auto Del = [&](int idx) { ans.eb(idx); cand.erase(idx); assert(ls[cnt[idx]].count(idx)); ls[cnt[idx]].erase(idx); int a = as[idx]; if (cand.count(a)) { assert(ls[cnt[a]].count(a)); ls[cnt[a]].erase(a); cnt[a]--; assert(cnt[a] >= 0); ls[cnt[a]].insert(a); } }; rep(i, N) { assert(cand.size() + i == N); int rem = N - 1 - i; if (!ls[rem].empty()) { int idx = *ls[rem].begin(); Del(idx); continue; } int idx = *cand.begin(); if (!ans.empty() && as[ans.back()] == idx) { assert(cand.size() > 1); auto itr = cand.begin(); ++itr; idx = *itr; } Del(idx); } assert(cand.empty() && ans.size() == N); if (as[ans[N - 2]] == ans[N - 1]) { swap(ans[N - 3], ans[N - 2]); } rep(i, N) { if (i) cout << " "; cout << ans[i] + 1; } cout << endl; rep(i, N - 1) assert(ans[i + 1] != as[ans[i]]); }
#include <bits/stdc++.h> #define XX first #define YY second #define pb emplace_back #define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i)) #define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i)) #define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X)) #define REP rep #define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X)) #define all(X) (X).begin(), (X).end() #define eb emplace_back using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef ll LL; typedef pii PII; typedef pll PLL; const ll MOD = 1e9 + 7; #define rall(X) (X).rbegin(), (X).rend() #define UNIQUE(X) (X).erase(unique(all(X)), (X).end()) #define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X)) #define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X)) template <class T> inline bool MX(T &l, const T &r) { return l < r ? l = r, 1 : 0; } template <class T> inline bool MN(T &l, const T &r) { return l > r ? l = r, 1 : 0; } int N; vector<int> as; int cnt[114514]; set<int> ls[114514]; signed main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(0); cin >> N; rep(i, N) { int a; cin >> a; --a; as.eb(a); cnt[a]++; } if (N == 2) { cout << "-1\n"; return 0; } set<int> cand; rep(i, N) { cand.insert(i); ls[cnt[i]].insert(i); } vector<int> ans; auto Del = [&](int idx) { ans.eb(idx); cand.erase(idx); assert(ls[cnt[idx]].count(idx)); ls[cnt[idx]].erase(idx); int a = as[idx]; if (cand.count(a)) { assert(ls[cnt[a]].count(a)); ls[cnt[a]].erase(a); cnt[a]--; assert(cnt[a] >= 0); ls[cnt[a]].insert(a); } }; rep(i, N) { assert(cand.size() + i == N); int rem = N - 1 - i; if (!ls[rem].empty()) { int idx = *ls[rem].begin(); Del(idx); continue; } int idx = *cand.begin(); if (!ans.empty() && as[ans.back()] == idx) { assert(cand.size() > 1); auto itr = cand.begin(); ++itr; idx = *itr; } Del(idx); } assert(cand.empty() && ans.size() == N); if (as[ans[N - 2]] == ans[N - 1]) { if (as[ans[N - 4]] == ans[N - 2]) { swap(ans[N - 3], ans[N - 1]); swap(ans[N - 2], ans[N - 1]); } else { swap(ans[N - 3], ans[N - 2]); } } rep(i, N) { if (i) cout << " "; cout << ans[i] + 1; } cout << endl; rep(i, N - 1) assert(ans[i + 1] != as[ans[i]]); }
replace
97
98
97
103
0
p02809
C++
Runtime Error
/** * author: otera **/ #include <algorithm> #include <bitset> #include <cassert> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; // #define int long long typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; typedef long double ld; const int inf = 1e9 + 7; const ll INF = 1LL << 60; const ll mod = 1e9 + 7; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) typedef complex<ld> Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<int, int> P; typedef pair<ld, ld> LDP; typedef pair<ll, ll> LP; #define fr first #define sc second #define all(c) c.begin(), c.end() #define pb push_back #define debug(x) cerr << #x << " = " << (x) << endl; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } void fail() { cout << -1 << endl; exit(0); } void out(vector<int> x) { rep(i, (int)x.size()) { if (i) cout << " "; cout << x[i] + 1; } cout << endl; exit(0); } void solve() { int n; cin >> n; vector<int> a(n); rep(i, n) { cin >> a[i]; --a[i]; } set<int> se; for (int i = 1; i < n; ++i) se.insert(i); vector<int> ans(1, 0); for (int i = 1; i < n - 1; ++i) { auto ite = se.begin(); if (*ite == a[ans.back()]) ++ite; ans.push_back(*ite); se.erase(ite); } int b = *se.begin(); if (b != a[ans.back()]) { ans.push_back(b); out(ans); } set<int> pool; while (ans.size() and a[ans.back()] == b) { pool.insert(ans.back()); ans.pop_back(); } if (pool.size() == 1) { vector<int> p; p.push_back(b); p.push_back(*pool.begin()); p.push_back(ans.back()); ans.pop_back(); sort(all(p)); auto tmp = ans; vector<int> res = {n + 1}; do { for (int x : p) tmp.push_back(x); bool ok = 1; rep(i, n - 1) { if (tmp[i + 1] == a[tmp[i]]) ok = 0; } if (ok) chmin(res, tmp); rep(i, 3) tmp.pop_back(); } while (next_permutation(all(p))); ans = res; } else { ans.push_back(b); while (pool.size()) { auto ite = pool.begin(); if (*ite == a[ans.back()]) ++ite; ans.push_back(*ite); pool.erase(ite); } } out(ans); } signed main() { ios::sync_with_stdio(false); cin.tie(0); // cout << fixed << setprecision(10); // int t; cin >> t; rep(i, t)solve(); solve(); return 0; }
/** * author: otera **/ #include <algorithm> #include <bitset> #include <cassert> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; // #define int long long typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; typedef long double ld; const int inf = 1e9 + 7; const ll INF = 1LL << 60; const ll mod = 1e9 + 7; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) typedef complex<ld> Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<int, int> P; typedef pair<ld, ld> LDP; typedef pair<ll, ll> LP; #define fr first #define sc second #define all(c) c.begin(), c.end() #define pb push_back #define debug(x) cerr << #x << " = " << (x) << endl; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } void fail() { cout << -1 << endl; exit(0); } void out(vector<int> x) { rep(i, (int)x.size()) { if (i) cout << " "; cout << x[i] + 1; } cout << endl; exit(0); } void solve() { int n; cin >> n; vector<int> a(n); rep(i, n) { cin >> a[i]; --a[i]; } set<int> se; for (int i = 1; i < n; ++i) se.insert(i); vector<int> ans(1, 0); for (int i = 1; i < n - 1; ++i) { auto ite = se.begin(); if (*ite == a[ans.back()]) ++ite; ans.push_back(*ite); se.erase(ite); } int b = *se.begin(); if (b != a[ans.back()]) { ans.push_back(b); out(ans); } set<int> pool; while (ans.size() and a[ans.back()] == b) { pool.insert(ans.back()); ans.pop_back(); } if (pool.size() == 1) { if (n == 2) fail(); vector<int> p; p.push_back(b); p.push_back(*pool.begin()); p.push_back(ans.back()); ans.pop_back(); sort(all(p)); auto tmp = ans; vector<int> res = {n + 1}; do { for (int x : p) tmp.push_back(x); bool ok = 1; rep(i, n - 1) { if (tmp[i + 1] == a[tmp[i]]) ok = 0; } if (ok) chmin(res, tmp); rep(i, 3) tmp.pop_back(); } while (next_permutation(all(p))); ans = res; } else { ans.push_back(b); while (pool.size()) { auto ite = pool.begin(); if (*ite == a[ans.back()]) ++ite; ans.push_back(*ite); pool.erase(ite); } } out(ans); } signed main() { ios::sync_with_stdio(false); cin.tie(0); // cout << fixed << setprecision(10); // int t; cin >> t; rep(i, t)solve(); solve(); return 0; }
insert
111
111
111
113
0
p02809
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define fr(i, n) for (int i = 0; i < (n); ++i) #define Fr(i, n) for (int i = 1; i <= (n); ++i) #define ifr(i, n) for (int i = (n)-1; i >= 0; --i) #define iFr(i, n) for (int i = (n); i > 0; --i) template <size_t n, class T> struct heap { size_t sz; T e; T a[n + 1]; bool v[n + 1]; size_t hp[n + 1], idx[n + 1]; explicit heap(T e = numeric_limits<T>::max()) : sz{}, e(e), a(), v(), hp(), idx() { fill(v + 1, v + 1 + n, false); fill(a + 1, a + 1 + n, e); fill(hp + 1, hp + 1 + n, -1u); fill(idx + 1, idx + 1 + n, -1u); } void prioritize(size_t i, T x) { size_t s = idx[i]; if (a[i] < x) { while ((s << 1u) <= sz) { if ((s << 1u) == sz) { if (x > a[hp[s << 1u]]) { hp[s] = hp[s << 1u]; idx[hp[s]] = s; s <<= 1u; } break; } if (x <= min(a[hp[s << 1u]], a[hp[(s << 1u) ^ 1u]])) break; if ((s << 1u) < sz && a[hp[s << 1u]] > a[hp[(s << 1u) ^ 1u]]) { hp[s] = hp[(s << 1u) ^ 1u]; idx[hp[s]] = s; s <<= 1u; s ^= 1u; } else { hp[s] = hp[s << 1u]; idx[hp[s]] = s; s <<= 1u; } } } else { while (s > 1 && x < a[hp[s >> 1u]]) { hp[s] = hp[s >> 1u]; idx[hp[s]] = s; s >>= 1u; } } a[i] = x; hp[s] = i; idx[i] = s; } void Push(size_t i, T x) { if (!v[i]) { v[i] = true; ++sz; hp[sz] = i; idx[i] = sz; } prioritize(i, x); } void push(size_t i, T x) { Push(i + 1, x); } void pop() { const size_t t = hp[1]; if (sz == 1) { hp[1] = -1u; a[t] = e; idx[t] = -1u; --sz; return; } idx[t] = -1u; idx[hp[sz]] = 1; hp[1] = hp[sz]; hp[sz] = -1u; T x = a[hp[1]]; a[hp[1]] = a[t]; a[t] = e; --sz; v[t] = false; prioritize(hp[1], x); } T pri(size_t i) { return a[i + 1]; } T top() { return hp[1] - 1; } void erase(size_t i) { prioritize(i, numeric_limits<T>::min()); pop(); } bool find(size_t i) { return v[i + 1]; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); istream &in(cin); ostream &out(cout); int n; in >> n; if (n == 2) return cout << -1 << endl, 0; heap<100000, int> hp; vector<int> a(n), c(n); for (auto &i : a) in >> i, --c[--i]; fr(i, n) hp.push(i, c[i]); vector<int> ans(n - 3); set<int> st; fr(i, n) st.insert(i); int ng = -1; fr(i, n - 3) { int j = hp.top(); if (-hp.pri(j) == n - 1 - i) { ans[i] = j; hp.pop(); hp.push(a[j], hp.pri(j) + 1); st.erase(j); ng = a[j]; } else { auto it = st.begin(); j = *it; ++it; int J = *it; if (j == ng) j = J; st.erase(j); hp.erase(j); ng = a[j]; ans[i] = j; if (hp.find(a[j])) hp.push(a[j], hp.pri(a[j]) + 1); } } auto it = st.begin(); vector<int> p(3), P = {0, 1, 2}; fr(i, 3) p[i] = *(it++); do { if (p[P[0]] == ng) continue; if (a[p[P[0]]] == p[P[1]]) continue; if (a[p[P[1]]] == p[P[2]]) continue; fr(i, 3) ans.emplace_back(p[P[i]]); break; } while (next_permutation(P.begin(), P.end())); fr(i, n) cout << ans[i] + 1 << " "; cout << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define fr(i, n) for (int i = 0; i < (n); ++i) #define Fr(i, n) for (int i = 1; i <= (n); ++i) #define ifr(i, n) for (int i = (n)-1; i >= 0; --i) #define iFr(i, n) for (int i = (n); i > 0; --i) template <size_t n, class T> struct heap { size_t sz; T e; T a[n + 1]; bool v[n + 1]; size_t hp[n + 1], idx[n + 1]; explicit heap(T e = numeric_limits<T>::max()) : sz{}, e(e), a(), v(), hp(), idx() { fill(v + 1, v + 1 + n, false); fill(a + 1, a + 1 + n, e); fill(hp + 1, hp + 1 + n, -1u); fill(idx + 1, idx + 1 + n, -1u); } void prioritize(size_t i, T x) { size_t s = idx[i]; if (a[i] < x) { while ((s << 1u) <= sz) { if ((s << 1u) == sz) { if (x > a[hp[s << 1u]]) { hp[s] = hp[s << 1u]; idx[hp[s]] = s; s <<= 1u; } break; } if (x <= min(a[hp[s << 1u]], a[hp[(s << 1u) ^ 1u]])) break; if ((s << 1u) < sz && a[hp[s << 1u]] > a[hp[(s << 1u) ^ 1u]]) { hp[s] = hp[(s << 1u) ^ 1u]; idx[hp[s]] = s; s <<= 1u; s ^= 1u; } else { hp[s] = hp[s << 1u]; idx[hp[s]] = s; s <<= 1u; } } } else { while (s > 1 && x < a[hp[s >> 1u]]) { hp[s] = hp[s >> 1u]; idx[hp[s]] = s; s >>= 1u; } } a[i] = x; hp[s] = i; idx[i] = s; } void Push(size_t i, T x) { if (!v[i]) { v[i] = true; ++sz; hp[sz] = i; idx[i] = sz; } prioritize(i, x); } void push(size_t i, T x) { Push(i + 1, x); } void pop() { const size_t t = hp[1]; if (sz == 1) { hp[1] = -1u; a[t] = e; idx[t] = -1u; --sz; return; } idx[t] = -1u; idx[hp[sz]] = 1; hp[1] = hp[sz]; hp[sz] = -1u; T x = a[hp[1]]; a[hp[1]] = a[t]; a[t] = e; --sz; v[t] = false; prioritize(hp[1], x); } T pri(size_t i) { return a[i + 1]; } T top() { return hp[1] - 1; } void erase(size_t i) { if (!find(i)) return; prioritize(i + 1, numeric_limits<T>::min()); pop(); } bool find(size_t i) { return v[i + 1]; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); istream &in(cin); ostream &out(cout); int n; in >> n; if (n == 2) return cout << -1 << endl, 0; heap<100000, int> hp; vector<int> a(n), c(n); for (auto &i : a) in >> i, --c[--i]; fr(i, n) hp.push(i, c[i]); vector<int> ans(n - 3); set<int> st; fr(i, n) st.insert(i); int ng = -1; fr(i, n - 3) { int j = hp.top(); if (-hp.pri(j) == n - 1 - i) { ans[i] = j; hp.pop(); hp.push(a[j], hp.pri(j) + 1); st.erase(j); ng = a[j]; } else { auto it = st.begin(); j = *it; ++it; int J = *it; if (j == ng) j = J; st.erase(j); hp.erase(j); ng = a[j]; ans[i] = j; if (hp.find(a[j])) hp.push(a[j], hp.pri(a[j]) + 1); } } auto it = st.begin(); vector<int> p(3), P = {0, 1, 2}; fr(i, 3) p[i] = *(it++); do { if (p[P[0]] == ng) continue; if (a[p[P[0]]] == p[P[1]]) continue; if (a[p[P[1]]] == p[P[2]]) continue; fr(i, 3) ans.emplace_back(p[P[i]]); break; } while (next_permutation(P.begin(), P.end())); fr(i, n) cout << ans[i] + 1 << " "; cout << endl; }
replace
90
91
90
93
0
p02809
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int const maxn = 1e5 + 10; int N, a[maxn], b[maxn]; int main() { scanf("%d", &N); int deg[maxn] = {}; for (int i = 1; i <= N; i++) { scanf("%d", &a[i]); deg[a[i]]++; } bool vis[maxn] = {}; function<void(int, int)> dfs = [&](int cur, int mn) { if (cur == N + 1) { for (int i = 1; i <= N; i++) printf("%d ", b[i]); putchar('\n'); exit(0); } if (cur > 1 && !vis[a[b[cur - 1]]] && deg[a[b[cur - 1]]] == N - cur + 1) return; while (vis[mn]) mn++; for (int v = mn; v <= N; v++) { if (vis[v] || (cur > 1 && v == a[b[cur - 1]])) continue; b[cur] = v; vis[v] = true; deg[a[v]]--; dfs(cur + 1, v == mn ? mn + 1 : mn); deg[a[v]]++; vis[v] = false; } }; dfs(1, 1); puts("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; int const maxn = 1e5 + 10; int N, a[maxn], b[maxn]; int main() { scanf("%d", &N); int deg[maxn] = {}; for (int i = 1; i <= N; i++) { scanf("%d", &a[i]); deg[a[i]]++; } bool vis[maxn] = {}; function<void(int, int)> dfs = [&](int cur, int mn) { if (cur == N + 1) { for (int i = 1; i <= N; i++) printf("%d ", b[i]); putchar('\n'); exit(0); } if (cur > 1 && !vis[a[b[cur - 1]]] && deg[a[b[cur - 1]]] == N - cur) return; while (vis[mn]) mn++; for (int v = mn; v <= N; v++) { if (vis[v] || (cur > 1 && v == a[b[cur - 1]])) continue; b[cur] = v; vis[v] = true; deg[a[v]]--; dfs(cur + 1, v == mn ? mn + 1 : mn); deg[a[v]]++; vis[v] = false; } }; dfs(1, 1); puts("-1"); return 0; }
replace
25
26
25
26
TLE
p02809
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; // #define int long long typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = (1e+18) + 7; typedef pair<int, int> P; #define stop \ char nyaa; \ cin >> nyaa; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) #define all(v) (v).begin(), (v).end() typedef pair<ll, ll> LP; typedef long double ld; typedef pair<ld, ld> LDP; const ld eps = 1e-6; const ld pi = acos(-1.0); // typedef vector<vector<ll>> mat; typedef vector<int> vec; ll mod_pow(ll a, ll n) { ll res = 1; while (n) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } struct modint { ll n; modint() : n(0) { ; } modint(ll m) : n(m) { if (n >= mod) n %= mod; else if (n < 0) n = (n % mod + mod) % mod; } operator int() { return n; } }; bool operator==(modint a, modint b) { return a.n == b.n; } modint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod) a.n -= mod; return a; } modint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0) a.n += mod; return a; } modint operator*=(modint &a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; } modint operator+(modint a, modint b) { return a += b; } modint operator-(modint a, modint b) { return a -= b; } modint operator*(modint a, modint b) { return a *= b; } modint operator^(modint a, int n) { if (n == 0) return modint(1); modint res = (a * a) ^ (n / 2); if (n % 2) res = res * a; return res; } ll inv(ll a, ll p) { return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p); } modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); } const int max_n = 1 << 20; modint fact[max_n], factinv[max_n]; void init() { fact[0] = modint(1); for (int i = 0; i < max_n - 1; i++) { fact[i + 1] = fact[i] * modint(i + 1); } factinv[max_n - 1] = modint(1) / fact[max_n - 1]; for (int i = max_n - 2; i >= 0; i--) { factinv[i] = factinv[i + 1] * modint(i + 1); } } modint comb(int a, int b) { if (a < 0 || b < 0 || a < b) return 0; return fact[a] * factinv[b] * factinv[a - b]; } int a[1 << 17]; vector<int> calc(vector<int> v, int pre) { sort(v.begin(), v.end()); while (true) { bool valid = true; if (v[0] == pre) valid = false; rep(i, v.size() - 1) { if (v[i + 1] == a[v[i]]) valid = false; } if (valid) return v; if (!next_permutation(v.begin(), v.end())) break; } return {}; } modint dp[1001]; modint cop[1001]; void solve() { int n; cin >> n; rep(i, n) { cin >> a[i]; a[i]--; } if (n == 2) { cout << -1 << endl; return; } if (n <= 6) { vector<int> ori(n); rep(i, n) ori[i] = i; vector<int> ans = calc(ori, -1); rep(i, n) { if (i > 0) cout << " "; cout << ans[i] + 1; } cout << endl; return; } vector<int> ans; set<int> st; rep(i, n) st.insert(i); int out = -1; rep(i, n - 6) { int z = *st.begin(); if (out == z) { z = *(++st.begin()); } ans.push_back(z); st.erase(z); out = a[z]; } vector<int> v; for (auto &&z : st) { v.push_back(z); } vector<int> las = calc(v, out); rep(i, las.size()) ans.push_back(las[i]); assert(ans.size() == n); rep(i, n) { if (i > 0) cout << " "; cout << ans[i] + 1; } cout << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); // cout << fixed << setprecision(17); init(); // int t; cin >> t; rep(i, t)solve(); solve(); stop return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; // #define int long long typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = (1e+18) + 7; typedef pair<int, int> P; #define stop \ char nyaa; \ cin >> nyaa; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) #define all(v) (v).begin(), (v).end() typedef pair<ll, ll> LP; typedef long double ld; typedef pair<ld, ld> LDP; const ld eps = 1e-6; const ld pi = acos(-1.0); // typedef vector<vector<ll>> mat; typedef vector<int> vec; ll mod_pow(ll a, ll n) { ll res = 1; while (n) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } struct modint { ll n; modint() : n(0) { ; } modint(ll m) : n(m) { if (n >= mod) n %= mod; else if (n < 0) n = (n % mod + mod) % mod; } operator int() { return n; } }; bool operator==(modint a, modint b) { return a.n == b.n; } modint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod) a.n -= mod; return a; } modint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0) a.n += mod; return a; } modint operator*=(modint &a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; } modint operator+(modint a, modint b) { return a += b; } modint operator-(modint a, modint b) { return a -= b; } modint operator*(modint a, modint b) { return a *= b; } modint operator^(modint a, int n) { if (n == 0) return modint(1); modint res = (a * a) ^ (n / 2); if (n % 2) res = res * a; return res; } ll inv(ll a, ll p) { return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p); } modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); } const int max_n = 1 << 20; modint fact[max_n], factinv[max_n]; void init() { fact[0] = modint(1); for (int i = 0; i < max_n - 1; i++) { fact[i + 1] = fact[i] * modint(i + 1); } factinv[max_n - 1] = modint(1) / fact[max_n - 1]; for (int i = max_n - 2; i >= 0; i--) { factinv[i] = factinv[i + 1] * modint(i + 1); } } modint comb(int a, int b) { if (a < 0 || b < 0 || a < b) return 0; return fact[a] * factinv[b] * factinv[a - b]; } int a[1 << 17]; vector<int> calc(vector<int> v, int pre) { sort(v.begin(), v.end()); while (true) { bool valid = true; if (v[0] == pre) valid = false; rep(i, v.size() - 1) { if (v[i + 1] == a[v[i]]) valid = false; } if (valid) return v; if (!next_permutation(v.begin(), v.end())) break; } return {}; } modint dp[1001]; modint cop[1001]; void solve() { int n; cin >> n; rep(i, n) { cin >> a[i]; a[i]--; } if (n == 2) { cout << -1 << endl; return; } if (n <= 6) { vector<int> ori(n); rep(i, n) ori[i] = i; vector<int> ans = calc(ori, -1); rep(i, n) { if (i > 0) cout << " "; cout << ans[i] + 1; } cout << endl; return; } vector<int> ans; set<int> st; rep(i, n) st.insert(i); int out = -1; rep(i, n - 6) { int z = *st.begin(); if (out == z) { z = *(++st.begin()); } ans.push_back(z); st.erase(z); out = a[z]; } vector<int> v; for (auto &&z : st) { v.push_back(z); } vector<int> las = calc(v, out); if (las.empty()) { int chk; if (a[v[0]] == a[v[1]]) chk = a[v[0]]; else chk = a[v[2]]; rep(i, n - 6) { if (a[ans.back()] == chk) { v.push_back(ans.back()); ans.pop_back(); } else break; } ans.push_back(chk); sort(v.begin(), v.end()); if (a[chk] == v[0]) swap(v[0], v[1]); rep(i, v.size()) if (v[i] != chk) ans.push_back(v[i]); } else { rep(i, las.size()) ans.push_back(las[i]); } // assert(ans.size() == n); rep(i, n) { if (i > 0) cout << " "; cout << ans[i] + 1; } cout << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); // cout << fixed << setprecision(17); init(); // int t; cin >> t; rep(i, t)solve(); solve(); stop return 0; }
replace
180
182
180
203
0
p02809
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <complex> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(0); #ifdef LOCAL freopen("input.txt", "r", stdin); #endif int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; --a[i]; } if (n == 2) { cout << "-1\n"; return 0; } vector<int> hate(n); for (int i = 0; i < n; ++i) ++hate[a[i]]; set<int> have; for (int i = 0; i < n; ++i) have.insert(i); vector<int> ans; vector<bool> used(n); int who = -1; for (int i = 0; i < n; ++i) { for (int j : have) { if (j == who) continue; int x = a[j]; if (!used[x] && hate[x] == n - i - 1) { continue; } ans.push_back(j); used[j] = 1; --hate[x]; who = x; break; } have.erase(ans.back()); if (n - i - 1 == 3) break; } vector<int> cur; for (int j : have) cur.push_back(j); vector<int> best; do { if (cur.front() == who) continue; bool bad = false; for (int i = 0; i + 1 < (int)cur.size(); ++i) { if (cur[i + 1] == a[cur[i]]) { bad = true; break; } } if (!bad) { if (best.empty() || cur < best) best = cur; } } while (next_permutation(cur.begin(), cur.end())); for (int x : ans) { cout << x + 1 << ' '; } for (int x : best) { cout << x + 1 << ' '; } }
#include <algorithm> #include <cassert> #include <cmath> #include <complex> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(0); #ifdef LOCAL freopen("input.txt", "r", stdin); #endif int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; --a[i]; } if (n == 2) { cout << "-1\n"; return 0; } vector<int> hate(n); for (int i = 0; i < n; ++i) ++hate[a[i]]; set<int> have; for (int i = 0; i < n; ++i) have.insert(i); vector<int> ans; vector<bool> used(n); int who = -1; for (int i = 0; i < n; ++i) { for (int j : have) { if (j == who) continue; int x = a[j]; if (!used[x] && hate[x] == n - i - 1) { continue; } ans.push_back(j); used[j] = 1; --hate[x]; who = x; break; } have.erase(ans.back()); if (n - i - 1 <= 6) break; } vector<int> cur; for (int j : have) cur.push_back(j); vector<int> best; do { if (cur.front() == who) continue; bool bad = false; for (int i = 0; i + 1 < (int)cur.size(); ++i) { if (cur[i + 1] == a[cur[i]]) { bad = true; break; } } if (!bad) { if (best.empty() || cur < best) best = cur; } } while (next_permutation(cur.begin(), cur.end())); for (int x : ans) { cout << x + 1 << ' '; } for (int x : best) { cout << x + 1 << ' '; } }
replace
65
66
65
66
0
p02809
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define uint unsigned int #define ull unsigned long long #define inf 1010000000 #define infll 1010000000000000000ll #define vi vector<int> #define vll vector<ll> #define pb push_back #define pii pair<int, int> #define pll pair<ll, ll> #define pdd pair<double, double> #define mpr make_pair #define fi first #define se second #define pq priority_queue<int> #define pqll priority_queue<ll> #define up(x, y) (x < (y) ? x = (y) : 0) #define dn(x, y) (x > (y) ? x = (y) : 0) #define ad(x, y) (x = (x + (y)) % mod) #define cbn(x, y) ((ll)fac[x] * inv[y] % mod * inv[(x) - (y)] % mod) #define mod 1000000007 #define N 100009 using namespace std; int n, cnt, sz, a[N], vis[N], bo[N], path[N], blg[N], bg[N], dgr[N]; set<int> S; set<pii> T; pq Q; vi ans[N], f, g, b[N]; int read(); ll readll(); void dfs(int x, int para) { if (bo[x] == para) return; vis[x] = 1; bo[x] = para; S.insert(x); for (int p : b[x]) { // if (min(u,v)==min(x,p) && max(u,v)==max(x,p)) continue; dfs(p, para); } } bool work(int k, int len, int last, int rst, int j) { if (k > len) { ans[cnt].clear(); int i; for (i = 1; i <= len; i++) ans[cnt].pb(path[i]); return 1; } if ((*T.rbegin()).fi == S.size() - 1) { // cerr<<"!\n"; int x = (*T.rbegin()).se; // cerr<<x<<'\n'; if (x == a[last]) return 0; path[k] = x; S.erase(x); T.erase(mpr(dgr[x], x)); bool flag = 0; if (S.find(a[x]) != S.end()) { flag = 1; T.erase(mpr(dgr[a[x]]--, a[x])); T.insert(mpr(dgr[a[x]], a[x])); } if (work(k + 1, len, x, 0, j)) return 1; if (flag) { T.erase(mpr(dgr[a[x]]++, a[x])); T.insert(mpr(dgr[a[x]], a[x])); } return 0; } for (auto it = S.begin(); it != S.end();) if ((*it) != a[last]) { path[k] = *it; S.erase(it); int x = *it; T.erase(mpr(dgr[x], x)); bool flag = 0; if (S.find(a[x]) != S.end()) { flag = 1; T.erase(mpr(dgr[a[x]]--, a[x])); T.insert(mpr(dgr[a[x]], a[x])); } if (work(k + 1, len, *it, rst, j)) return 1; S.insert(path[k]); it = S.upper_bound(path[k]); x = path[k]; T.insert(mpr(dgr[x], x)); if (flag) { T.erase(mpr(dgr[a[x]]++, a[x])); T.insert(mpr(dgr[a[x]], a[x])); } } else it++; return 0; } int main() { scanf("%d", &n); if (n == 2) { puts("-1"); return 0; } int i, j; for (i = 1; i <= n; i++) { scanf("%d", &a[i]); b[a[i]].pb(i); } cnt = 1; for (i = 1; i <= n; i++) S.insert(i); for (int p : S) dgr[a[p]]++; T.clear(); for (int p : S) T.insert(mpr(dgr[p], p)); work(1, S.size(), 0, 0, j); for (int p : ans[1]) printf("%d ", p); puts(""); return 0; } int read() { int x = 0; char ch = getchar(); bool flag = 0; while (ch < '0' || ch > '9') { if (ch == '-') flag = 1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return flag ? -x : x; } ll readll() { ll x = 0; char ch = getchar(); bool flag = 0; while (ch < '0' || ch > '9') { if (ch == '-') flag = 1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return flag ? -x : x; }
#include <bits/stdc++.h> #define ll long long #define uint unsigned int #define ull unsigned long long #define inf 1010000000 #define infll 1010000000000000000ll #define vi vector<int> #define vll vector<ll> #define pb push_back #define pii pair<int, int> #define pll pair<ll, ll> #define pdd pair<double, double> #define mpr make_pair #define fi first #define se second #define pq priority_queue<int> #define pqll priority_queue<ll> #define up(x, y) (x < (y) ? x = (y) : 0) #define dn(x, y) (x > (y) ? x = (y) : 0) #define ad(x, y) (x = (x + (y)) % mod) #define cbn(x, y) ((ll)fac[x] * inv[y] % mod * inv[(x) - (y)] % mod) #define mod 1000000007 #define N 100009 using namespace std; int n, cnt, sz, a[N], vis[N], bo[N], path[N], blg[N], bg[N], dgr[N]; set<int> S; set<pii> T; pq Q; vi ans[N], f, g, b[N]; int read(); ll readll(); void dfs(int x, int para) { if (bo[x] == para) return; vis[x] = 1; bo[x] = para; S.insert(x); for (int p : b[x]) { // if (min(u,v)==min(x,p) && max(u,v)==max(x,p)) continue; dfs(p, para); } } bool work(int k, int len, int last, int rst, int j) { if (k > len) { ans[cnt].clear(); int i; for (i = 1; i <= len; i++) ans[cnt].pb(path[i]); return 1; } if ((*T.rbegin()).fi == S.size() - 1) { // cerr<<"!\n"; int x = (*T.rbegin()).se; // cerr<<x<<'\n'; if (x == a[last]) return 0; path[k] = x; S.erase(x); T.erase(mpr(dgr[x], x)); bool flag = 0; if (S.find(a[x]) != S.end()) { flag = 1; T.erase(mpr(dgr[a[x]]--, a[x])); T.insert(mpr(dgr[a[x]], a[x])); } if (work(k + 1, len, x, 0, j)) return 1; if (flag) { T.erase(mpr(dgr[a[x]]++, a[x])); T.insert(mpr(dgr[a[x]], a[x])); } S.insert(x); T.insert(mpr(dgr[x], x)); return 0; } for (auto it = S.begin(); it != S.end();) if ((*it) != a[last]) { path[k] = *it; S.erase(it); int x = *it; T.erase(mpr(dgr[x], x)); bool flag = 0; if (S.find(a[x]) != S.end()) { flag = 1; T.erase(mpr(dgr[a[x]]--, a[x])); T.insert(mpr(dgr[a[x]], a[x])); } if (work(k + 1, len, *it, rst, j)) return 1; S.insert(path[k]); it = S.upper_bound(path[k]); x = path[k]; T.insert(mpr(dgr[x], x)); if (flag) { T.erase(mpr(dgr[a[x]]++, a[x])); T.insert(mpr(dgr[a[x]], a[x])); } } else it++; return 0; } int main() { scanf("%d", &n); if (n == 2) { puts("-1"); return 0; } int i, j; for (i = 1; i <= n; i++) { scanf("%d", &a[i]); b[a[i]].pb(i); } cnt = 1; for (i = 1; i <= n; i++) S.insert(i); for (int p : S) dgr[a[p]]++; T.clear(); for (int p : S) T.insert(mpr(dgr[p], p)); work(1, S.size(), 0, 0, j); for (int p : ans[1]) printf("%d ", p); puts(""); return 0; } int read() { int x = 0; char ch = getchar(); bool flag = 0; while (ch < '0' || ch > '9') { if (ch == '-') flag = 1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return flag ? -x : x; } ll readll() { ll x = 0; char ch = getchar(); bool flag = 0; while (ch < '0' || ch > '9') { if (ch == '-') flag = 1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return flag ? -x : x; }
insert
72
72
72
74
0
p02809
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(0); #define pb push_back #define eb emplace_back #define ins insert #define ph push #define f first #define s second #define cbr cerr << "hi\n" #define mmst(x, v) memset((x), v, sizeof((x))) #define siz(x) ll(x.size()) #define all(x) (x).begin(), (x).end() #define lbd(x, y) lower_bound(all(x), y) #define ubd(x, y) upper_bound(all(x), y) mt19937 rng(chrono::steady_clock::now() .time_since_epoch() .count()); // can be used by calling rng() or shuffle(A, A+n, rng) inline long long rand(long long x, long long y) { return (rng() % (y + 1 - x)) + x; } // inclusivesss string inline to_string(char c) { string s(1, c); return s; } template <typename T> inline T gcd(T a, T b) { return a == 0 ? llabs(b) : gcd(b % a, a); } typedef long long ll; typedef long double ld; #define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i) #define DEC(i, s, e) for (ll i = s; i >= ll(e); --i) typedef pair<ll, ll> pi; typedef pair<ll, pi> spi; typedef pair<pi, pi> dpi; #define LLINF ((long long)1e18) // 1234567890987654321 #define INF 1234567890ll // #define cerr if(0)cout #define MAXN (100006) ll n, A[MAXN], cnt[MAXN]; set<int> s[MAXN]; set<int> av; int main() { FAST cin >> n; FOR(i, 1, n) cin >> A[i], ++cnt[A[i]]; if (n == 2) { cout << "-1\n"; return 0; } FOR(i, 1, n) s[cnt[i]].ins(i), av.ins(i); vector<ll> v; while (n - siz(v) > 3) { ll V = n - siz(v); ll tp = -1; assert(siz(s[V]) == 0); if (siz(s[V - 1])) { tp = *s[V - 1].begin(); } else { if (v.empty() || A[v.back()] != *av.begin()) { tp = *av.begin(); } else { tp = *++av.begin(); } } assert(~tp); v.pb(tp); av.erase(tp); s[cnt[tp]].ins(tp); if (av.find(A[tp]) != av.end()) { s[cnt[A[tp]]].erase(A[tp]); --cnt[A[tp]]; s[cnt[A[tp]]].ins(A[tp]); } } assert(n - siz(v) == 3); /* next permutation here */ vector<ll> hey = {*av.begin(), *++av.begin(), *++ ++av.begin()}; vector<ll> lol = {0, 1, 2}; do { v.pb(hey[lol[0]]), v.pb(hey[lol[1]]), v.pb(hey[lol[2]]); bool fk = 1; FOR(i, max(0ll, n - 4), n - 2) if (A[v[i]] == v[i + 1]) { fk = 0; } if (fk) break; v.pop_back(), v.pop_back(), v.pop_back(); } while (next_permutation(all(lol))); /* */ assert(v.size() == n); for (auto i : v) cout << i << ' '; cout << '\n'; }
#include "bits/stdc++.h" using namespace std; #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(0); #define pb push_back #define eb emplace_back #define ins insert #define ph push #define f first #define s second #define cbr cerr << "hi\n" #define mmst(x, v) memset((x), v, sizeof((x))) #define siz(x) ll(x.size()) #define all(x) (x).begin(), (x).end() #define lbd(x, y) lower_bound(all(x), y) #define ubd(x, y) upper_bound(all(x), y) mt19937 rng(chrono::steady_clock::now() .time_since_epoch() .count()); // can be used by calling rng() or shuffle(A, A+n, rng) inline long long rand(long long x, long long y) { return (rng() % (y + 1 - x)) + x; } // inclusivesss string inline to_string(char c) { string s(1, c); return s; } template <typename T> inline T gcd(T a, T b) { return a == 0 ? llabs(b) : gcd(b % a, a); } typedef long long ll; typedef long double ld; #define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i) #define DEC(i, s, e) for (ll i = s; i >= ll(e); --i) typedef pair<ll, ll> pi; typedef pair<ll, pi> spi; typedef pair<pi, pi> dpi; #define LLINF ((long long)1e18) // 1234567890987654321 #define INF 1234567890ll // #define cerr if(0)cout #define MAXN (100006) ll n, A[MAXN], cnt[MAXN]; set<int> s[MAXN]; set<int> av; int main() { FAST cin >> n; FOR(i, 1, n) cin >> A[i], ++cnt[A[i]]; if (n == 2) { cout << "-1\n"; return 0; } FOR(i, 1, n) s[cnt[i]].ins(i), av.ins(i); vector<ll> v; while (n - siz(v) > 3) { ll V = n - siz(v); ll tp = -1; assert(siz(s[V]) == 0); if (siz(s[V - 1])) { tp = *s[V - 1].begin(); } else { if (v.empty() || A[v.back()] != *av.begin()) { tp = *av.begin(); } else { tp = *++av.begin(); } } assert(~tp); v.pb(tp); av.erase(tp); s[cnt[tp]].erase(tp); if (av.find(A[tp]) != av.end()) { s[cnt[A[tp]]].erase(A[tp]); --cnt[A[tp]]; s[cnt[A[tp]]].ins(A[tp]); } } assert(n - siz(v) == 3); /* next permutation here */ vector<ll> hey = {*av.begin(), *++av.begin(), *++ ++av.begin()}; vector<ll> lol = {0, 1, 2}; do { v.pb(hey[lol[0]]), v.pb(hey[lol[1]]), v.pb(hey[lol[2]]); bool fk = 1; FOR(i, max(0ll, n - 4), n - 2) if (A[v[i]] == v[i + 1]) { fk = 0; } if (fk) break; v.pop_back(), v.pop_back(), v.pop_back(); } while (next_permutation(all(lol))); /* */ assert(v.size() == n); for (auto i : v) cout << i << ' '; cout << '\n'; }
replace
73
74
73
74
0
p02809
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <complex> #include <cstdint> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; #define MP make_pair #define PB push_back #define inf 1000000007 #define mod 1000000007 #define rep(i, n) for (int i = 0; i < (int)(n); ++i) int main() { int n; cin >> n; if (n == 2) { cout << -1 << endl; return 0; } vector<int> a(n + 1); vector<int> c(n + 1); vector<int> pre(n + 1); rep(i, n) { cin >> a[i + 1]; c[a[i + 1]]++; pre[c[a[i + 1]]] = c[a[i + 1]]; } set<pair<int, int>, greater<pair<int, int>>> s; set<int> st; rep(i, n) { s.insert(MP(c[i + 1], i + 1)); st.insert(i + 1); } vector<int> res; for (int i = 0; i < n - 3; i++) { auto x = *s.begin(); int k = x.first; int z = x.second; if (k == n - i - 1) { res.push_back(z); s.erase(x); st.erase(z); int y = a[z]; if (st.count(y)) { s.erase(MP(pre[y], y)); pre[y]--; s.insert(MP(pre[y], y)); } } else { int p = 0; if (i != 0) p = a[res[i - 1]]; auto z = st.begin(); int nx; if (p != (*z)) { nx = *z; } else { z++; nx = *z; } res.push_back(nx); st.erase(nx); s.erase(MP(pre[nx], nx)); int y = a[nx]; if (st.count(y)) { s.erase(MP(pre[y], y)); pre[y]--; s.insert(MP(pre[y], y)); } } } // for(auto x:res){ // cerr << x << " "; // } // cerr << endl; vector<int> v; for (auto x : st) { v.push_back(x); } int ng; if (n > 3) ng = a[res[n - 4]]; else ng = 0; do { if (v[0] == ng) continue; if (v[1] == a[v[0]]) continue; if (v[2] == a[v[1]]) continue; res.push_back(v[0]); res.push_back(v[1]); res.push_back(v[2]); break; } while (next_permutation(v.begin(), v.end())); rep(i, n - 1) { if (a[res[i]] == res[i + 1]) assert(0); } for (auto x : res) { cout << x << " "; } cout << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <complex> #include <cstdint> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; #define MP make_pair #define PB push_back #define inf 1000000007 #define mod 1000000007 #define rep(i, n) for (int i = 0; i < (int)(n); ++i) int main() { int n; cin >> n; if (n == 2) { cout << -1 << endl; return 0; } vector<int> a(n + 1); vector<int> c(n + 1); vector<int> pre(n + 1); rep(i, n) { cin >> a[i + 1]; c[a[i + 1]]++; pre[a[i + 1]] = c[a[i + 1]]; } set<pair<int, int>, greater<pair<int, int>>> s; set<int> st; rep(i, n) { s.insert(MP(c[i + 1], i + 1)); st.insert(i + 1); } vector<int> res; for (int i = 0; i < n - 3; i++) { auto x = *s.begin(); int k = x.first; int z = x.second; if (k == n - i - 1) { res.push_back(z); s.erase(x); st.erase(z); int y = a[z]; if (st.count(y)) { s.erase(MP(pre[y], y)); pre[y]--; s.insert(MP(pre[y], y)); } } else { int p = 0; if (i != 0) p = a[res[i - 1]]; auto z = st.begin(); int nx; if (p != (*z)) { nx = *z; } else { z++; nx = *z; } res.push_back(nx); st.erase(nx); s.erase(MP(pre[nx], nx)); int y = a[nx]; if (st.count(y)) { s.erase(MP(pre[y], y)); pre[y]--; s.insert(MP(pre[y], y)); } } } // for(auto x:res){ // cerr << x << " "; // } // cerr << endl; vector<int> v; for (auto x : st) { v.push_back(x); } int ng; if (n > 3) ng = a[res[n - 4]]; else ng = 0; do { if (v[0] == ng) continue; if (v[1] == a[v[0]]) continue; if (v[2] == a[v[1]]) continue; res.push_back(v[0]); res.push_back(v[1]); res.push_back(v[2]); break; } while (next_permutation(v.begin(), v.end())); rep(i, n - 1) { if (a[res[i]] == res[i + 1]) assert(0); } for (auto x : res) { cout << x << " "; } cout << endl; return 0; }
replace
43
44
43
44
0
p02809
C++
Runtime Error
#include <bits/stdc++.h> #include <random> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef long double ld; // #define int ll typedef pair<int, int> pii; typedef pair<pii, pii> piii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii> vpi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<short> vs; typedef vector<vs> vvs; typedef vector<vvs> vvvs; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef vector<ld> vld; typedef vector<vld> vvld; typedef vector<vvld> vvvld; typedef vector<string> vst; typedef vector<vst> vvst; typedef pair<ld, ld> pld; typedef complex<double> base; #define inmin(a, b) a = min(a, (b)) #define inmax(a, b) a = max(a, (b)) #define mp(a, b) make_pair((a), (b)) #define ALL(a) a.begin(), a.end() #define RALL(a) a.rbegin(), a.rend() #define sqr(x) ((x) * (x)) #define fori(i, n) for (int i = 0; i < int(n); ++i) #define SZ(a) ((int)((a).size())) #define triple(T) tuple<T, T, T> #define quad(T) tuple<T, T, T, T> #define watch(x) cerr << (#x) << " = " << (x) << endl; #ifdef MAX_HOME #define cerr cout #else #define cerr \ if (false) \ cerr #endif const double PI = 2 * acos(0.0); #define rand shittttty_shit mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count()); const string DIGITS = "0123456789"; const string ALPH = "abcdefghijklmnopqrstuvwxyz"; template <class T0, class T1> inline ostream &operator<<(ostream &out, pair<T0, T1> &a) { return out << "{" << a.first << ", " << a.second << "}"; } template <class T0, class T1, class T2> inline ostream &operator<<(ostream &out, tuple<T0, T1, T2> &a) { return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << "}"; } template <class T0, class T1, class T2, class T3> inline ostream &operator<<(ostream &out, tuple<T0, T1, T2, T3> &a) { return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ", " << get<3>(a) << "}"; } template <class T> inline ostream &operator<<(ostream &out, vector<T> &a) { out << "["; fori(i, a.size()) out << a[i] << vector<string>{", ", "] "}[i + 1 == a.size()]; return out; } void smain(); signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef MAX_HOME freopen("input.txt", "r", stdin); clock_t start = clock(); #endif cout << setprecision(12) << fixed; smain(); #ifdef MAX_HOME cout << "\n\n\n\nTOTAL EXECUTION TIME: " << float(clock() - start) / CLOCKS_PER_SEC << endl; #endif return 0; } bool kek(vi a) { int n = a.size(); int fr = -1; vi ns(n); fori(i, n) { ns[i] = i + 1; } vi p = ns; int ans = -1; do { bool ok = fr != p[0]; fori(i, n - 1) { if (p[i + 1] == a[p[i] - 1]) { ok = false; break; } } if (ok) { if (ans == -1) { ans = p[0]; } else if (ans != p[0]) { return true; } } } while (next_permutation(ALL(p))); return false; } vi naive(const vi &a, vi ns, int fr) { a.size(); int n = ns.size(); sort(ALL(ns)); vi p = ns; vi ans = {}; do { bool ok = fr != p[0]; fori(i, n - 1) { if (p[i + 1] == a[p[i] - 1]) { ok = false; break; } } if (ok) { if (ans.empty()) { ans = p; } else if (ans > p) { ans = p; } } } while (next_permutation(ALL(p))); return ans; } void stress() { int cnt = -1; set<vi> samples; while (true) { if (++cnt % 50 == 0) watch(cnt); int n = 3 + rng() % 7; vi a(n); fori(i, n) { a[i] = rng() % (n - 1); if (a[i] == i) a[i]++; a[i]++; } if (!kek(a)) { set<int> kek; for (auto x : a) { kek.insert(x); } if (kek.size() != 2) { watch(a); break; } } } } void smain() { // stress(); int n; cin >> n; vi a(n); vi deg(n + 1, 0); fori(i, n) { cin >> a[i]; deg[a[i]]++; } set<int> fre; for (int i = 1; i <= n; ++i) { fre.insert(i); } vi p; int fr = -1; set<pii> degver; for (int i = 1; i <= n; ++i) { degver.insert({deg[i], i}); } for (int i = 0; n - i > 3; ++i) { auto it = fre.begin(); if (*it == fr) ++it; int x = *it; if ((*degver.rbegin()).first == n - i - 1) { x = (*degver.rbegin()).second; } assert(fr != x); p.push_back(x); degver.erase({deg[x], x}); if (fre.count(a[x])) { degver.erase({deg[a[x]], a[x]}); deg[a[x]]--; degver.insert({deg[a[x]], a[x]}); } fr = a[x - 1]; fre.erase(x); } vi rest(ALL(fre)); vi sol = naive(a, rest, fr); if (sol.empty()) { // assert(n == 2); cout << "-1"; return; } p.insert(p.end(), ALL(sol)); for (auto x : p) { cout << x << ' '; } }
#include <bits/stdc++.h> #include <random> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef long double ld; // #define int ll typedef pair<int, int> pii; typedef pair<pii, pii> piii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii> vpi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<short> vs; typedef vector<vs> vvs; typedef vector<vvs> vvvs; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef vector<ld> vld; typedef vector<vld> vvld; typedef vector<vvld> vvvld; typedef vector<string> vst; typedef vector<vst> vvst; typedef pair<ld, ld> pld; typedef complex<double> base; #define inmin(a, b) a = min(a, (b)) #define inmax(a, b) a = max(a, (b)) #define mp(a, b) make_pair((a), (b)) #define ALL(a) a.begin(), a.end() #define RALL(a) a.rbegin(), a.rend() #define sqr(x) ((x) * (x)) #define fori(i, n) for (int i = 0; i < int(n); ++i) #define SZ(a) ((int)((a).size())) #define triple(T) tuple<T, T, T> #define quad(T) tuple<T, T, T, T> #define watch(x) cerr << (#x) << " = " << (x) << endl; #ifdef MAX_HOME #define cerr cout #else #define cerr \ if (false) \ cerr #endif const double PI = 2 * acos(0.0); #define rand shittttty_shit mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count()); const string DIGITS = "0123456789"; const string ALPH = "abcdefghijklmnopqrstuvwxyz"; template <class T0, class T1> inline ostream &operator<<(ostream &out, pair<T0, T1> &a) { return out << "{" << a.first << ", " << a.second << "}"; } template <class T0, class T1, class T2> inline ostream &operator<<(ostream &out, tuple<T0, T1, T2> &a) { return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << "}"; } template <class T0, class T1, class T2, class T3> inline ostream &operator<<(ostream &out, tuple<T0, T1, T2, T3> &a) { return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ", " << get<3>(a) << "}"; } template <class T> inline ostream &operator<<(ostream &out, vector<T> &a) { out << "["; fori(i, a.size()) out << a[i] << vector<string>{", ", "] "}[i + 1 == a.size()]; return out; } void smain(); signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef MAX_HOME freopen("input.txt", "r", stdin); clock_t start = clock(); #endif cout << setprecision(12) << fixed; smain(); #ifdef MAX_HOME cout << "\n\n\n\nTOTAL EXECUTION TIME: " << float(clock() - start) / CLOCKS_PER_SEC << endl; #endif return 0; } bool kek(vi a) { int n = a.size(); int fr = -1; vi ns(n); fori(i, n) { ns[i] = i + 1; } vi p = ns; int ans = -1; do { bool ok = fr != p[0]; fori(i, n - 1) { if (p[i + 1] == a[p[i] - 1]) { ok = false; break; } } if (ok) { if (ans == -1) { ans = p[0]; } else if (ans != p[0]) { return true; } } } while (next_permutation(ALL(p))); return false; } vi naive(const vi &a, vi ns, int fr) { a.size(); int n = ns.size(); sort(ALL(ns)); vi p = ns; vi ans = {}; do { bool ok = fr != p[0]; fori(i, n - 1) { if (p[i + 1] == a[p[i] - 1]) { ok = false; break; } } if (ok) { if (ans.empty()) { ans = p; } else if (ans > p) { ans = p; } } } while (next_permutation(ALL(p))); return ans; } void stress() { int cnt = -1; set<vi> samples; while (true) { if (++cnt % 50 == 0) watch(cnt); int n = 3 + rng() % 7; vi a(n); fori(i, n) { a[i] = rng() % (n - 1); if (a[i] == i) a[i]++; a[i]++; } if (!kek(a)) { set<int> kek; for (auto x : a) { kek.insert(x); } if (kek.size() != 2) { watch(a); break; } } } } void smain() { // stress(); int n; cin >> n; vi a(n); vi deg(n + 1, 0); fori(i, n) { cin >> a[i]; deg[a[i]]++; } set<int> fre; for (int i = 1; i <= n; ++i) { fre.insert(i); } vi p; int fr = -1; set<pii> degver; for (int i = 1; i <= n; ++i) { degver.insert({deg[i], i}); } for (int i = 0; n - i > 3; ++i) { auto it = fre.begin(); if (*it == fr) ++it; int x = *it; if ((*degver.rbegin()).first == n - i - 1) { x = (*degver.rbegin()).second; } assert(fr != x); p.push_back(x); degver.erase({deg[x], x}); if (fre.count(a[x - 1])) { degver.erase({deg[a[x - 1]], a[x - 1]}); deg[a[x - 1]]--; degver.insert({deg[a[x - 1]], a[x - 1]}); } fr = a[x - 1]; fre.erase(x); } vi rest(ALL(fre)); vi sol = naive(a, rest, fr); if (sol.empty()) { // assert(n == 2); cout << "-1"; return; } p.insert(p.end(), ALL(sol)); for (auto x : p) { cout << x << ' '; } }
replace
219
223
219
223
0
p02809
C++
Runtime Error
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (int)(b); i++) #define FORD(i, a, b) for (int i = a; i > (int)(b); i--) #define PPC(x) __builtin_popcount(x) #define pb push_back #define ALL(x) (x).begin(), (x).end() #define ft first #define sd second #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; const int maxN = 1 << 17; int T[maxN], res[maxN]; bool juz[maxN]; void fail() { printf("-1\n"); exit(0); } void ans(int n) { FOR(i, 1, n + 1) printf("%d ", res[i]); printf("\n"); exit(0); } bool colide(int i) { return T[res[i]] == res[i + 1]; } int main() { int n; scanf("%d", &n); FOR(i, 1, n + 1) scanf("%d", T + i); if (n == 2) fail(); int list = 1, mx = 0; FOR(i, 0, n) { int v = res[i]; if (T[v] != list) res[i + 1] = list; else res[i + 1] = mx == list - 1 ? mx + 2 : mx + 1; juz[res[i + 1]] = true; while (juz[list]) list++; mx = max(mx, res[i + 1]); } int i = n; while (colide(i - 1)) swap(res[i], res[i - 1]), i--; sort(res + i + 1, res + n + 1); if (!colide(i)) ans(n); if (i <= n - 2) swap(res[i + 1], res[i + 2]), ans(n); assert(i == n - 1); swap(res[n - 2], res[n - 1]); swap(res[n - 2], res[n]); if (colide(n - 3)) swap(res[n - 2], res[n]); ans(n); assert(false); }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (int)(b); i++) #define FORD(i, a, b) for (int i = a; i > (int)(b); i--) #define PPC(x) __builtin_popcount(x) #define pb push_back #define ALL(x) (x).begin(), (x).end() #define ft first #define sd second #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; const int maxN = 1 << 17; int T[maxN], res[maxN]; bool juz[maxN]; void fail() { printf("-1\n"); exit(0); } void ans(int n) { FOR(i, 1, n + 1) printf("%d ", res[i]); printf("\n"); exit(0); } bool colide(int i) { return T[res[i]] == res[i + 1]; } int main() { int n; scanf("%d", &n); FOR(i, 1, n + 1) scanf("%d", T + i); if (n == 2) fail(); int list = 1, mx = 0; FOR(i, 0, n) { int v = res[i]; if (T[v] != list) res[i + 1] = list; else res[i + 1] = mx == list - 1 ? mx + 2 : mx + 1; juz[res[i + 1]] = true; while (juz[list]) list++; mx = max(mx, res[i + 1]); } if (res[n] > n) res[n] = list; int i = n; while (colide(i - 1)) swap(res[i], res[i - 1]), i--; sort(res + i + 1, res + n + 1); if (!colide(i)) ans(n); if (i <= n - 2) swap(res[i + 1], res[i + 2]), ans(n); assert(i == n - 1); swap(res[n - 2], res[n - 1]); swap(res[n - 2], res[n]); if (colide(n - 3)) swap(res[n - 2], res[n]); ans(n); assert(false); }
insert
58
58
58
61
0
p02810
C++
Time Limit Exceeded
#pragma GCC optimize("O2") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx,avx2,sse,sse2,ssse3,tune=native") #include <bits/stdc++.h> #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define pb push_back using namespace std; using ll = long long; using vi = vector<ll>; using pi = pair<ll, ll>; using vpi = vector<pi>; const int maxn = 113, maxl = 503, mod = 1e9 + 7; int n, x, dp[maxn][maxn][2 * maxl], sm[maxn][maxn][2 * maxl], ssm[maxn][maxn][2 * maxl]; int fact[maxn], ifact[maxn]; vi l; int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; } int sub(int x, int y) { if (y) y = mod - y; return add(x, y); } int mul(int x, int y) { return x * 1ll * y % mod; } int setup() { fact[0] = ifact[0] = 1; for (int i = 1; i < maxn; i++) { fact[i] = mul(fact[i - 1], i); if (i == 1) ifact[i] = 1; else ifact[i] = (mod - mul(ifact[mod % i], mod / i)); } for (int i = 2; i < maxn; i++) ifact[i] = mul(ifact[i], ifact[i - 1]); } int nck(int n, int k) { if (k > n) return 0; return mul(fact[n], mul(ifact[k], ifact[n - k])); } int main() { ios::sync_with_stdio(0); cin.tie(0); setup(); cin >> n >> x; l.resize(n); for (auto &i : l) cin >> i; sort(rall(l)); dp[0][0][0] = 1; int &X = x; for (int i = 0; i <= n; i++) { for (int k = 0; k <= n; k++) { int running = 0; for (int s = 0; s <= x; s++) { running = add(running, sm[i][k][s]); dp[i][k][s] = add(dp[i][k][s], running); if (i == n) continue; dp[i + 1][k + 1][s + l[i]] = add(dp[i + 1][k + 1][s + l[i]], dp[i][k][s]); int t; if (k > 1) { t = mul(mul(k, (k - 1)), dp[i][k][s]); for (int x = 0; x < l[i] - 1 && s + x <= X; x++) { dp[i + 1][k - 1][s + x] = add(dp[i + 1][k - 1][s + x], mul(t, l[i] - x - 1)); } } if (k) { t = mul(k, dp[i][k][s]); t = mul(t, 2); sm[i + 1][k][s + 1] = add(sm[i + 1][k][s + 1], t); sm[i + 1][k][s + l[i]] = sub(sm[i + 1][k][s + l[i]], t); dp[i + 1][k][s] = add(dp[i + 1][k][s], mul(dp[i][k][s], sub(s, mul(k, l[i] - 1)))); } } } } int ans = 0; for (int i = 0; i <= n; i++) ans = add(ans, mul(dp[n][i][x], fact[i])); cout << ans << '\n'; }
#pragma GCC optimize("O2") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx,avx2,sse,sse2,ssse3,tune=native") #include <bits/stdc++.h> #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define pb push_back using namespace std; using ll = long long; using vi = vector<ll>; using pi = pair<ll, ll>; using vpi = vector<pi>; const int maxn = 113, maxl = 503, mod = 1e9 + 7; int n, x, dp[maxn][maxn][2 * maxl], sm[maxn][maxn][2 * maxl], ssm[maxn][maxn][2 * maxl]; int fact[maxn], ifact[maxn]; vi l; int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; } int sub(int x, int y) { if (y) y = mod - y; return add(x, y); } int mul(int x, int y) { return x * 1ll * y % mod; } int setup() { fact[0] = ifact[0] = 1; for (int i = 1; i < maxn; i++) { fact[i] = mul(fact[i - 1], i); if (i == 1) ifact[i] = 1; else ifact[i] = (mod - mul(ifact[mod % i], mod / i)); } for (int i = 2; i < maxn; i++) ifact[i] = mul(ifact[i], ifact[i - 1]); } int nck(int n, int k) { if (k > n) return 0; return mul(fact[n], mul(ifact[k], ifact[n - k])); } int main() { ios::sync_with_stdio(0); cin.tie(0); setup(); cin >> n >> x; l.resize(n); for (auto &i : l) cin >> i; sort(rall(l)); dp[0][0][0] = 1; int &X = x; for (int i = 0; i <= n; i++) { for (int k = 0; k <= n; k++) { int running = 0; for (int s = 0; s <= x; s++) { running = add(running, sm[i][k][s]); dp[i][k][s] = add(dp[i][k][s], running); if (!dp[i][k][s]) continue; if (i == n) continue; dp[i + 1][k + 1][s + l[i]] = add(dp[i + 1][k + 1][s + l[i]], dp[i][k][s]); int t; if (k > 1) { t = mul(mul(k, (k - 1)), dp[i][k][s]); for (int x = 0; x < l[i] - 1 && s + x <= X; x++) { dp[i + 1][k - 1][s + x] = add(dp[i + 1][k - 1][s + x], mul(t, l[i] - x - 1)); } } if (k) { t = mul(k, dp[i][k][s]); t = mul(t, 2); sm[i + 1][k][s + 1] = add(sm[i + 1][k][s + 1], t); sm[i + 1][k][s + l[i]] = sub(sm[i + 1][k][s + l[i]], t); dp[i + 1][k][s] = add(dp[i + 1][k][s], mul(dp[i][k][s], sub(s, mul(k, l[i] - 1)))); } } } } int ans = 0; for (int i = 0; i <= n; i++) ans = add(ans, mul(dp[n][i][x], fact[i])); cout << ans << '\n'; }
insert
63
63
63
65
TLE
p02810
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; /*#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<typename T> using gpp_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<typename T, typename L> using gpp_map = tree<T, L, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<typename T> using gpp_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;*/ struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for (int i = (begin); i < (end); i++) #define REP(i, n) FOR(i, 0, n) #define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--) #define IREP(i, n) IFOR(i, 0, n) #define Sort(v) sort(v.begin(), v.end()) #define Reverse(v) reverse(v.begin(), v.end()) #define all(v) v.begin(), v.end() #define SZ(v) ((int)v.size()) #define Lower_bound(v, x) \ distance(v.begin(), lower_bound(v.begin(), v.end(), x)) #define Upper_bound(v, x) \ distance(v.begin(), upper_bound(v.begin(), v.end(), x)) #define Max(a, b) a = max(a, b) #define Min(a, b) a = min(a, b) #define bit(n) (1LL << (n)) #define bit_exist(x, n) ((x >> n) & 1) #define debug(x) cout << #x << "=" << x << endl; #define vdebug(v) \ { \ cout << #v << "=" << endl; \ REP(i_debug, v.size()) { cout << v[i_debug] << ","; } \ cout << endl; \ } #define mdebug(m) \ { \ cout << #m << "=" << endl; \ REP(i_debug, m.size()) { \ REP(j_debug, m[i_debug].size()) { cout << m[i_debug][j_debug] << ","; } \ cout << endl; \ } \ } #define Return(ans) \ { \ cout << (ans) << endl; \ return 0; \ } #define pb push_back #define f first #define s second #define int long long #define INF 1000000000000000000 template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &x : v) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { for (int i = 0; i < v.size(); i++) { cout << v[i]; if (i != v.size() - 1) cout << endl; }; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { cout << '(' << p.first << ',' << p.second << ')'; return os; } template <typename T> void Out(T x) { cout << x << endl; } template <typename T1, typename T2> void Ans(bool f, T1 y, T2 n) { if (f) Out(y); else Out(n); } using vec = vector<int>; using mat = vector<vec>; using Pii = pair<int, int>; using PiP = pair<int, Pii>; using PPi = pair<Pii, int>; using bools = vector<bool>; using pairs = vector<Pii>; // int dx[4] = {1,0,-1,0}; // int dy[4] = {0,1,0,-1}; // char d[4] = {'D','R','U','L'}; const int mod = 1000000007; // const int mod = 998244353; // #define Add(x, y) x = (x + (y)) % mod // #define Mult(x, y) x = (x * (y)) % mod template <long long MOD> struct ModInt { using ll = long long; ll val; void setval(ll v) { val = v % MOD; }; ModInt() : val(0) {} ModInt(ll v) { setval(v); }; ModInt operator+(const ModInt &x) const { return ModInt(val + x.val); } ModInt operator-(const ModInt &x) const { return ModInt(val - x.val + MOD); } ModInt operator*(const ModInt &x) const { return ModInt(val * x.val); } ModInt operator/(const ModInt &x) const { return *this * x.inv(); } ModInt operator-() const { return ModInt(MOD - val); } ModInt operator+=(const ModInt &x) { return *this = *this + x; } ModInt operator-=(const ModInt &x) { return *this = *this - x; } ModInt operator*=(const ModInt &x) { return *this = *this * x; } ModInt operator/=(const ModInt &x) { return *this = *this / x; } friend ostream &operator<<(ostream &os, const ModInt &x) { os << x.val; return os; } friend istream &operator>>(istream &is, ModInt &x) { is >> x.val; x.val = (x.val % MOD + MOD) % MOD; return is; } ModInt pow(ll n) const { ModInt a = 1; if (n == 0) return a; int i0 = 64 - __builtin_clzll(n); for (int i = i0 - 1; i >= 0; i--) { a = a * a; if ((n >> i) & 1) a *= (*this); } return a; } ModInt inv() const { return this->pow(MOD - 2); } }; using mint = ModInt<mod>; mint pow(mint x, long long n) { return x.pow(n); } // using mint = double; //for debug using mvec = vector<mint>; using mmat = vector<mvec>; struct Combination { vector<mint> fact, invfact; Combination(int N) { fact = vector<mint>({mint(1)}); invfact = vector<mint>({mint(1)}); fact_initialize(N); } void fact_initialize(int N) { int i0 = fact.size(); if (i0 >= N + 1) return; fact.resize(N + 1); invfact.resize(N + 1); for (int i = i0; i <= N; i++) fact[i] = fact[i - 1] * i; invfact[N] = (mint)1 / fact[N]; for (int i = N - 1; i >= i0; i--) invfact[i] = invfact[i + 1] * (i + 1); } mint nCr(int n, int r) { if (n < 0 || r < 0 || r > n) return mint(0); if (fact.size() < n + 1) fact_initialize(n); return fact[n] * invfact[r] * invfact[n - r]; } mint nPr(int n, int r) { if (n < 0 || r < 0 || r > n) return mint(0); if (fact.size() < n + 1) fact_initialize(n); return fact[n] * invfact[n - r]; } }; mint dp[100][100][501] = {}; signed main() { int N, X; cin >> N >> X; vec L(N); cin >> L; Sort(L); Reverse(L); dp[0][0][L[0]] = 1; FOR(i, 1, N) { int jmax = max(i, X / L[i - 1]); REP(j, jmax) FOR(k, L[0], X + 1) { int t = k - L[i] + 1 - j * (L[i] - 1); if (t > 0) dp[i][j][k] += dp[i - 1][j][k] * t; if (k + L[i] <= X) dp[i][j + 1][k + L[i]] += dp[i - 1][j][k] * (2 + j); FOR(l, 1, L[i]) if (k + l <= X) { dp[i][j][k + l] += dp[i - 1][j][k] * 2 * (j + 1); } if (j > 0) { FOR(l, 0, L[i] - 1) if (k + l <= X) { dp[i][j - 1][k + l] += dp[i - 1][j][k] * j * (L[i] - 1 - l); } } } } mint ans = 0; REP(j, N) ans += dp[N - 1][j][X]; Out(ans); return 0; }
#include <bits/stdc++.h> using namespace std; /*#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<typename T> using gpp_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<typename T, typename L> using gpp_map = tree<T, L, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<typename T> using gpp_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;*/ struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for (int i = (begin); i < (end); i++) #define REP(i, n) FOR(i, 0, n) #define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--) #define IREP(i, n) IFOR(i, 0, n) #define Sort(v) sort(v.begin(), v.end()) #define Reverse(v) reverse(v.begin(), v.end()) #define all(v) v.begin(), v.end() #define SZ(v) ((int)v.size()) #define Lower_bound(v, x) \ distance(v.begin(), lower_bound(v.begin(), v.end(), x)) #define Upper_bound(v, x) \ distance(v.begin(), upper_bound(v.begin(), v.end(), x)) #define Max(a, b) a = max(a, b) #define Min(a, b) a = min(a, b) #define bit(n) (1LL << (n)) #define bit_exist(x, n) ((x >> n) & 1) #define debug(x) cout << #x << "=" << x << endl; #define vdebug(v) \ { \ cout << #v << "=" << endl; \ REP(i_debug, v.size()) { cout << v[i_debug] << ","; } \ cout << endl; \ } #define mdebug(m) \ { \ cout << #m << "=" << endl; \ REP(i_debug, m.size()) { \ REP(j_debug, m[i_debug].size()) { cout << m[i_debug][j_debug] << ","; } \ cout << endl; \ } \ } #define Return(ans) \ { \ cout << (ans) << endl; \ return 0; \ } #define pb push_back #define f first #define s second #define int long long #define INF 1000000000000000000 template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &x : v) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { for (int i = 0; i < v.size(); i++) { cout << v[i]; if (i != v.size() - 1) cout << endl; }; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { cout << '(' << p.first << ',' << p.second << ')'; return os; } template <typename T> void Out(T x) { cout << x << endl; } template <typename T1, typename T2> void Ans(bool f, T1 y, T2 n) { if (f) Out(y); else Out(n); } using vec = vector<int>; using mat = vector<vec>; using Pii = pair<int, int>; using PiP = pair<int, Pii>; using PPi = pair<Pii, int>; using bools = vector<bool>; using pairs = vector<Pii>; // int dx[4] = {1,0,-1,0}; // int dy[4] = {0,1,0,-1}; // char d[4] = {'D','R','U','L'}; const int mod = 1000000007; // const int mod = 998244353; // #define Add(x, y) x = (x + (y)) % mod // #define Mult(x, y) x = (x * (y)) % mod template <long long MOD> struct ModInt { using ll = long long; ll val; void setval(ll v) { val = v % MOD; }; ModInt() : val(0) {} ModInt(ll v) { setval(v); }; ModInt operator+(const ModInt &x) const { return ModInt(val + x.val); } ModInt operator-(const ModInt &x) const { return ModInt(val - x.val + MOD); } ModInt operator*(const ModInt &x) const { return ModInt(val * x.val); } ModInt operator/(const ModInt &x) const { return *this * x.inv(); } ModInt operator-() const { return ModInt(MOD - val); } ModInt operator+=(const ModInt &x) { return *this = *this + x; } ModInt operator-=(const ModInt &x) { return *this = *this - x; } ModInt operator*=(const ModInt &x) { return *this = *this * x; } ModInt operator/=(const ModInt &x) { return *this = *this / x; } friend ostream &operator<<(ostream &os, const ModInt &x) { os << x.val; return os; } friend istream &operator>>(istream &is, ModInt &x) { is >> x.val; x.val = (x.val % MOD + MOD) % MOD; return is; } ModInt pow(ll n) const { ModInt a = 1; if (n == 0) return a; int i0 = 64 - __builtin_clzll(n); for (int i = i0 - 1; i >= 0; i--) { a = a * a; if ((n >> i) & 1) a *= (*this); } return a; } ModInt inv() const { return this->pow(MOD - 2); } }; using mint = ModInt<mod>; mint pow(mint x, long long n) { return x.pow(n); } // using mint = double; //for debug using mvec = vector<mint>; using mmat = vector<mvec>; struct Combination { vector<mint> fact, invfact; Combination(int N) { fact = vector<mint>({mint(1)}); invfact = vector<mint>({mint(1)}); fact_initialize(N); } void fact_initialize(int N) { int i0 = fact.size(); if (i0 >= N + 1) return; fact.resize(N + 1); invfact.resize(N + 1); for (int i = i0; i <= N; i++) fact[i] = fact[i - 1] * i; invfact[N] = (mint)1 / fact[N]; for (int i = N - 1; i >= i0; i--) invfact[i] = invfact[i + 1] * (i + 1); } mint nCr(int n, int r) { if (n < 0 || r < 0 || r > n) return mint(0); if (fact.size() < n + 1) fact_initialize(n); return fact[n] * invfact[r] * invfact[n - r]; } mint nPr(int n, int r) { if (n < 0 || r < 0 || r > n) return mint(0); if (fact.size() < n + 1) fact_initialize(n); return fact[n] * invfact[n - r]; } }; mint dp[100][100][501] = {}; signed main() { int N, X; cin >> N >> X; vec L(N); cin >> L; Sort(L); Reverse(L); dp[0][0][L[0]] = 1; FOR(i, 1, N) { int jmax = min(i, X / L[i - 1]); REP(j, jmax) FOR(k, L[0], X + 1) { int t = k - L[i] + 1 - j * (L[i] - 1); if (t > 0) dp[i][j][k] += dp[i - 1][j][k] * t; if (k + L[i] <= X) dp[i][j + 1][k + L[i]] += dp[i - 1][j][k] * (2 + j); FOR(l, 1, L[i]) if (k + l <= X) { dp[i][j][k + l] += dp[i - 1][j][k] * 2 * (j + 1); } if (j > 0) { FOR(l, 0, L[i] - 1) if (k + l <= X) { dp[i][j - 1][k + l] += dp[i - 1][j][k] * j * (L[i] - 1 - l); } } } } mint ans = 0; REP(j, N) ans += dp[N - 1][j][X]; Out(ans); return 0; }
replace
205
206
205
206
0
p02810
C++
Time Limit Exceeded
/* `-:://:::- `//:-------:/:` .+:--.......--:+` `+:--..`````..--//` .o:--..`` ``..--:o` .o:--...```..---+/` `/y+o/---....---:+o. `...````-os+/:---:/+o/--.` `-/+++++/:. `...` :h+d+oooo+/+-` ... `/++//:::://++-`....` -.`//````````:` `..` `o+/::------://o/` `-` -. -` `..` `---.-o/:./o/::-..``..-ЗАПУСКАЕМ .. .. -` `... ``..`` `....o+:-++/:--.```..-://s. `-` .- -` `-o: .-//::::/:-` `:s+/:--....-::/+s-` .- `- -` -///:--------:/:` ./s+//:::::://oo-``..НЕЙРОННУЮ: СЕТЬ:::::::-`РАБОТЯГИ `+:--........--:/` .:ooo+++++osso-` `.:-...`/` ./::-------:/:` -` :+--..``````.--:+:...-+:-` `.-/+++++/+-.-` -. ``:so:/:--.......--:+` `-```````o+/+--..`````..--:o/-..:s+:. ```````:``.. `-` -` `+:--..`````..--/+-.../.`````..-o:--.......---/o. ` `: `:- -. .o:--..`` ``..--:o` `-` `:o+:--------:+o-` `-`-... .. .o/--...```..--:+/` `-` `oy/so/////++o/.` -/` `-` `- ``+s/o/:---...---:++. `-` .-../d://///:-.` `.---..``-..- .-/..`````-oo+/:::::/+o+- `-``-` `-. ```` `:++++/+++++- ..``.-/:` /y-:/++o++/:.`..` ./. `- -++/::::::://+/..:-``:` .. `-.` ```.``` `..` `..`-` `- `` -o//:--....-::/++` -.-` `-`.-` `..`..` `-.- -----ss+:++/:--.```..-://s. /. `:: `-:. ./` `````/:..+o/::-..``.--:/+s. ..-` `-``-` ..` `-` `-`-` `-s+/::-----::/+oo---``-` .. .:- ``` .-` .-.- `-` `:oo+//::://+os/..:`..-/:` :y.-:::::::.`.-` ./-` `-` `./+oooooooo+/.`- .-:...`.. .//:-------://` `- `..` `:. ``.-::::-.``-/` `-` `- `oo:+:--.......--:/` `- `.:--h.``..``` -.-`.- .- `+:--..`````..--//` `- /s-//::::::::. -` `/- .. .o:--..`` ``..--:o.```.- `//:--------://` -` .-`.-` -.`-o/--...```..--:+/.``-:....``:-.+:--....`...--:+` ..`-. `-. ``:os:o/:---...---:++. `- ``///+:-..``````.--:+-````-.` `.:///////.-` .:-..` -``-+o+/:::::/+o/. `- `:+:-..`````..--:o/:--/ys+- `-++///////+o/. ``....`-. :` `.:++++++/:.` .- -o/---......---/o. `.` `++//:-----::/+o:..` .-` : ``````` .- `+so+:--------:++-` `````:-``:o/::-..`..--:/+o` -. `- .- `../../+o+////+o+:.` -----syo/o+/:--.```..-://s. .-` `- .- `... ``-:////:-`` .` `/s//:--....-::/+s. -. `-` .- `..` .+o+/:::--:://+s/-..` .::+y ``` .- `..` ./oo++////+oso-` `.... :y-+:::::::/` ... `.:+oooooo/-` `....-. .//:-------:/:-.` ``...`` /+:+:--.......--:+` `+:--..`````..--//` .o:--..`` ``..--:o` .+/--...```..--:+/` `-o/:---...---:++. `-+o+/:---:/+o/. `.:+oooo+/-.` `````` */ #ifdef aimbot #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") #endif #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <istream> #include <limits> #include <list> #include <map> #include <ostream> #include <queue> #include <random> #include <set> #include <string> #include <typeinfo> #include <unordered_map> #include <unordered_set> #include <vector> #define random escape__from__random__aetuhoetnuhshe #define mt make_tuple #define x first #define y second #define pb push_back #define ppb pop_back #define mp make_pair #define umap unordered_map #define uset unordered_set #define elif else if #define len(v) ((int)v.size()) #define f(i, n) for (int i = 0; i < (n); i++) #define rof(i, n) for (int i = ((n)-1); i >= 0; i--) #define apply(v, act) \ for (auto &x : v) { \ act; \ } #define log(args...) \ { \ string s = #args; \ deque<string> deq; \ string buf = ""; \ int bal = 0; \ for (char c : s) { \ if (c == '(' || c == '[' || c == '{') { \ bal++; \ } else if (c == ')' || c == ']' || c == '}') { \ bal--; \ } else { \ if (bal == 0) { \ if (c == ',') { \ deq.pb(buf); \ buf = ""; \ } else { \ if (c != ' ') { \ buf += c; \ } \ } \ } \ } \ } \ if (!buf.empty()) { \ deq.pb(buf); \ } \ smart_io::precall_print(); \ smart_io::_print(deq, args); \ } inline int min(const int &x, const int &y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; } inline int max(const int &x, const int &y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; } inline long long min(const long long &x, const long long &y) { return (((y - x) >> (64 - 1)) & (x ^ y)) ^ x; } inline long long max(const long long &x, const long long &y) { return (((y - x) >> (64 - 1)) & (x ^ y)) ^ y; } #define print \ smart_io::precall_print(); \ cout, #define scan cin, #ifdef fast_allocator const int MAXMEM = 200 * 1000 * 1024; char _memory[MAXMEM]; size_t _ptr = 0; void *operator new(size_t _x) { _ptr += _x; assert(_ptr < MAXMEM); return _memory + _ptr - _x; } void operator delete(void *) noexcept {} #endif using namespace std; char string_in_buffer[(int)260]; void fast_scan(int &x) { scanf("%d", &x); } void fast_scan(long long &x) { scanf("%lld", &x); } void fast_scan(unsigned long long &x) { scanf("%llu", &x); } void fast_scan(double &x) { scanf("%lf", &x); } void fast_scan(long double &x) { scanf("%Lf", &x); } void fast_scan(char &x) { scanf("%c", &x); if (x == '\n') { fast_scan(x); } } void fast_scan(string &x) { scanf("%s", string_in_buffer); x = string(string_in_buffer); } template <class TFirst, class TSecond> void fast_scan(pair<TFirst, TSecond> &p) { fast_scan(p.first); fast_scan(p.second); } template <class T> void fast_scan(vector<T> &v) { for (auto &x : v) fast_scan(x); } void fast_print(const int &x) { printf("%d", x); } void fast_print(const unsigned int &x) { printf("%u", x); } void fast_print(const long long &x) { printf("%lld", x); } void fast_print(const unsigned long long &x) { printf("%llu", x); } void fast_print(const char &x) { printf("%c", x); }; // void fast_print(__int128 x) { // if (x == 0) { fast_print('0'); return; } // if (x < 0) { // fast_print('-'); // x = -x; // } // __int128 p = 1; // while (x / (p * 10)) p *= 10; // while (p) { // __int128 symb = x / p; // fast_print((int)symb); // x -= p * symb; // p /= 10; // } // }; void fast_print(const double &x) { printf("%.15lf", x); } void fast_print(const long double &x) { printf("%.15Lf", x); } void fast_print(const string &x) { printf("%s", x.c_str()); } void fast_print(const char v[]) { fast_print((string)v); } template <class TFirst, class TSecond> void fast_print(const pair<TFirst, TSecond> &p) { fast_print(p.first); fast_print(' '); fast_print(p.second); } template <class T> void fast_print(const vector<T> &v) { if (v.empty()) return; fast_print(v[0]); for (int i = 1; i < v.size(); i++) { fast_print(' '); fast_print(v[i]); } } template <class T> void fast_print(const vector<vector<T>> &v) { if (v.empty()) return; fast_print(v[0]); for (int i = 1; i < v.size(); i++) { fast_print('\n'); fast_print(v[i]); } } template <class T> void fast_print(const T &v) { for (const auto &x : v) { fast_print(x); fast_print(' '); } } using namespace std; namespace smart_io { string print_start = ""; string sep = " "; bool first_print = false; void precall_print() { fast_print(print_start); print_start = "\n"; first_print = true; } void _print(deque<string>) {} template <class T, class... Args> void _print(deque<string> names, T elem, Args... args) { if (!first_print) { fast_print("\n"); } else { first_print = false; } fast_print(names.front()); fast_print(" = "); fast_print(elem); names.pop_front(); _print(names, args...); } } // namespace smart_io template <class T> ostream &operator,(ostream &os, const T &object) { if (!smart_io::first_print) { fast_print(smart_io::sep); } else { smart_io::first_print = false; } fast_print(object); return os; } template <class T> istream &operator,(istream &is, T &object) { fast_scan(object); return is; } namespace random { using namespace std::chrono; mt19937 rng(duration_cast<milliseconds>(system_clock::now().time_since_epoch()) .count()); uniform_real_distribution<> prob_dist(0.0, 1.0); }; // namespace random namespace typedefs { typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef long double ld; } // namespace typedefs namespace numbers_operation { template <class T> inline T floor_mod(T a, const T &b) { a %= b; if (a < 0) a += b; return a; } } // namespace numbers_operation using namespace numbers_operation; using namespace typedefs; using namespace random; const ll MOD = 1e9 + 7; void add(int &x, int y) { x += y; if (x >= MOD) x -= MOD; } ll mul(ll a, ll b) { return (a * b) % MOD; } int dp[102][502]; int ndp[102][502]; signed main(signed argc, char *argv[]) { int n, x; scan n, x; vector<int> v(n); scan v; sort(v.rbegin(), v.rend()); // groups, busy space dp[0][0] = 1; for (int l : v) { memset(ndp, 0, sizeof ndp); for (int g = 0; g < n + 1; g++) { for (int busy = 0; busy < x + 1; busy++) { // add as a new one if (g + 1 < n + 1 && busy + l < x + 1) add(ndp[g + 1][busy + l], mul(g + 1, dp[g][busy])); // dissappear add(ndp[g][busy], mul(dp[g][busy], busy - g * (l - 1))); // add to group for (int eat = 1; eat < l && busy + eat < x + 1; eat++) { // left or right add(ndp[g][busy + eat], mul(2, mul(g, dp[g][busy]))); } // merge two if (g >= 2 && l > 1) { for (int eat = 0; eat <= l - 2 && busy + eat < x + 1; eat++) { add(ndp[g - 1][busy + eat], mul(l - eat - 1, mul(g - 1, dp[g][busy]))); } } } } swap(dp, ndp); } int rez = 0; for (int i = 0; i < n + 1; i++) { add(rez, dp[i][x]); } print rez; }
/* `-:://:::- `//:-------:/:` .+:--.......--:+` `+:--..`````..--//` .o:--..`` ``..--:o` .o:--...```..---+/` `/y+o/---....---:+o. `...````-os+/:---:/+o/--.` `-/+++++/:. `...` :h+d+oooo+/+-` ... `/++//:::://++-`....` -.`//````````:` `..` `o+/::------://o/` `-` -. -` `..` `---.-o/:./o/::-..``..-ЗАПУСКАЕМ .. .. -` `... ``..`` `....o+:-++/:--.```..-://s. `-` .- -` `-o: .-//::::/:-` `:s+/:--....-::/+s-` .- `- -` -///:--------:/:` ./s+//:::::://oo-``..НЕЙРОННУЮ: СЕТЬ:::::::-`РАБОТЯГИ `+:--........--:/` .:ooo+++++osso-` `.:-...`/` ./::-------:/:` -` :+--..``````.--:+:...-+:-` `.-/+++++/+-.-` -. ``:so:/:--.......--:+` `-```````o+/+--..`````..--:o/-..:s+:. ```````:``.. `-` -` `+:--..`````..--/+-.../.`````..-o:--.......---/o. ` `: `:- -. .o:--..`` ``..--:o` `-` `:o+:--------:+o-` `-`-... .. .o/--...```..--:+/` `-` `oy/so/////++o/.` -/` `-` `- ``+s/o/:---...---:++. `-` .-../d://///:-.` `.---..``-..- .-/..`````-oo+/:::::/+o+- `-``-` `-. ```` `:++++/+++++- ..``.-/:` /y-:/++o++/:.`..` ./. `- -++/::::::://+/..:-``:` .. `-.` ```.``` `..` `..`-` `- `` -o//:--....-::/++` -.-` `-`.-` `..`..` `-.- -----ss+:++/:--.```..-://s. /. `:: `-:. ./` `````/:..+o/::-..``.--:/+s. ..-` `-``-` ..` `-` `-`-` `-s+/::-----::/+oo---``-` .. .:- ``` .-` .-.- `-` `:oo+//::://+os/..:`..-/:` :y.-:::::::.`.-` ./-` `-` `./+oooooooo+/.`- .-:...`.. .//:-------://` `- `..` `:. ``.-::::-.``-/` `-` `- `oo:+:--.......--:/` `- `.:--h.``..``` -.-`.- .- `+:--..`````..--//` `- /s-//::::::::. -` `/- .. .o:--..`` ``..--:o.```.- `//:--------://` -` .-`.-` -.`-o/--...```..--:+/.``-:....``:-.+:--....`...--:+` ..`-. `-. ``:os:o/:---...---:++. `- ``///+:-..``````.--:+-````-.` `.:///////.-` .:-..` -``-+o+/:::::/+o/. `- `:+:-..`````..--:o/:--/ys+- `-++///////+o/. ``....`-. :` `.:++++++/:.` .- -o/---......---/o. `.` `++//:-----::/+o:..` .-` : ``````` .- `+so+:--------:++-` `````:-``:o/::-..`..--:/+o` -. `- .- `../../+o+////+o+:.` -----syo/o+/:--.```..-://s. .-` `- .- `... ``-:////:-`` .` `/s//:--....-::/+s. -. `-` .- `..` .+o+/:::--:://+s/-..` .::+y ``` .- `..` ./oo++////+oso-` `.... :y-+:::::::/` ... `.:+oooooo/-` `....-. .//:-------:/:-.` ``...`` /+:+:--.......--:+` `+:--..`````..--//` .o:--..`` ``..--:o` .+/--...```..--:+/` `-o/:---...---:++. `-+o+/:---:/+o/. `.:+oooo+/-.` `````` */ #ifdef aimbot #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") #endif #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <istream> #include <limits> #include <list> #include <map> #include <ostream> #include <queue> #include <random> #include <set> #include <string> #include <typeinfo> #include <unordered_map> #include <unordered_set> #include <vector> #define random escape__from__random__aetuhoetnuhshe #define mt make_tuple #define x first #define y second #define pb push_back #define ppb pop_back #define mp make_pair #define umap unordered_map #define uset unordered_set #define elif else if #define len(v) ((int)v.size()) #define f(i, n) for (int i = 0; i < (n); i++) #define rof(i, n) for (int i = ((n)-1); i >= 0; i--) #define apply(v, act) \ for (auto &x : v) { \ act; \ } #define log(args...) \ { \ string s = #args; \ deque<string> deq; \ string buf = ""; \ int bal = 0; \ for (char c : s) { \ if (c == '(' || c == '[' || c == '{') { \ bal++; \ } else if (c == ')' || c == ']' || c == '}') { \ bal--; \ } else { \ if (bal == 0) { \ if (c == ',') { \ deq.pb(buf); \ buf = ""; \ } else { \ if (c != ' ') { \ buf += c; \ } \ } \ } \ } \ } \ if (!buf.empty()) { \ deq.pb(buf); \ } \ smart_io::precall_print(); \ smart_io::_print(deq, args); \ } inline int min(const int &x, const int &y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; } inline int max(const int &x, const int &y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; } inline long long min(const long long &x, const long long &y) { return (((y - x) >> (64 - 1)) & (x ^ y)) ^ x; } inline long long max(const long long &x, const long long &y) { return (((y - x) >> (64 - 1)) & (x ^ y)) ^ y; } #define print \ smart_io::precall_print(); \ cout, #define scan cin, #ifdef fast_allocator const int MAXMEM = 200 * 1000 * 1024; char _memory[MAXMEM]; size_t _ptr = 0; void *operator new(size_t _x) { _ptr += _x; assert(_ptr < MAXMEM); return _memory + _ptr - _x; } void operator delete(void *) noexcept {} #endif using namespace std; char string_in_buffer[(int)260]; void fast_scan(int &x) { scanf("%d", &x); } void fast_scan(long long &x) { scanf("%lld", &x); } void fast_scan(unsigned long long &x) { scanf("%llu", &x); } void fast_scan(double &x) { scanf("%lf", &x); } void fast_scan(long double &x) { scanf("%Lf", &x); } void fast_scan(char &x) { scanf("%c", &x); if (x == '\n') { fast_scan(x); } } void fast_scan(string &x) { scanf("%s", string_in_buffer); x = string(string_in_buffer); } template <class TFirst, class TSecond> void fast_scan(pair<TFirst, TSecond> &p) { fast_scan(p.first); fast_scan(p.second); } template <class T> void fast_scan(vector<T> &v) { for (auto &x : v) fast_scan(x); } void fast_print(const int &x) { printf("%d", x); } void fast_print(const unsigned int &x) { printf("%u", x); } void fast_print(const long long &x) { printf("%lld", x); } void fast_print(const unsigned long long &x) { printf("%llu", x); } void fast_print(const char &x) { printf("%c", x); }; // void fast_print(__int128 x) { // if (x == 0) { fast_print('0'); return; } // if (x < 0) { // fast_print('-'); // x = -x; // } // __int128 p = 1; // while (x / (p * 10)) p *= 10; // while (p) { // __int128 symb = x / p; // fast_print((int)symb); // x -= p * symb; // p /= 10; // } // }; void fast_print(const double &x) { printf("%.15lf", x); } void fast_print(const long double &x) { printf("%.15Lf", x); } void fast_print(const string &x) { printf("%s", x.c_str()); } void fast_print(const char v[]) { fast_print((string)v); } template <class TFirst, class TSecond> void fast_print(const pair<TFirst, TSecond> &p) { fast_print(p.first); fast_print(' '); fast_print(p.second); } template <class T> void fast_print(const vector<T> &v) { if (v.empty()) return; fast_print(v[0]); for (int i = 1; i < v.size(); i++) { fast_print(' '); fast_print(v[i]); } } template <class T> void fast_print(const vector<vector<T>> &v) { if (v.empty()) return; fast_print(v[0]); for (int i = 1; i < v.size(); i++) { fast_print('\n'); fast_print(v[i]); } } template <class T> void fast_print(const T &v) { for (const auto &x : v) { fast_print(x); fast_print(' '); } } using namespace std; namespace smart_io { string print_start = ""; string sep = " "; bool first_print = false; void precall_print() { fast_print(print_start); print_start = "\n"; first_print = true; } void _print(deque<string>) {} template <class T, class... Args> void _print(deque<string> names, T elem, Args... args) { if (!first_print) { fast_print("\n"); } else { first_print = false; } fast_print(names.front()); fast_print(" = "); fast_print(elem); names.pop_front(); _print(names, args...); } } // namespace smart_io template <class T> ostream &operator,(ostream &os, const T &object) { if (!smart_io::first_print) { fast_print(smart_io::sep); } else { smart_io::first_print = false; } fast_print(object); return os; } template <class T> istream &operator,(istream &is, T &object) { fast_scan(object); return is; } namespace random { using namespace std::chrono; mt19937 rng(duration_cast<milliseconds>(system_clock::now().time_since_epoch()) .count()); uniform_real_distribution<> prob_dist(0.0, 1.0); }; // namespace random namespace typedefs { typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef long double ld; } // namespace typedefs namespace numbers_operation { template <class T> inline T floor_mod(T a, const T &b) { a %= b; if (a < 0) a += b; return a; } } // namespace numbers_operation using namespace numbers_operation; using namespace typedefs; using namespace random; const ll MOD = 1e9 + 7; void add(int &x, int y) { x += y; if (x >= MOD) x -= MOD; } ll mul(ll a, ll b) { return (a * b) % MOD; } int dp[102][502]; int ndp[102][502]; signed main(signed argc, char *argv[]) { int n, x; scan n, x; vector<int> v(n); scan v; sort(v.rbegin(), v.rend()); // groups, busy space dp[0][0] = 1; for (int l : v) { memset(ndp, 0, sizeof ndp); for (int g = 0; g < n + 1; g++) { for (int busy = 0; busy < x + 1; busy++) { if (!dp[g][busy]) continue; // add as a new one if (g + 1 < n + 1 && busy + l < x + 1) add(ndp[g + 1][busy + l], mul(g + 1, dp[g][busy])); // dissappear add(ndp[g][busy], mul(dp[g][busy], busy - g * (l - 1))); // add to group for (int eat = 1; eat < l && busy + eat < x + 1; eat++) { // left or right add(ndp[g][busy + eat], mul(2, mul(g, dp[g][busy]))); } // merge two if (g >= 2 && l > 1) { for (int eat = 0; eat <= l - 2 && busy + eat < x + 1; eat++) { add(ndp[g - 1][busy + eat], mul(l - eat - 1, mul(g - 1, dp[g][busy]))); } } } } swap(dp, ndp); } int rez = 0; for (int i = 0; i < n + 1; i++) { add(rez, dp[i][x]); } print rez; }
insert
371
371
371
373
TLE
p02811
Python
Runtime Error
K = int(input("Enter k: ")) X = int(input("Enter x: ")) if K * 500 >= X: print("Yes") else: print("No")
K, X = map(int, input().split()) if K * 500 >= X: print("Yes") else: print("No")
replace
0
2
0
1
ValueError: invalid literal for int() with base 10: '2 900'
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02811/Python/s761507450.py", line 1, in <module> K = int(input("Enter k: ")) ValueError: invalid literal for int() with base 10: '2 900'
p02811
Python
Runtime Error
if __name__ == "__main__": x = int(input()) m, n = map(int, input().split()) if m * 500 >= n: print("Yes") else: print("No")
if __name__ == "__main__": m, n = map(int, input().split()) if m * 500 >= n: print("Yes") else: print("No")
delete
1
2
1
1
ValueError: invalid literal for int() with base 10: '2 900'
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02811/Python/s384378340.py", line 2, in <module> x = int(input()) ValueError: invalid literal for int() with base 10: '2 900'
p02811
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define END '\n' #define int long long #define pb push_back #define pii pair<int, int> #define ff first #define ss second #define trace(x) cerr << x << END; #define loop(i, a, b) for (int i = (a); i < (b); i++) #define loopb(i, b, a) for (int i = (b); i > (a); --i) const int mod = 1e9 + 7; const int mod1 = 998244353; const int inf = INT_MAX; bool mywish(const pii &a, const pii &b) { return a.first > b.first; } void solve() { int k, x; cin >> k >> x; cout << (500ll * k >= x ? "Yes" : "No"); } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif /* int t; cin>>t; while(t--)*/ solve(); // cerr<<END<<1.0*clock(); return 0; }
#include <bits/stdc++.h> using namespace std; #define END '\n' #define int long long #define pb push_back #define pii pair<int, int> #define ff first #define ss second #define trace(x) cerr << x << END; #define loop(i, a, b) for (int i = (a); i < (b); i++) #define loopb(i, b, a) for (int i = (b); i > (a); --i) const int mod = 1e9 + 7; const int mod1 = 998244353; const int inf = INT_MAX; bool mywish(const pii &a, const pii &b) { return a.first > b.first; } void solve() { int k, x; cin >> k >> x; cout << (500ll * k >= x ? "Yes" : "No"); } signed main() { /* int t; cin>>t; while(t--)*/ solve(); // cerr<<END<<1.0*clock(); return 0; }
replace
22
30
22
23
0
p02811
Python
Runtime Error
import itertools N = int(input()) P = tuple(map(int, input().split())) Q = tuple(map(int, input().split())) permutation = list(itertools.permutations([x for x in range(1, N + 1)])) a = permutation.index(P) + 1 b = permutation.index(Q) + 1 print(abs(a - b))
K, X = map(int, input().split()) print("Yes" if K * 500 >= X else "No")
replace
0
10
0
2
ValueError: invalid literal for int() with base 10: '2 900'
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02811/Python/s360744826.py", line 3, in <module> N = int(input()) ValueError: invalid literal for int() with base 10: '2 900'
p02811
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long int #define ld long double #define pb push_back #define eb emplace_back #define xx first #define yy second #define INF 0x3f3f3f3f #define show(x) cerr << #x << " : " << x << '\n'; #define yogeshk972() \ cerr << "\nTime Taken : " << (float)(clock() - time_p) / CLOCKS_PER_SEC \ << "\n"; clock_t time_p = clock(); using namespace std; const int N = 1e6 + 10, mod = 998244353; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif ll k, x; cin >> k >> x; if (500 * k >= x) cout << "Yes\n"; else cout << "No\n"; return 0; }
#include <bits/stdc++.h> #define ll long long int #define ld long double #define pb push_back #define eb emplace_back #define xx first #define yy second #define INF 0x3f3f3f3f #define show(x) cerr << #x << " : " << x << '\n'; #define yogeshk972() \ cerr << "\nTime Taken : " << (float)(clock() - time_p) / CLOCKS_PER_SEC \ << "\n"; clock_t time_p = clock(); using namespace std; const int N = 1e6 + 10, mod = 998244353; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); /* #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif */ ll k, x; cin >> k >> x; if (500 * k >= x) cout << "Yes\n"; else cout << "No\n"; return 0; }
replace
21
27
21
27
0
p02811
C++
Runtime Error
#include "bits/stdc++.h" #define ll long long int #define F first #define S second const ll mod = 1e9 + 7; #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); using namespace std; // ll gcd(ll a, ll b) //{ // if (a == 0) // return b; // else // return gcd(b % a, a); // } // ll modexp(ll x, ll y, ll p) //{ // ll res = 1; // x = x % p; // while (y > 0) // { // if (y & 1) // res = (res * x) % p; // y = y >> 1; // x = (x * x) % p; // } // return res; // } // ll sub_mod(ll a, ll b, ll m) //{ // if (a >= b) // return a - b; // else // return m - b + a; // } // ll add_mod(ll a, ll b) // { // if (0 == b) // return a; // b = mod - b; // if (a >= b) // return a - b; // else // return mod - b + a; // } int main() { fastio; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll k = 0, x = 0; cin >> k >> x; if (k * 500 >= x) cout << "Yes"; else cout << "No"; return 0; }
#include "bits/stdc++.h" #define ll long long int #define F first #define S second const ll mod = 1e9 + 7; #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); using namespace std; // ll gcd(ll a, ll b) //{ // if (a == 0) // return b; // else // return gcd(b % a, a); // } // ll modexp(ll x, ll y, ll p) //{ // ll res = 1; // x = x % p; // while (y > 0) // { // if (y & 1) // res = (res * x) % p; // y = y >> 1; // x = (x * x) % p; // } // return res; // } // ll sub_mod(ll a, ll b, ll m) //{ // if (a >= b) // return a - b; // else // return m - b + a; // } // ll add_mod(ll a, ll b) // { // if (0 == b) // return a; // b = mod - b; // if (a >= b) // return a - b; // else // return mod - b + a; // } int main() { fastio; // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif ll k = 0, x = 0; cin >> k >> x; if (k * 500 >= x) cout << "Yes"; else cout << "No"; return 0; }
replace
54
58
54
58
0
p02811
C++
Runtime Error
#include <bits/stdc++.h> #define endl '\n' #define fastIO \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define int long long int using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int combination(int n, int k) { int spaceOptimize[k + 1]; memset(spaceOptimize, 0, sizeof(spaceOptimize)); spaceOptimize[0] = 1; for (int i = 1; i <= n; i++) { for (int j = min(i, k); j > 0; j--) { spaceOptimize[j] = spaceOptimize[j] + spaceOptimize[j - 1]; } } return spaceOptimize[k]; } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif fastIO int n, k; cin >> n >> k; if (n * 500 >= k) cout << "Yes"; else cout << "No"; return 0; }
#include <bits/stdc++.h> #define endl '\n' #define fastIO \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define int long long int using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int combination(int n, int k) { int spaceOptimize[k + 1]; memset(spaceOptimize, 0, sizeof(spaceOptimize)); spaceOptimize[0] = 1; for (int i = 1; i <= n; i++) { for (int j = min(i, k); j > 0; j--) { spaceOptimize[j] = spaceOptimize[j] + spaceOptimize[j - 1]; } } return spaceOptimize[k]; } signed main() { #ifndef ONLINE_JUDGE // freopen("input.txt","r", stdin); #endif fastIO int n, k; cin >> n >> k; if (n * 500 >= k) cout << "Yes"; else cout << "No"; return 0; }
replace
26
27
26
27
0
p02811
C++
Runtime Error
#include <bits/stdc++.h> #define pb push_back #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define ll long long #define ii pair<int, int> #define vi vector<int> #define vll vector<ll> #define vii vector<ii> using namespace std; void solve() { return; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n, k; cin >> n >> k; cout << (n * 500 >= k ? "Yes" : "No") << endl; return 0; }
#include <bits/stdc++.h> #define pb push_back #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define ll long long #define ii pair<int, int> #define vi vector<int> #define vll vector<ll> #define vii vector<ii> using namespace std; void solve() { return; } int32_t main() { int n, k; cin >> n >> k; cout << (n * 500 >= k ? "Yes" : "No") << endl; return 0; }
delete
17
25
17
17
0
p02811
C++
Runtime Error
#include <bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; typedef long double ld; #define endl '\n' #define loop(i, m, n) for (ll i = m; i < n; i++) #define loope(i, m, n) for (ll i = m; i <= n; i++) #define epool(i, m, n) for (ll i = m; i >= n; i--) #define mod 1000000007 #define mod2 998244353 #define vi vector<ll> #define fi first #define se second #define pb push_back #define MP make_pair #define empf emplace_front #define empb emplace_back #define ret(x) return cout << x, 0; #define all(v) v.begin(), v.end() #define naman \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); using namespace std; ll mpow(ll base, ll exp) { base %= mod; ll result = 1; while (exp > 0) { if (exp & 1) result = ((ll)result * base) % mod; base = ((ll)base * base) % mod; exp >>= 1; } return result; } const ll INF = 1e18; const double PI = 4 * atan(1); void solve() { ll k, x; cin >> k >> x; if (500 * k >= x) cout << "Yes"; else cout << "No"; cout << endl; } int32_t main() { naman ll i = 0, j = 0; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // cout << fixed << setprecision(10); int t; t = 1; // cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; typedef long double ld; #define endl '\n' #define loop(i, m, n) for (ll i = m; i < n; i++) #define loope(i, m, n) for (ll i = m; i <= n; i++) #define epool(i, m, n) for (ll i = m; i >= n; i--) #define mod 1000000007 #define mod2 998244353 #define vi vector<ll> #define fi first #define se second #define pb push_back #define MP make_pair #define empf emplace_front #define empb emplace_back #define ret(x) return cout << x, 0; #define all(v) v.begin(), v.end() #define naman \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); using namespace std; ll mpow(ll base, ll exp) { base %= mod; ll result = 1; while (exp > 0) { if (exp & 1) result = ((ll)result * base) % mod; base = ((ll)base * base) % mod; exp >>= 1; } return result; } const ll INF = 1e18; const double PI = 4 * atan(1); void solve() { ll k, x; cin >> k >> x; if (500 * k >= x) cout << "Yes"; else cout << "No"; cout << endl; } int32_t main() { naman ll i = 0, j = 0; /* #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif */ // cout << fixed << setprecision(10); int t; t = 1; // cin >> t; while (t--) { solve(); } }
replace
54
60
54
60
0
p02811
C++
Runtime Error
#include <algorithm> #include <cassert> #include <iostream> #include <queue> #include <set> #include <string> #include <vector> using namespace std; int main() { int x, y; cin >> x >> y; return x * 500 >= y; }
#include <algorithm> #include <cassert> #include <iostream> #include <queue> #include <set> #include <string> #include <vector> using namespace std; int main() { int x, y; cin >> x >> y; if (x * 500 >= y) cout << "Yes"; else cout << "No"; return 0; }
replace
12
13
12
17
1
p02811
C++
Runtime Error
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<long long, long long>; #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(x) (x).begin(), (x).end() constexpr char ln = '\n'; constexpr long long MOD = 1000000007LL; // constexpr long long MOD = 998244353LL; template <class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return true; } return false; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int k, x; cin >> k >> x; string ans = 0; if (k * 500 >= x) ans = "Yes"; else ans = "No"; cout << ans << ln; return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<long long, long long>; #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(x) (x).begin(), (x).end() constexpr char ln = '\n'; constexpr long long MOD = 1000000007LL; // constexpr long long MOD = 998244353LL; template <class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return true; } return false; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int k, x; cin >> k >> x; string ans; if (k * 500 >= x) ans = "Yes"; else ans = "No"; cout << ans << ln; return 0; }
replace
34
35
34
35
-6
terminate called after throwing an instance of 'std::logic_error' what(): basic_string: construction from null is not valid
p02811
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL) #define mod 1000000007 #define qmod 998244353 #define M 1000001 #define endl "\n" #define all(a) (a).begin(), (a).end() #define rep(i, a, b) for (int i = a; i < b; ++i) #define repr(i, a, b) for (int i = a; i >= b; --i) #define F first #define S second #define pb push_back #define mp make_pair typedef long long ll; typedef pair<ll, ll> pii; typedef vector<int> vi; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vi> vvi; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast; int k, x; cin >> k >> x; cout << (k * 500 >= x ? "Yes" : "No"); return 0; } // keep it simple stupid
#include <bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL) #define mod 1000000007 #define qmod 998244353 #define M 1000001 #define endl "\n" #define all(a) (a).begin(), (a).end() #define rep(i, a, b) for (int i = a; i < b; ++i) #define repr(i, a, b) for (int i = a; i >= b; --i) #define F first #define S second #define pb push_back #define mp make_pair typedef long long ll; typedef pair<ll, ll> pii; typedef vector<int> vi; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vi> vvi; int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // #endif fast; int k, x; cin >> k >> x; cout << (k * 500 >= x ? "Yes" : "No"); return 0; } // keep it simple stupid
replace
25
29
25
29
0
p02811
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base ::sync_with_stdio(false); cin.tie(0); cout.tie(0); int a, b; cin >> a >> b; if (a * 500 >= b) cout << "Yes" << endl; else cout << "No" << endl; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base ::sync_with_stdio(false); cin.tie(0); cout.tie(0); int a, b; cin >> a >> b; if (a * 500 >= b) cout << "Yes" << endl; else cout << "No" << endl; }
delete
4
8
4
4
0
p02811
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; string S; cin >> S; int count = 0; for (int i = 0; i < N - 2; i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') count++; } cout << count << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int K, X; cin >> K >> X; if (K * 500 >= X) cout << "Yes" << endl; else cout << "No" << endl; }
replace
4
14
4
10
0
p02811
C++
Runtime Error
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> #define int long long #define rep(i, begin, end) \ for (__typeof(end) i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define F first #define S second #define sz(x) ((int)x.size()) #define pb push_back #define pf push_front #define eb emplace_back #define all(v) (v).begin(), (v).end() #define mod 1000000007 #define what_is(x) cerr << #x << " is " << x << "\n"; #define sortA(v) sort(v.begin(), v.end()) #define sortD(v) sort(v.rbegin(), v.rend()) #define PI 3.14159265358979323846 #define vout(a) \ for (auto x : a) \ cout << x << " "; #define INF 10000000000000000 // #define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, // tree_order_statistics_node_update> // using namespace __gnu_pbds; using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; using vii = vector<pii>; using mi = multiset<int>; using mii = multiset<pii>; int lcm(int a, int b) { return (a * (b / __gcd(a, b))); } void solve() { int t, n, m, i, k, r, l, u, v, w, j, s, sum = 0, pos, flag = 0, count = 0; string S, T, U; int x; cin >> k >> x; if (k * 500 >= x) cout << "Yes"; else cout << "No"; } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; // cin>>t; while (t--) { solve(); if (t) cout << "\n"; } return 0; }
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> #define int long long #define rep(i, begin, end) \ for (__typeof(end) i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define F first #define S second #define sz(x) ((int)x.size()) #define pb push_back #define pf push_front #define eb emplace_back #define all(v) (v).begin(), (v).end() #define mod 1000000007 #define what_is(x) cerr << #x << " is " << x << "\n"; #define sortA(v) sort(v.begin(), v.end()) #define sortD(v) sort(v.rbegin(), v.rend()) #define PI 3.14159265358979323846 #define vout(a) \ for (auto x : a) \ cout << x << " "; #define INF 10000000000000000 // #define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, // tree_order_statistics_node_update> // using namespace __gnu_pbds; using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; using vii = vector<pii>; using mi = multiset<int>; using mii = multiset<pii>; int lcm(int a, int b) { return (a * (b / __gcd(a, b))); } void solve() { int t, n, m, i, k, r, l, u, v, w, j, s, sum = 0, pos, flag = 0, count = 0; string S, T, U; int x; cin >> k >> x; if (k * 500 >= x) cout << "Yes"; else cout << "No"; } signed main() { // #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // #endif ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; // cin>>t; while (t--) { solve(); if (t) cout << "\n"; } return 0; }
replace
52
56
52
56
0
p02811
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define in(s) \ ll s; \ cin >> s #define pr(r) cout << r << endl #define prr(m, n) cout << m << " " << n << endl #define prarr(a, i, n) fab(j, i, n) cout << a[j] << " " #define wh(t) \ ll t; \ cin >> t; \ while (t--) #define sz(u) u.size() #define all(v) v.begin(), v.end() #define arr(a, n) \ ll a[n]; \ fab(i, 0, n) cin >> a[i] #define showdp(d, n, m) \ fab(i, 0, n) { \ fab(j, 0, m) if (d[i][j] == -1 || d[i][j] > 9) cout << d[i][j] << " "; \ else cout << " " << d[i][j] << " "; \ cout << endl; \ } #define prs(u) cout << u << " " #define fab(i, a, b) for (ll i = a; i < b; i++) #define fba(i, b, a) for (ll i = b; i >= a; i--) #define sup \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define MX 1000000007 #define MAX 10000007 #define endl "\n" #define ll long long int #define vl vector<ll> #define vpl vector<pair<ll, ll>> #define pl pair<ll, ll> #define pll pair<ll, pl> #define vvl vector<vl> #define mxe(x, n) *max_element(x, x + n) #define mne(x, n) *min_element(x, x + n) #define mp make_pair #define pb push_back #define fs first #define sd second #define fr front() #define bk back() #define rr return #define N 300005 #define inf 1e18 + 7 bool rsort1(const pair<ll, ll> &a, const pair<ll, ll> &b) { return (a.first > b.first); } bool rsort2(const pair<ll, ll> &a, const pair<ll, ll> &b) { return (a.second > b.second); } bool sort2(const pair<ll, ll> &a, const pair<ll, ll> &b) { return (a.second < b.second); } bool isPrime(ll p) { for (ll i = 2; i * i <= p; i++) if (p % i == 0) rr false; rr true; } void sieve(ll n, vl &v) { v.pb(2); bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (ll i = 3; i * i <= n; i += 2) if (prime[i]) for (ll j = i; j * i <= n; j += 2) prime[j * i] = false; for (ll i = 3; i <= n; i += 2) if (prime[i]) v.pb(i); } ll phi(ll n) { ll result = n; for (ll p = 2; p * p <= n; p++) { if (n % p == 0) { while (n % p == 0) n /= p; result -= result / p; } } if (n > 1) result -= result / n; return result; } int main() { sup; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif ll k, n; cin >> k >> n; if (k * 500 >= n) pr("Yes"); else pr("No"); return 0; }
#include <bits/stdc++.h> using namespace std; #define in(s) \ ll s; \ cin >> s #define pr(r) cout << r << endl #define prr(m, n) cout << m << " " << n << endl #define prarr(a, i, n) fab(j, i, n) cout << a[j] << " " #define wh(t) \ ll t; \ cin >> t; \ while (t--) #define sz(u) u.size() #define all(v) v.begin(), v.end() #define arr(a, n) \ ll a[n]; \ fab(i, 0, n) cin >> a[i] #define showdp(d, n, m) \ fab(i, 0, n) { \ fab(j, 0, m) if (d[i][j] == -1 || d[i][j] > 9) cout << d[i][j] << " "; \ else cout << " " << d[i][j] << " "; \ cout << endl; \ } #define prs(u) cout << u << " " #define fab(i, a, b) for (ll i = a; i < b; i++) #define fba(i, b, a) for (ll i = b; i >= a; i--) #define sup \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define MX 1000000007 #define MAX 10000007 #define endl "\n" #define ll long long int #define vl vector<ll> #define vpl vector<pair<ll, ll>> #define pl pair<ll, ll> #define pll pair<ll, pl> #define vvl vector<vl> #define mxe(x, n) *max_element(x, x + n) #define mne(x, n) *min_element(x, x + n) #define mp make_pair #define pb push_back #define fs first #define sd second #define fr front() #define bk back() #define rr return #define N 300005 #define inf 1e18 + 7 bool rsort1(const pair<ll, ll> &a, const pair<ll, ll> &b) { return (a.first > b.first); } bool rsort2(const pair<ll, ll> &a, const pair<ll, ll> &b) { return (a.second > b.second); } bool sort2(const pair<ll, ll> &a, const pair<ll, ll> &b) { return (a.second < b.second); } bool isPrime(ll p) { for (ll i = 2; i * i <= p; i++) if (p % i == 0) rr false; rr true; } void sieve(ll n, vl &v) { v.pb(2); bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (ll i = 3; i * i <= n; i += 2) if (prime[i]) for (ll j = i; j * i <= n; j += 2) prime[j * i] = false; for (ll i = 3; i <= n; i += 2) if (prime[i]) v.pb(i); } ll phi(ll n) { ll result = n; for (ll p = 2; p * p <= n; p++) { if (n % p == 0) { while (n % p == 0) n /= p; result -= result / p; } } if (n > 1) result -= result / n; return result; } int main() { sup; /*#ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); freopen("error.txt","w",stderr); #endif*/ ll k, n; cin >> k >> n; if (k * 500 >= n) pr("Yes"); else pr("No"); return 0; }
replace
99
104
99
104
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int ans = 0; for (int i = 0; i < n; i++) { if (s.at(i) == 'A' && s.at(i + 1) == 'B' && s.at(i + 2) == 'C') ans++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int ans = 0; for (int i = 0; i < n - 2; i++) { if (s.at(i) == 'A' && s.at(i + 1) == 'B' && s.at(i + 2) == 'C') ans++; } cout << ans << endl; return 0; }
replace
10
11
10
11
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; int main() { int N, count = 0; string S; cin >> N; cin >> S; rep(i, N) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') { count++; } } cout << count << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; int main() { int N, count = 0; string S; cin >> N; cin >> S; // rep(i, N) { N-1かN番目に'A'があったらN+1かN+2番目まで操作を行ってしまう rep(i, N - 2) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') { count++; } } cout << count << endl; return 0; }
replace
10
11
10
12
0
p02812
C++
Runtime Error
#include <iostream> using namespace std; int main(void) { int count = 0; int N; string s; cin >> N >> s; for (int i = 0; i < N; i++) { if (s.at(i) == 'A') { if (i + 2 <= N) { if (s.at(i + 1) == 'B') { if (s.at(i + 2) == 'C') { count++; } } } } } cout << count << endl; }
#include <iostream> using namespace std; int main(void) { int count = 0; int N; string s; cin >> N >> s; for (int i = 0; i < N; i++) { if (s.at(i) == 'A') { if (i + 2 <= N - 1) { if (s.at(i + 1) == 'B') { if (s.at(i + 2) == 'C') { count++; } } } } } cout << count << endl; }
replace
10
11
10
11
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N; int count = 0; string S; cin >> N >> S; for (int i = 0; i < N; i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') { count++; } } cout << count << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; int count = 0; string S; cin >> N >> S; for (int i = 0; i < N - 2; i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') { count++; } } cout << count << endl; return 0; }
replace
9
10
9
10
0
p02812
C++
Runtime Error
#include <iostream> using namespace std; int main() { string objStr; int len, count = 0; cin >> len; getline(cin, objStr); for (int i = 0; i <= len - 3; i++) { if (objStr.substr(i, 3) == "ABC") { ++count; i = i + 2; } } cout << count << endl; }
#include <iostream> using namespace std; int main() { string objStr; int len, count = 0; cin >> len; cin >> objStr; for (int i = 0; i <= len - 3; i++) { if (objStr.substr(i, 3) == "ABC") { ++count; i = i + 2; } } cout << count << endl; }
replace
7
8
7
8
-6
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr: __pos (which is 1) > this->size() (which is 0)
p02812
C++
Runtime Error
#include <iostream> #include <string> using namespace std; int main() { string s; cin >> s; int sum = 0; for (int i = 0; i < s.size() - 2; i++) { if (s.substr(i, 3) == "ABC") { sum++; } } cout << sum << endl; }
#include <iostream> #include <string> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int sum = 0; for (int i = 0; i < s.size() - 2; i++) { if (s.substr(i, 3) == "ABC") { sum++; } } cout << sum << endl; }
insert
5
5
5
7
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define srep(i, s, t) for (int i = s; i < t; ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) using namespace std; typedef long long int ll; typedef pair<int, int> P; #define yn \ { puts("Yes"); } \ else { \ puts("No"); \ } #define MAX_N 200005 int main() { string s; cin >> s; int ans = 0; rep(i, s.size() - 2) { if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') ans++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define srep(i, s, t) for (int i = s; i < t; ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) using namespace std; typedef long long int ll; typedef pair<int, int> P; #define yn \ { puts("Yes"); } \ else { \ puts("No"); \ } #define MAX_N 200005 int main() { int n; cin >> n; string s; cin >> s; int ans = 0; rep(i, s.size() - 2) { if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') ans++; } cout << ans << endl; return 0; }
insert
15
15
15
17
0
p02812
C++
Runtime Error
#include <iostream> using namespace std; int main() { int n, count = 0; cin >> n; string s; for (int i = 0; i < n; ++i) cin >> s[i]; for (int i = 0; i < n; ++i) { if (s.substr(i, 3) == "ABC") count++; } cout << count << endl; }
#include <iostream> using namespace std; int main() { int n, count = 0; cin >> n; string s; for (int i = 0; i < n; ++i) cin >> s[i]; for (int i = 0; i < n - 2; ++i) { if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') count++; } cout << count << endl; }
replace
9
11
9
11
-6
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr: __pos (which is 1) > this->size() (which is 0)
p02812
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n, ans = 0; char ch[n + 1]; cin >> n; for (int i = 1; i <= n; i++) cin >> ch[i]; for (int i = 3; i <= n; i++) { if (ch[i] == 'C' && ch[i - 1] == 'B' && ch[i - 2] == 'A') ans++; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, ans = 0; string s; cin >> n >> s; for (int i = 2; i < n; i++) { if (s[i] == 'C' && s[i - 1] == 'B' && s[i - 2] == 'A') ans++; } cout << ans; return 0; }
replace
4
10
4
8
TLE
p02812
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; int main() { string S; cin >> S; long ans = 0; for (int i = 0; i < S.size() - 2; i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') ans++; } cout << ans << endl; }
#include "bits/stdc++.h" using namespace std; int main() { int N; cin >> N; string S; cin >> S; long ans = 0; for (int i = 0; i < S.size() - 2; i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') ans++; } cout << ans << endl; }
insert
3
3
3
5
0
p02812
C++
Runtime Error
#include <iostream> #include <string> int main() { int N; std::string S; std::cin >> N >> S; int result = 0; for (int i = 0; i != N - 2; i++) { if (S[i] == 'A' && S[i + 1] == 'B' && S[i + 2] == 'C') { result++; i = i + 2; } } std::cout << result << std::endl; return 0; }
#include <iostream> #include <string> int main() { int N; std::string S; std::cin >> N >> S; int result = 0; for (int i = 0; i < N - 2; i++) { if (S[i] == 'A' && S[i + 1] == 'B' && S[i + 2] == 'C') { result++; i = i + 2; } } std::cout << result << std::endl; return 0; }
replace
9
10
9
10
-11
p02812
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { long n, ans = 0; string s; cin >> n >> s; for (int i = 0; i < n; i++) { if (s.at(i) == 'A') { if (s.at(i + 1) == 'B') { if (s.at(i + 2) == 'C') { ans++; } } } } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { long n, ans = 0; string s; cin >> n >> s; for (int i = 2; i < n; i++) { if (s.at(i) == 'C') { if (s.at(i - 1) == 'B') { if (s.at(i - 2) == 'A') { ans++; } } } } cout << ans; }
replace
7
11
7
11
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N; int Y = 0; string S; cin >> N >> S; for (int X = 0; X < N; X++) { if (X + 2 <= N) { if (S.at(X) == 'A' && S.at(X + 1) == 'B' && S.at(X + 2) == 'C') { Y++; } } } cout << Y << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N; int Y = 0; string S; cin >> N >> S; for (int X = 0; X < N; X++) { if (X + 2 < N) { if (S.at(X) == 'A' && S.at(X + 1) == 'B' && S.at(X + 2) == 'C') { Y++; } } } cout << Y << endl; }
replace
9
10
9
10
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define p_q priority_queue #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) int main() { int n; cin >> n; string s; cin >> s; int co = 0; for (int i = 0; i < n; i++) { if (s.at(i) == 'A') { if (s.at(i + 1) == 'B') { if (s.at(i + 2) == 'C') { co++; } } } } cout << co << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define p_q priority_queue #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) int main() { int n; cin >> n; string s; cin >> s; int co = 0; for (int i = 0; i + 2 < n; i++) { if (s.at(i) == 'A') { if (s.at(i + 1) == 'B') { if (s.at(i + 2) == 'C') { co++; } } } } cout << co << endl; }
replace
14
15
14
15
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int K; string S; cin >> K >> S; int res = 0; for (int i = 0; i < (K - 1); i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') { res++; } } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int K; string S; cin >> K >> S; int res = 0; for (int i = 0; i < (K - 2); i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') { res++; } } cout << res << endl; return 0; }
replace
8
9
8
9
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define endl '\n' #define MOD 1000000007 #define maxn 100010 void solve() { int n; string s; cin >> s; int ans = 0; for (int i = 0; i < s.size() - 2; i++) { if (s.substr(i, 3) == "ABC") ans++; } cout << ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define endl '\n' #define MOD 1000000007 #define maxn 100010 void solve() { int n; cin >> n; string s; cin >> s; int ans = 0; for (int i = 0; i < s.size() - 2; i++) { if (s.substr(i, 3) == "ABC") ans++; } cout << ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; while (t--) solve(); return 0; }
insert
11
11
11
13
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using Graph = vector<vector<int>>; // グラフ型 typedef long long ll; #define _GLIBCXX_DEBUG /* // 深さ優先探索 vector<bool> seen; int dfs(const Graph &G, ll v) { seen[v] = true; // v を訪問済にする int score = 0; // v から行ける各頂点 next_v について for (auto next_v : G[v]) { if (seen[next_v]) continue; // next_v が探索済だったらスルー // 負荷さ優先探索中の処理 dfs(G, next_v); // 再帰的に探索, 毎回mat渡してええの? } return score; } */ /* //桁数の計算 ll keta(ll num){ ll ans= 0 ; ll rem; for (ll i = 4; i >= 0 ; i--){ rem = pow(10,i); ans += (num / rem); num = num % rem; } return ans; } */ int main() { ll N; string S; cin >> N >> S; ll ans = 0; for (ll i = 0; i < N; i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') ans++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using Graph = vector<vector<int>>; // グラフ型 typedef long long ll; #define _GLIBCXX_DEBUG /* // 深さ優先探索 vector<bool> seen; int dfs(const Graph &G, ll v) { seen[v] = true; // v を訪問済にする int score = 0; // v から行ける各頂点 next_v について for (auto next_v : G[v]) { if (seen[next_v]) continue; // next_v が探索済だったらスルー // 負荷さ優先探索中の処理 dfs(G, next_v); // 再帰的に探索, 毎回mat渡してええの? } return score; } */ /* //桁数の計算 ll keta(ll num){ ll ans= 0 ; ll rem; for (ll i = 4; i >= 0 ; i--){ rem = pow(10,i); ans += (num / rem); num = num % rem; } return ans; } */ int main() { ll N; string S; cin >> N >> S; ll ans = 0; for (ll i = 0; i < N - 2; i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') ans++; } cout << ans << endl; return 0; }
replace
46
47
46
47
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main(void) { int N; string S; cin >> N >> S; int cnt = 0; for (int i = 0; i < N; i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') { cnt++; } } cout << cnt << endl; }
#include <bits/stdc++.h> using namespace std; int main(void) { int N; string S; cin >> N >> S; int cnt = 0; for (int i = 0; i < N - 2; i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') { cnt++; } } cout << cnt << endl; }
replace
9
10
9
10
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; int main() { int N; string S; cin >> N >> S; int count = 0; rep(i, N) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') { count++; } } cout << count << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; int main() { int N; string S; cin >> N >> S; int count = 0; for (int i = 0; i < N - 2; i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') { count++; } } cout << count << endl; }
replace
10
11
10
11
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N; string S; cin >> N >> S; int count = 0; for (int i = 0; i < N; i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') { count++; } } cout << count << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N; string S; cin >> N >> S; int count = 0; for (int i = 2; i < N; i++) { if (S.at(i - 2) == 'A' && S.at(i - 1) == 'B' && S.at(i) == 'C') { count++; } } cout << count << endl; }
replace
7
9
7
9
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int cnt = 0; for (int i = 0; i < n - 1; i++) { if (s.at(i) == 'A' && s.at(i + 1) == 'B' && s.at(i + 2) == 'C') cnt++; } cout << cnt; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int cnt = 0; for (int i = 0; i < n - 2; i++) { if (s.at(i) == 'A' && s.at(i + 1) == 'B' && s.at(i + 2) == 'C') cnt++; } cout << cnt; return 0; }
replace
9
10
9
10
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; int res = 0; cin >> n >> s; for (int i = 0; i < n; i++) { if (s.at(i) == 'A' && s.at(i + 1) == 'B' && s.at(i + 2) == 'C') { res++; } } cout << res << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; int res = 0; cin >> n >> s; for (int i = 0; i < n - 2; i++) { if (s.at(i) == 'A' && s.at(i + 1) == 'B' && s.at(i + 2) == 'C') { res++; } } cout << res << endl; }
replace
9
10
9
10
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> #define ROOP(i, N) for (int i = 0; i < N; i++) #define RVROOP(i, N) for (int i = N; i >= 0; i--) #define INF 1e9 #define ALL(v) v.begin(), v.end() using namespace std; typedef long long ll; int main() { int N; cin >> N; string S; cin >> S; int ans = 0; for (int i = 0; i < N; i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') { ans++; } } cout << ans << endl; }
#include <bits/stdc++.h> #define ROOP(i, N) for (int i = 0; i < N; i++) #define RVROOP(i, N) for (int i = N; i >= 0; i--) #define INF 1e9 #define ALL(v) v.begin(), v.end() using namespace std; typedef long long ll; int main() { int N; cin >> N; string S; cin >> S; int ans = 0; for (int i = 0; i <= N - 3; i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') { ans++; } } cout << ans << endl; }
replace
14
15
14
15
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N; int abc = 0; string S; cin >> N; cin >> S; for (int i = 0; i < N; i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') { abc++; } } cout << abc << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N; int abc = 0; string S; cin >> N; cin >> S; for (int i = 0; i < N - 2; i++) { if (S.at(i) == 'A' && S.at(i + 1) == 'B' && S.at(i + 2) == 'C') { abc++; } } cout << abc << endl; }
replace
9
10
9
10
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string S; int N; int count = 0; cin >> N >> S; for (int i = 0; i < N; i++) { if (S.at(i) == 'A') { if (S.at(i + 1) == 'B') { if (S.at(i + 2) == 'C') { count++; } } } } cout << count << endl; }
#include <bits/stdc++.h> using namespace std; int main() { string S; int N; int count = 0; cin >> N >> S; for (int i = 0; i <= N - 3; i++) { if (S.at(i) == 'A') { if (S.at(i + 1) == 'B') { if (S.at(i + 2) == 'C') { count++; } } } } cout << count << endl; }
replace
8
9
8
9
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; string s; cin >> s; int ans = 0; for (int i = 0; i < N; i++) { if (s.at(i) == 'A') { if (s.at(i + 1) == 'B' && s.at(i + 2) == 'C') { i += 2; ans++; } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; string s; cin >> s; int ans = 0; for (int i = 0; i < N - 2; i++) { if (s.at(i) == 'A' && s.at(i + 1) == 'B' && s.at(i + 2) == 'C') { ans++; } } cout << ans << endl; }
replace
8
14
8
11
0
p02812
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int count = 0; for (int i = 0; i < n; i++) { if (s.at(i) == 'A') { if (s.at(i + 1) == 'B') { if (s.at(i + 2) == 'C') { count++; } } } } cout << count << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int count = 0; for (int i = 0; i < n - 2; i++) { if (s.at(i) == 'A') { if (s.at(i + 1) == 'B') { if (s.at(i + 2) == 'C') { count++; } } } } cout << count << endl; }
replace
10
11
10
11
0