text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; using int64 = long long; constexpr int DEBUG = 0; struct Item { int64 a, b; Item(int64 a, int64 b) : a(a), b(b) {} }; int64 GCD(int64 a, int64 b) { if (a < b) return GCD(b, a); int64 r = a % b; if (r == 0) return b; else return GCD(b, r); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<Item> items; int64 a_sum = 0; for (int i = 0; i < n; i++) { int64 a, b; cin >> a >> b; items.emplace_back(a, b); a_sum += a; } sort(items.begin(), items.end(), [](const Item& item1, const Item& item2) { return max(item1.a, item1.b) > max(item2.a, item2.b); }); int64 t_sum = 0; int64 t_count = 0; for (int i = 0; i < n; i++) { t_sum += max(items[i].a, items[i].b); t_count++; if (t_sum >= a_sum) break; } if (DEBUG) cout << "t_sum: " << t_sum << endl; if (DEBUG) cout << "t_count: " << t_count << endl; int64 max_num = 0; int64 max_den = 1; for (int i = 0; i < n; i++) { if (i < t_count) { if (t_sum - max(items[i].a, items[i].b) + items[i].b >= a_sum) { int64 r = a_sum - (t_sum - max(items[i].a, items[i].b)); int64 num = items[i].b - r; int64 den = items[i].b; if (num * max_den > max_num * den) { max_num = num; max_den = den; } } } else { if (t_sum - max(items[t_count - 1].a, items[t_count - 1].b) + items[i].b >= a_sum) { int64 r = a_sum - (t_sum - max(items[t_count - 1].a, items[t_count - 1].b)); int64 num = items[i].b - r; int64 den = items[i].b; if (num * max_den > max_num * den) { max_num = num; max_den = den; } } } } if (DEBUG) cout << "max_num: " << max_num << endl; if (DEBUG) cout << "max_den: " << max_den << endl; int64 ans_num = (n - (t_count)) * max_den + max_num; int64 ans_den = n * max_den; if (ans_num == 0) ans_den = 1; else { int64 d = GCD(ans_num, ans_den); ans_num /= d; ans_den /= d; } cout << ans_num << " " << ans_den << endl; }
#include<bits/stdc++.h> #define ll long long #define fornum(A,B,C) for(A=B;A<C;A++) #define mp make_pair #define pii pair<int,int> #define pll pair<ll,ll> using namespace std; ///////////////////////////////////////////////////// ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b, a % b); } ll N; ll A[101010], B[101010],As; pll BA[101010]; pll abv[101010]; ll mk[101010],Sm[101010]; pll ans; ll i, j, k; bool comp(pll a){ if(ans.first/ans.second<a.first/a.second){ return true; } if(ans.first/ans.second>a.first/a.second){ return false; } ll aa = ans.first % ans.second; ll bb = a.first % a.second; return aa * a.second < bb * ans.second; } pll solve(ll b,ll i){ ll l = 0, r = N; while(l<r){ ll c = (l + r + 1) / 2; ll a = Sm[c] - (c >= i + 1 ? abv[i].first : 0); if (a < As - b) { l = c; } else { r = c - 1; } } if(l==N) return {0l, 1l}; ++l; ll a = As - Sm[l] + (l >= i + 1 ? abv[i].first : 0); //printf("%lld %lld\n", l, a); if(a<0) return {0l, 1l}; return {(N - l + (l >= i + 1 ? 1 : 0)) * b - a, b}; } int main(){ scanf("%lld",&N); As = 0; fornum(i,0,N){ scanf("%lld%lld", &A[i], &B[i]); abv[i] = {max(A[i], B[i]), i}; As += A[i]; } if(N==1){ if(A[0]>=B[0]){ printf("0 1"); }else{ j = gcd(A[0], B[0]); A[0] /= j; B[0] /= j; printf("%lld %lld", B[0] - A[0], B[0]); } return 0; } sort(abv, abv + N,greater<pll>()); fornum(i,0,N){ Sm[i + 1] = Sm[i] + abv[i].first; } ans = {0ll, 1ll}; fornum(i,0,N){ pll a = solve(B[abv[i].second], i); if(comp(a)){ ans = a; } } ans.second *= N; j = gcd(ans.first, ans.second); ans.first /= j; ans.second /= j; printf("%lld %lld", ans.first, ans.second); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < (n); i++) #define reps(i, n) for (int i = 1; i <= (n); i++) #define all(x) (x).begin(), (x).end() #define uniq(x) (x).erase(unique(all(x)), end(x)) #define bit(n) (1LL << (n)) #define cdiv(a, b) (((a) - 1) / (b) + 1) #define dump(x) cerr << #x " = " << (x) << endl using vint = vector<int>; using vvint = vector<vint>; using pint = pair<int, int>; using vpint = vector<pint>; template<typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; constexpr double PI = 3.1415926535897932384626433832795028; constexpr int DY[8] = {0, 1, 0, -1, 1, 1, -1, -1}; constexpr int DX[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int gcd(int a, int b) { while (b) { swap(a %= b, b); } return a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } template<typename T> void fin(T mes) { cout << mes << endl; exit(0); } template<typename T, typename U> bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template<typename T, typename U> bool chmin(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template<typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &rhs) { os << "(" << rhs.first << ", " << rhs.second << ")"; return os; } template<typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) { os << "{"; for (auto itr = rhs.begin(); itr != rhs.end(); itr++) { os << *itr << (next(itr) != rhs.end() ? ", " : ""); } os << "}"; return os; } struct setup { static constexpr int PREC = 20; setup() { cout << fixed << setprecision(PREC); cerr << fixed << setprecision(PREC); }; } setup; struct rational { long long num, den; static long long gcd(long long a, long long b) { if (a < 0) { a = -a; } if (b < 0) { b = -b; } while (b) { std::swap(a %= b, b); } return a; } void reduce() { long long g = gcd(num, den); num /= g, den /= g; } rational(long long num_ = 0, long long den_ = 1, bool reduction = true) : num(num_), den(den_) { if (reduction) { reduce(); } if (den < 0) { num = -num, den = -den; } } bool operator==(const rational &rhs) const { return num == rhs.num && den == rhs.den; } bool operator!=(const rational &rhs) const { return std::rel_ops::operator!=(*this, rhs); } bool operator<(const rational &rhs) const { return to_double() < rhs.to_double(); } bool operator>(const rational &rhs) const { return std::rel_ops::operator>(*this, rhs); } bool operator<=(const rational &rhs) const { return std::rel_ops::operator<=(*this, rhs); } bool operator>=(const rational &rhs) const { return std::rel_ops::operator>=(*this, rhs); } rational &operator+=(const rational &rhs) { num = num * rhs.den + rhs.num * den, den *= rhs.den; reduce(); return *this; } rational &operator-=(const rational &rhs) { return *this += -rhs; } rational &operator*=(const rational &rhs) { num *= rhs.num, den *= rhs.den; reduce(); return *this; } rational &operator/=(const rational &rhs) { return *this *= rhs.inv(); } rational operator+() const { return *this; } rational operator-() const { return rational(-num, den, false); } rational operator+(const rational &rhs) const { return rational(*this) += rhs; } rational operator-(const rational &rhs) const { return rational(*this) -= rhs; } rational operator*(const rational &rhs) const { return rational(*this) *= rhs; } rational operator/(const rational &rhs) const { return rational(*this) /= rhs; } rational inv() const { return rational(den, num, false); } rational pow(long long n) const { if (n < 0) { return pow(-n).inv(); } rational ret(1, 1, false), mul = *this; while (n > 0) { if (n & 1) { ret.num *= mul.num, ret.den *= mul.den; } mul.num *= mul.num, mul.den *= mul.den; n >>= 1; } return ret; } double to_double() const { return (double) num / den; } friend std::istream &operator>>(std::istream &is, rational &rhs) { long long n, d; char s; is >> n >> s >> d; rhs = rational(n, d); return is; } friend std::ostream &operator<<(std::ostream &os, const rational &rhs) { return os << rhs.num << " / " << rhs.den; } }; template<typename F> long long discrete_binary_search(long long ok, long long ng, F is_ok) { while (abs(ok - ng) > 1) { long long mid = (ok + ng) / 2; (is_ok(mid) ? ok : ng) = mid; } return ok; } int N; int A[110000], B[110000]; pint mB[110000]; int mcc[110000]; signed main() { cin >> N; int S = 0; rep(i, N) { cin >> A[i] >> B[i]; S += A[i]; mB[i] = {max(A[i], B[i]), B[i]}; } sort(mB, mB + N), reverse(mB, mB + N); rep(i, N) { mcc[i + 1] = mcc[i] + mB[i].first; } rational ans = 0; rep(i, N) { int k = discrete_binary_search(N - 1, -1, [&](int x) { int a; if (x > i) { a = mcc[x + 1] - mB[i].first; } else { a = mcc[x]; } return a >= S - mB[i].second; }); int a; if (k > i) { a = mcc[k + 1] - mB[i].first; } else { a = mcc[k]; } int b = S - a; if (b < 0) { continue; } chmax(ans, -(rational(b) / mB[i].second + k) + N); } ans /= N; cout << ans.num << " " << ans.den << endl; }
#include<deque> #include<queue> #include<vector> #include<algorithm> #include<iostream> #include<set> #include<cmath> #include<tuple> #include<string> #include<chrono> #include<functional> #include<iterator> #include<random> #include<unordered_set> #include<array> #include<map> #include<iomanip> #include<assert.h> #include<list> #include<bitset> #include<stack> #include<memory> #include<numeric> using namespace std; using namespace std::chrono; typedef long long int llint; typedef long double lldo; #define mp make_pair #define mt make_tuple #define pub push_back #define puf push_front #define pob pop_back #define pof pop_front #define fir first #define sec second #define res resize #define ins insert #define era erase #define REP(i, n) for(int64 i = 0;i < (n);i++) /*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/ const int mod=998244353 ; const llint inf=2.19e15+1; const long double pai=3.141592653589793238462643383279502884197; const long double eps=1e-10; template <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;} template <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;} llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);} llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;} template<class T> void SO(T& ve){sort(ve.begin(),ve.end());} template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());} template<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();} template<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();} //13:14~ int main(void){ cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false); llint n,i;cin>>n; llint At=0,ans=0; vector<pair<llint,llint>>hei(n);//通常move,ずらしmove for(i=0;i<n;i++){ llint A,B;cin>>A>>B; if(A<=B){At+=B-A;hei[i]=mp(B,B);} else{hei[i]=mp(A,B);} } SO(hei); while(At>=hei[ans].fir){At-=hei[ans].fir;ans++;} //2パターン llint bbb=0; for(i=0;i<=ans;i++){chmax(bbb,hei[i].sec);} llint P=max(bbb+At-hei[ans].fir,0LL),Q=bbb; //At/aaa for(i=ans;i<n;i++){ llint nP=At-hei[i].fir+hei[i].sec; llint nQ=hei[i].sec; if(P*nQ<Q*nP){P=nP,Q=nQ;}//1- At/bbbが大きい } P+=ans*Q; Q*=n; llint G=gcd(P,Q); cout<<P/G<<" "<<Q/G<<endl; return 0; } /* s---r---|--- A B C Aゾーン すぬけ早い Bゾーン すぬけ有利 Cゾーン B-A */
//Zory-2019 #include<bits/stdc++.h> using namespace std; typedef long long ll; #define pr pair<int,int> #define FR first #define SE second #define MP make_pair #define PB push_back #define vc vector #define all(x) (x).begin(),(x).end() #define sz(x) ((int)(x).size()) #define bin(x) (1ll<<(x)) #define fo(i,l,r) for(int i=(l),I=(r);i<=I;i++) #define fd(i,r,l) for(int i=(r),I=(l);i>=I;i--) #ifdef DEBUG #define GG(x) assert(x) #else #define GG(x) (x) #endif namespace mine { ll qread() { ll ans=0,f=1;char c=getchar(); while(c<'0' or c>'9') {if(c=='-')f=-1;c=getchar();} while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar(); return ans*f; } void write(ll num){if(num<0) putchar('-'),num=-num;if(num>=10) write(num/10);putchar('0'+num%10);} void write1(ll num){write(num);putchar(' ');} void write2(ll num){write(num);putchar('\n');} template<typename T> void chmax(T &x,const T y) {x=(x>y?x:y);} template<typename T> void chmin(T &x,const T y) {x=(x<y?x:y);} ll gcd(ll x,ll y){return y?gcd(y,x%y):x;} const int INF=0x3f3f3f3f; const int MOD=1e9+7; int mm(const int x){return x>=MOD?x-MOD:x;} template<typename T> void add(T &x,const int &y){x=(x+y>=MOD?x+y-MOD:x+y);} ll qpower(ll x,ll e,int mod=MOD){ll ans=1;GG(e>=0);while(e){if(e&1)ans=ans*x%mod;x=x*x%mod;e>>=1;}return ans;} ll invm(ll x){return qpower(x,MOD-2);} const int N=1e5+10; pr a[N];bool cmp(pr x,pr y){return max(x.FR,x.SE)>max(y.FR,y.SE);} ll sum[N]; void main() { int n=qread();fo(i,1,n) a[i].FR=qread(),a[i].SE=qread();sort(a+1,a+n+1,cmp); ll S=0;fo(i,1,n) S+=a[i].FR,a[i].FR=max(a[i].FR,a[i].SE),sum[i]=sum[i-1]+a[i].FR; ll ansx=0,ansy=1; fo(k,1,n) { int fl=0,fr=n,q=-1; while(fl<=fr) { int mid=(fl+fr)/2; if(sum[mid]-(k<=mid?a[k].FR:0)+a[k].SE>=S) q=mid,fr=mid-1; else fl=mid+1; } if(q<0) continue; if(sum[q]-(k<=q?a[k].FR:0)>=S) continue;//debug ll nowx=ll(n-(q-(k<=q)))*a[k].SE-S+sum[q]-(k<=q?a[k].FR:0),nowy=1ll*a[k].SE*n; ll d=gcd(nowx,nowy);nowx/=d,nowy/=d; if(1.0*nowx*ansy>1.0*ansx*nowy) ansx=nowx,ansy=nowy;//爆ll } write1(ansx),write(ansy); } };//(ans+MOD)%MOD signed main() { srand(time(0)); mine::main(); }
#include<bits/stdc++.h> #define llong long long #define mkpr make_pair #define x first #define y second #define iter iterator #define riter reversed_iterator #define y1 Lorem_ipsum_ #define tm dolor_sit_amet_ using namespace std; inline int read() { int x = 0,f = 1; char ch = getchar(); for(;!isdigit(ch);ch=getchar()) {if(ch=='-') f = -1;} for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;} return x*f; } const int mxN = 1e5; struct Element { llong a,b; bool operator <(const Element &arg) const {return max(a,b)>max(arg.a,arg.b);} } a[mxN+3]; llong s[mxN+3]; int n; llong sum; struct Fraction { llong a,b,c; Fraction() {} Fraction(llong _a,llong _b,llong _c) {a = _a,b = _b,c = _c;} bool operator <(const Fraction &arg) const { if(a!=arg.a) {return a<arg.a;} return b*arg.c<arg.b*c; } }; llong gcd(llong x,llong y) {return y==0?x:gcd(y,x%y);} int main() { n = read(); for(int i=1; i<=n; i++) scanf("%lld%lld",&a[i].a,&a[i].b),sum += a[i].a; sort(a+1,a+n+1); for(int i=1; i<=n; i++) s[i] = s[i-1]+max(a[i].a,a[i].b); Fraction ans(n,0,1); for(int i=1; i<=n; i++) { int pos = lower_bound(s+1,s+n+1,sum-a[i].b)-s,cnt = pos; llong rst = sum-s[pos]; if(pos>=i) {pos = lower_bound(s+1,s+n+1,sum-a[i].b+max(a[i].a,a[i].b))-s,cnt = pos-1,rst = sum-s[pos]+max(a[i].a,a[i].b);} if(pos>n) {continue;} rst = max(0ll,rst); Fraction tmp(cnt,rst,a[i].b); if(tmp<ans) {ans = tmp;} } llong x = ans.c*ans.a+ans.b,y = ans.c*n; x = y-x; llong g = gcd(x,y); x/=g,y/=g; printf("%lld %lld\n",x,y); return 0; }
// ※※※ 解答不能 ※※※ // scott_wu氏 // https://atcoder.jp/contests/agc040/submissions/8277503 #include <bits/stdc++.h> using namespace std; using LL = long long; LL nt; vector<pair<LL, LL>> res; LL gcd(LL a, LL b){ if(b == 0) return a; return gcd(b, a % b); } int main(){ int N, a, b; scanf("%d", &N); for(int i = 0; i < N; i++){ scanf("%d %d", &a, &b); if(a < b) nt += b - a, res.push_back(make_pair(b, b)); else res.push_back (make_pair (a, b)); } sort(res.begin(), res.end()); LL shi = 0; LL nwhole = 0; LL nbest = 1, dbest = 1; for(int i = 0; i < N; i++){ if(res[i].first <= nt){ nt -= res[i].first; shi = max(shi, res[i].second); nwhole++; continue; }else{ LL nnum = res[i].first - nt; LL dnum = max (shi, res[i].second); LL g = gcd(nnum, dnum); nnum /= g; dnum /= g; if(nnum * dbest < dnum * nbest){ nbest = nnum; dbest = dnum; } } } LL tnum = (nwhole + 1) * dbest - nbest; LL tden = N * dbest; LL g = gcd(tnum, tden); tnum /= g; tden /= g; printf("%lld %lld\n", tnum, tden); return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define pll pair<ll,ll> struct node { ll x,y; }a[100010]; int n; inline int rd() { int x=0;char ch=getchar(); for (;ch<'0'||ch>'9';ch=getchar()); for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; return x; } inline ll gcd(ll x,ll y) { return (!y)?x:gcd(y,x%y); } inline bool cmp(const node &x,const node &y) { return max(x.x,x.y)>max(y.x,y.y); } inline pll check(pll x,pll y) { return (x.first*y.second>x.second*y.first)?x:y; } int main() { n=rd(); for (int i=1;i<=n;i++) a[i].x=rd(),a[i].y=rd(); ll sum=0; for (int i=1;i<=n;i++) sum+=a[i].x; sort(a+1,a+n+1,cmp); int id=0;ll s=0; for (int i=1;i<=n;i++) { if (s+max(a[i].x,a[i].y)>sum) break; s+=max(a[i].x,a[i].y);id=i; } if (id==n) { puts("0 1");return 0; } pll ans(0,1); for (int i=1;i<=n;i++) { if (i>id) { if (s+a[i].y>=sum) ans=check(ans,pll(a[i].y-sum+s,a[i].y)); } else { ll now=s-max(a[i].x,a[i].y)+max(a[id+1].x,a[id+1].y); if (now+a[i].y>=sum) ans=check(ans,pll(a[i].y-sum+now,a[i].y)); } } ans.first+=ans.second*(n-id-1);ans.second*=n; ll hh=gcd(ans.first,ans.second); printf("%lld %lld\n",ans.first/hh,ans.second/hh); return 0; }
#include<bits/stdc++.h> #define ll long long #define ull unsigned ll #define uint unsigned #define pii pair<int,int> #define pll pair<ll,ll> #define IT iterator #define PB push_back #define fi first #define se second #define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++) #define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--) #define UPD(x,y) (((x)+=(y))>=mo?x-=mo:233) #define CLR(a,v) memset(a,v,sizeof(a)); #define CPY(a,b) memcpy(a,b,sizeof(a)); #define debug puts("wzpakking") using namespace std; const int N=100005; int n,k; pll a[N]; ll As,Bs; ll gcd(ll x,ll y){ return y?gcd(y,x%y):x; } int main(){ scanf("%d",&n); For(i,1,n){ int x,y; scanf("%d%d",&x,&y); a[i]=pll(max(x,y),y); As+=x; } sort(a+1,a+n+1,greater<pll>()); k=Bs=0; for (;k<=n;++k){ Bs+=a[k].fi; if (Bs>=As) break; } //printf("%lld %lld\n",As,Bs); if (Bs<As) return puts("0 1\n"),0; ll fm=1,fz=1; For(i,1,n){ ll val=As-Bs+a[min(i,k)].fi; if (val<=a[i].se) if (val*fm<fz*a[i].se) fm=a[i].se,fz=val; } //printf("%lld %lld\n",fm,fz); fz=(fm-fz)+(n-k)*fm; fm*=n; ll G=gcd(fz,fm); fz/=G; fm/=G; printf("%lld %lld\n",fz,fm); } /* 所有选的y越大越在后 x<y一定会选 剩下选若干 */
#include <cstdio> #include <cstring> #include <iostream> #include <string> #include <cmath> #include <bitset> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <algorithm> #include <complex> #include <unordered_map> #include <unordered_set> #include <random> #include <cassert> #include <fstream> #include <utility> #include <functional> #include <time.h> #include <stack> #include <array> #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair<ll, ll> P; ll gcd(ll a, ll b){ if(b==0) return a; return gcd(b, a%b); } int main() { int n; cin>>n; vector<P> v1, v2; for(int i=0; i<n; i++){ ll a, b; cin>>a>>b; if(b>=a) v1.push_back({b, a}); else v2.push_back({a, b}); } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); int n1=v1.size(), n2=v2.size(); vector<ll> sc1(n1+1), sa1(n1+1), sa2(n2+1), mnb2(n2+1), mxb2(n2+1); mnb2[0]=1e9+7, mxb2[0]=0; for(int i=0; i<n2; i++) mnb2[i+1]=min(mnb2[i], v2[i].second), mxb2[i+1]=max(mxb2[i], v2[i].second); for(int i=0; i<n1; i++){ sc1[i+1]=sc1[i]+v1[i].first-v1[i].second; sa1[i+1]=sa1[i]+v1[i].second; } for(int i=0; i<n2; i++) sa2[i+1]=sa2[i]+v2[i].first; auto check=[&](int m){ for(int i=0; i<=min(m, n2); i++){ if(m-i>n1) continue; ll s=-sa2[i]-sa1[m-i]+sc1[n1]-sc1[m-i]; if(s>=0) return true; } return false; }; int l=0, r=n; while(r-l>1){ int mid=(l+r)/2; if(check(mid)) l=mid; else r=mid; } vector<ll> mxs(n2+2); mxs[0]=-1; using lll=__int128_t; P ans=P(0, 1); for(int i=0; i<=min(l, n2); i++){ ll s=-1; if(l-i<n1) s=-sa2[i]-sa1[l-i]+sc1[n1]-sc1[l-i]; mxs[i+1]=max(mxs[i], s); if(s<0) continue; if((lll)ans.first*v1[l-i].first<=(lll)s*ans.second) ans=P(s, v1[l-i].first); } for(int i=min(l, n2)+1; i<=n2; i++) mxs[i+1]=mxs[i]; for(int i=0; i<n2; i++){ ll s=mxs[i+1]+v2[i].second-v2[i].first; if(s<0) continue; if((lll)ans.first*v2[i].second<=(lll)s*ans.second) ans=P(s, v2[i].second); } for(int i=1; i<=n2 && i-1<=l; i++){ ll s=-1; bool neg=0; if(l-i+1<=n1){ if(-sa2[i]-sa1[l-i+1]+sc1[n1]-sc1[l-i+1]>=0) s=-sa2[i]-sa1[l-i+1]+sc1[n1]-sc1[l-i+1]+mnb2[i]; else neg=1, s=-sa2[i]-sa1[l-i+1]+sc1[n1]-sc1[l-i+1]+mxb2[i]; } if(s<0) continue; if(neg){ if((lll)ans.first*mxb2[i]<=(lll)s*ans.second) ans=P(s, mxb2[i]); }else{ if((lll)ans.first*mnb2[i]<=(lll)s*ans.second) ans=P(s, mnb2[i]); } } for(int i=0; i<=min(l, n2); i++){ ll s=-1; bool neg=0; if(l-i+1<=n1){ if(-sa2[i]-sa1[l-i+1]+sc1[n1]-sc1[l-i+1]>=0) s=-sa2[i]-sa1[l-i+1]+sc1[n1]-sc1[l-i+1]+v1[0].first; else neg=1, s=-sa2[i]-sa1[l-i+1]+sc1[n1]-sc1[l-i+1]+v1[l-i].first; } if(s<0) continue; if(neg){ if((lll)ans.first*v1[l-i].first<=(lll)s*ans.second) ans=P(s, v1[l-i].first); }else{ if((lll)ans.first*v1[0].first<=(lll)s*ans.second) ans=P(s, v1[0].first); } } ans=P(ans.first+l*ans.second, ans.second*n); ll g=gcd(ans.first, ans.second); cout<<ans.first/g<<" "<<ans.second/g<<endl; return 0; }
#include<cstdio> #include<cmath> #include<algorithm> #include<cstdlib> #include<cstring> #include<iostream> using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0' && ch<='9')x=x*10+ch-'0',ch=getchar(); return x*f; } int stack[20]; inline void write(int x) { if(x<0){putchar('-');x=-x;} if(!x){putchar('0');return;} int top=0; while(x)stack[++top]=x%10,x/=10; while(top)putchar(stack[top--]+'0'); } inline void pr1(int x){write(x),putchar(' ');} inline void pr2(int x){write(x),puts("");} struct node { int x,y; }a[100010]; bool cmp(node a,node b){return max(a.x,a.y)<max(b.x,b.y);} int n; long long sum,s[100010]; inline int erfen(int pos) { int l=1,r=n,p=0,d=max(a[pos].x,a[pos].y); while(l<=r) { int mid=l+r>>1; long long uf=s[mid];if(mid>=pos)uf-=d; if(uf<=sum)p=mid,l=mid+1; else r=mid-1; }return p; } inline long long gcd(long long x,long long y) { if(x==0)return y; else return gcd(y%x,x); } int main() { //freopen("a.in","r",stdin); //freopen("a.out","w",stdout); n=read(); for(int i=1;i<=n;i++)a[i].x=read(),a[i].y=read(); sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++) { s[i]=s[i-1]+max(a[i].x,a[i].y); if(a[i].y>a[i].x)sum+=a[i].y-a[i].x; } long long ax=0,ay=1,x,y,ul; for(int i=1;i<=n;i++) { x=erfen(i); long long uf=sum-s[x]; if(x>=i) x--,uf+=max(a[i].x,a[i].y); if(a[i].y>a[i].x) uf-=a[i].y-a[i].x; x=x*a[i].y+min(uf+a[i].y-a[i].x,1LL*a[i].y); y=1LL*n*a[i].y; if(x<=0) continue; ul=gcd(x,y); x/=ul,y/=ul; if(1.0*ay*x>1.0*y*ax) ax=x,ay=y;//爆ll } printf("%lld %lld\n",ax,ay); return 0; }
#include <iostream> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <random> #include <iomanip> #include <functional> #include <cassert> using namespace std; typedef long long ll; ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); } struct Frac { ll a, b; void normalize() { ll g = gcd(a, b); a /= g; b /= g; } Frac(ll _a, ll _b) { a = _a; b = _b; normalize(); } bool operator <(const Frac &other) const { return (__int128)a * other.b < (__int128)b * other.a; } }; 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), b(n); for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i]; } Frac ans(0, 1); { vector <int> p(n); iota(p.begin(), p.end(), 0); sort(p.begin(), p.end(), [&] (int i, int j) { return b[i] < b[j]; }); vector <int> na(n), nb(n); for (int i = 0; i < n; ++i) { na[i] = a[p[i]]; nb[i] = b[p[i]]; } a = na; b = nb; } vector <ll> can_suf(n); for (int i = n - 2; i >= 0; --i) { can_suf[i] = can_suf[i + 1] + max(0, b[i + 1] - a[i + 1]); } auto check = [&] (int m) { multiset <int> best; ll sum_a = 0; bool fl = false; for (int i = 0; i < n; ++i) { if (i > 0) { best.insert(a[i - 1]); sum_a += a[i - 1]; if ((int)best.size() == m + 1) { sum_a -= *best.rbegin(); best.erase(--best.end()); } } if (best.size() == m) { ll need = max(0LL, (sum_a + a[i]) - can_suf[i]); if (b[i] >= need) { fl = true; ll good = b[i] - need; Frac cur = Frac((ll)b[i] * m + good, (ll)b[i] * n); if (ans < cur) ans = cur; } } } return fl; }; int l = 0, r = n; while (r - l > 1) { int m = (l + r) >> 1; if (check(m)) l = m; else r = m; } check(l); cout << ans.a << ' ' << ans.b << '\n'; }
#include <bits/stdc++.h> // iostream is too mainstream #include <cstdio> // bitch please #include <iostream> #include <algorithm> #include <cstdlib> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <list> #include <cmath> #include <iomanip> #include <time.h> #define dibs reserve #define OVER9000 1234567890 #define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++) #define tisic 47 #define soclose 1e-8 #define chocolate win // so much chocolate #define patkan 9 #define ff first #define ss second #define abs(x) (((x) < 0)?-(x):(x)) #define uint unsigned int #define dbl long double #define pi 3.14159265358979323846 using namespace std; // mylittledoge using cat = long long; #ifdef DONLINE_JUDGE // palindromic tree is better than splay tree! #define lld I64d #endif cat gcd(cat x, cat y) { if(x > y) swap(x, y); return (x == 0) ? y : gcd(y%x, x); } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int N; cin >> N; vector< pair<int, int> > near, far; cat BA_dif = 0; for(int i = 0; i < N; i++) { int a, b; cin >> a >> b; if(a <= b) { near.push_back({b, a}); BA_dif += b-a; } else far.push_back({a, b}); } sort(begin(near), end(near)); vector<cat> B_sum_near(near.size()+1, 0); for(int i = 0; i < (int)near.size(); i++) B_sum_near[i+1] = B_sum_near[i] + near[i].ff; sort(begin(far), end(far)); vector<cat> A_sum_far(far.size()+1, 0); for(int i = 0; i < (int)far.size(); i++) A_sum_far[i+1] = A_sum_far[i] + far[i].ff; cat ans_num = 0, ans_denom = 1; for(int f = 0, n = 0; n < (int)near.size(); n++) { cat dif = BA_dif - B_sum_near[n]; if(dif < 0) continue; static cat cur_A_sum_far = 0; while(f < (int)far.size() && dif >= cur_A_sum_far + far[f].ff) { cur_A_sum_far += far[f].ff; f++; } while(f > 0 && dif < cur_A_sum_far) { f--; cur_A_sum_far -= far[f].ff; } dif -= cur_A_sum_far; if(dif < 0 || dif > near[n].ff) continue; cat P_num = 1LL * near[n].ff * (n + f) + dif; cat P_denom = near[n].ff; cat P = P_num / P_denom, ans = ans_num / ans_denom; if(P > ans) ans_num = P_num, ans_denom = P_denom; else if(P == ans && (P_num%P_denom) * ans_denom > (ans_num%ans_denom) * P_denom) ans_num = P_num, ans_denom = P_denom; } for(int f = 0; f < (int)far.size(); f++) { int n = upper_bound(begin(near), end(near), pair<int, int>(far[f].ss, -1)) - begin(near); cat dif = BA_dif - B_sum_near[n] + far[f].ss - far[f].ff; if(dif < 0) continue; int cnt_far = upper_bound(begin(A_sum_far), end(A_sum_far), dif) - 1 - begin(A_sum_far); cat cur_A_sum_far = A_sum_far[cnt_far]; if(f < cnt_far) { cur_A_sum_far -= far[f].ff; if(cnt_far < (int)far.size() && far[cnt_far].ff+cur_A_sum_far <= dif) { cur_A_sum_far += far[cnt_far].ff; cnt_far++; } } if(cnt_far > f) cnt_far--; dif -= cur_A_sum_far; if(dif < 0 || dif > far[f].ss) continue; cat P_num = 1LL * far[f].ss * (n + cnt_far) + dif; cat P_denom = far[f].ss; cat P = P_num / P_denom, ans = ans_num / ans_denom; if(P > ans) ans_num = P_num, ans_denom = P_denom; else if(P == ans && (P_num%P_denom) * ans_denom > (ans_num%ans_denom) * P_denom) ans_num = P_num, ans_denom = P_denom; } cat g = gcd(ans_num, ans_denom*N); cout << ans_num/g << " " << ans_denom*N/g << "\n"; return 0; } // look at my code // my code is amazing
#include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; const int maxn=1E5+5; ll n,sum,pre[maxn]; struct pt { ll x,y; bool operator<(const pt&A)const { return max(x,y)<max(A.x,A.y); } }a[maxn]; inline int get(int pos) { int l=1,r=n,p=0,d=max(a[pos].x,a[pos].y); while(l<=r) { int mid=l+r>>1; long long uf=pre[mid];if(mid>=pos)uf-=d; if(uf<=sum)p=mid,l=mid+1; else r=mid-1; }return p; /* int l=1,r=n,mid; int d=max(a[pos].x,a[pos].y); while(l<r) { mid=(l+r+1)>>1; ll s=pre[mid]-(mid>=pos?d:0); if(s<=sum) l=mid; else r=mid-1; } return l; */ } ll gcd(ll x,ll y) { if(y==0) return x; return x%y==0?y:gcd(y,x%y); } int main() { ios::sync_with_stdio(false); cin>>n; for(int i=1;i<=n;++i) { cin>>a[i].x>>a[i].y; sum+=max((ll)0,a[i].y-a[i].x); } sort(a+1,a+n+1); for(int i=1;i<=n;++i) pre[i]=pre[i-1]+max(a[i].x,a[i].y); ll ax=0,ay=1; for(int i=1;i<=n;++i) { int pos=get(i); ll s=sum-pre[pos]; if(pos>=i) --pos,s+=max(a[i].x,a[i].y); if(a[i].y>a[i].x) s-=a[i].y-a[i].x;//!!!!!!!!!!!!! ll x=(ll)pos*a[i].y+min(s+a[i].y-a[i].x,a[i].y); ll y=n*a[i].y; if(x<=0) continue; ll z=gcd(x,y); x/=z,y/=z; if((ld)ay*x>(ld)y*ax) ax=x,ay=y; } cout<<ax<<" "<<ay<<endl; return 0; }
#include <bits/stdc++.h> #define pb push_back #define pll pair <ll, ll> #define mp make_pair #define pyshnapyshnakaa ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0); #define x first #define y second #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") #define plll pair <pair <ll, ll>, ll> #define pllll pair <pair <ll, ll>, pair <ll, ll> > #define psl pair <string, ll> #define all(a) a.begin(), a.end() typedef long long ll; typedef long double ld; using namespace std; const ll maxn = 1e6; ll n, m, k, t; vector <pll> A; vector <ll> S; ll C[maxn]; ll gcd(ll a, ll b) { while (a != 0) { b = b % a; swap(a, b); } return b; } bool cmp(pll a, pll b) { return max(a.first, a.second) > max(b.first, b.second); } void minmin(pll &a, pll b) { if (a.first * 1.0 / a.second > b.first * 1.0 / b.second) { a = b; } // if (a.first * b.second > a.second * b.first) { // // cout << "CHECK_MOM" << endl; // // cout << a.first * b.second << " " << a.second * b.first << endl; // a = b; // } } pll getsum(pll s1, pll s2) { ll g = gcd(abs(s1.second), abs(s2.second)); ll lc = (s1.second / g) * s2.second; // ll lc = s1.second * s2.second / gcd(abs(s1.second), abs(s2.second)); return mp(s1.first * (lc / s1.second) + s2.first * (lc / s2.second), lc); } inline void ease(pll &p) { ll g = gcd(p.first, p.second); p.first /= g; p.second /= g; } int main() { pyshnapyshnakaa; ll q, w, e, a, b, c; cin >> n; ll sum = 0; for (q = 0; q < n; q++) { cin >> a >> b; sum += a; A.pb(mp(a, b)); } sort(all(A), cmp); // for (q = 0; q < A.size(); q++) { // cout << A[q].first << " " << A[q].second << endl; // } // cout << endl << endl; S.resize(n); S[0] = max(A[0].first, A[0].second); for (q = 1; q < n; q++) { S[q] = S[q - 1] + max(A[q].first, A[q].second); } pll ans = mp(1, 1); for (q = 0; q < n; q++) { // if (q != 4 && q != 6) { // continue; // } // cout << "Q " << q << endl; // ll d1 = max(A[q].second, A[q].first); ll d1 = A[q].second; ll i = lower_bound(all(S), sum - d1) - S.begin(); // cout << "I " << i << endl; if (i >= n) { continue; } ll cnt = i + 1; ll t = S[i]; ll d = max(A[q].first, A[q].second); // ll d = A[q].first; if (i >= q) { i = lower_bound(all(S), sum - d1 + d) - S.begin(); if (i >= n) { continue; } cnt = i; t = S[i] - d; } if (i >= n) { continue; } // cout << t << " " << cnt << " " << sum << " " << A[q].second << " T CNT sum second" << endl; // cout << "DEBUG " << t - sum + A[q].second << endl; pll zhopa = mp(sum - t, A[q].second); // zhopa.first *= -1; // zhopa = getsum(mp(1, 1), zhopa); // cout << zhopa.first << " " << zhopa.second << " ZHOPA1" << endl; zhopa.second *= n; ease(zhopa); // cout << zhopa.first << " " << zhopa.second << " ZHOPA" << endl; if (sum - t > A[q].second || sum - t < 0) { // cout << "POOOOOOHUUUUUUUUUI" << endl << endl << endl; continue; } pll dans = getsum(zhopa, mp(cnt, n)); ease(dans); // cout << "DANS " << dans.first << " " << dans.second << endl; minmin(ans, dans); // cout << endl << endl; } ans.first = ans.second - ans.first; // pll p1 = mp(697461712,2899550585); // minmin(ans, p1); ease(ans);/**/ cout << ans.first << " " << ans.second; return 0; }
#include<bits/stdc++.h> #define ld double #define ull unsigned long long #define ll long long #define pii pair<int, int> #define iiii pair<int, pii> #define mp make_pair #define INF 1000000000 #define MOD 1000000007 #define rep(i, x) for(int (i) = 0; (i) < (x); (i)++) inline int getint() { int x = 0, p = 1; char c = getchar(); while (c <= 32) c = getchar(); if (c == 45) p = -p, c = getchar(); while (c > 32) x = x * 10 + c - 48, c = getchar(); return x * p; } using namespace std; //ruogu_alter int n; ll s; vector<pii>v; // ll gcd(ll x, ll y) { if(!y) return x; return gcd(y, x % y); } inline void upd(pair<ll, ll> &u, ll x, ll y) { if(u.first * y > x * u.second) { u.first = x; u.second = y; } } int main() { //freopen(".in", "r", stdin); //freopen(".out", "w", stdout); n = getint(); rep(i, n) { int x = getint(), y = getint(); s += x; v.emplace_back(mp(max(x, y), y)); } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); int k = 0; ll sum = 0; rep(i, n) { sum += v[k++].first; if (sum >= s) break; } pair<ll, ll>px = mp(1, 1); rep(i, n) { int x = s - (sum - v[min(i, k - 1)].first); if(x <= v[i].second) upd(px, x, v[i].second); } px.first = px.second * (n - k + 1) - px.first; px.second *= n; ll g = gcd(px.first, px.second); px.first /= g; px.second /= g; cout << px.first << " " << px.second << endl; return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #define mp make_pair #define pii pair<int,int> #define fi first #define se second #define int long long using namespace std; typedef long long ll; const int MAXN = 100005; inline int readint() { int res = 0, f = 1; char c = 0; while(!isdigit(c)) { c = getchar(); if(c=='-') f = -1; } while(isdigit(c)) res = res*10+c-'0', c = getchar(); return res*f; } int n; pii s[MAXN],t[MAXN]; ll ansp = 1<<30, ansq = 1; inline void getMin(ll x, ll y) { if((__int128)x*ansq<(__int128)y*ansp) ansp = x, ansq = y; } ll pre[MAXN],sum; inline ll calc(int x, int mid) { int val = max(s[x].fi,s[x].se); return t[mid].fi<=val?pre[mid]-val:pre[mid]; } inline int binSearch(int x) { int left = 1, right = n, res = -1, mid; while(left<=right) { mid = (left+right)>>1; if(calc(x,mid)>=sum-s[x].se) res = mid, right = mid-1; else left = mid+1; } return res; } inline ll gcd(ll a, ll b) { return b?gcd(b,a%b):a; } signed main() { n = readint(); for(int i = 1; i<=n; i++) { s[i].fi = readint(), s[i].se = readint(); t[i].fi = max(s[i].fi,s[i].se), t[i].se = i; sum += s[i].fi; } sort(t+1,t+n+1), reverse(t+1,t+n+1); for(int i = 1; i<=n; i++) pre[i] = pre[i-1]+t[i].fi; for(int i = 1; i<=n; i++) { int res = binSearch(i); if(res<0) continue; ll val = sum-calc(i,res); if(t[res].fi<=max(s[i].fi,s[i].se)) res--; if(val<0) continue; getMin(1ll*res*s[i].se+val,s[i].se); } ansq *= n, ansp = ansq-ansp; if(ansp<=0) { cout << 0 << " " << 1 << endl; return 0; } ll g = gcd(ansp,ansq); cout << ansp/g << " " << ansq/g << endl; return 0; }
#include<cstdio> #include<iostream> #include<algorithm> #define RI register int #define CI const int& #define int long long using namespace std; const int N=100005; struct frac { int x,y; inline frac(CI X=0,CI Y=0) { x=X; y=Y; } friend inline bool operator < (const frac& A,const frac& B) { return 1.0*A.x*B.y<1.0*A.y*B.x; } friend inline bool operator > (const frac& A,const frac& B) { return 1.0*A.x*B.y>1.0*A.y*B.x; } }ans(0,1); struct data { int a,b,val; friend inline bool operator < (const data& A,const data& B) { return A.val<B.val; } }p[N]; int pfx[N],n,sum; inline int gcd(CI x,CI y) { return y?gcd(y,x%y):x; } signed main() { //freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout); RI i; for (scanf("%lld",&n),i=1;i<=n;++i) scanf("%lld%lld",&p[i].a,&p[i].b), p[i].val=max(p[i].a,p[i].b),sum+=max(p[i].b-p[i].a,0LL); for (sort(p+1,p+n+1),i=1;i<=n;++i) pfx[i]=pfx[i-1]+p[i].val; #define calc(x) (pfx[x]-((x)>=i?p[i].val:0)) for (i=1;i<=n;++i) { int cur=sum; if (p[i].a>p[i].b) cur+=p[i].b-p[i].a; int l=1,r=n,mid; while (l<=r) if (calc(mid=l+r>>1)<=cur) l=mid+1; else r=mid-1; //printf("%lld %lld\n",p[i].a,p[i].b); frac ret(cur-calc(l-1),p[i].b); ret=min(ret,frac(1,1)); ret.x+=ret.y*(l-1-(l>i)); ans=max(ans,ret); } #undef calc int g=gcd(ans.x,ans.y*=n); return printf("%lld %lld",ans.x/g,ans.y/g),0; }
#include<vector> #include<queue> #include<iostream> #include<fstream> #include<string> #include<cassert> #include<algorithm> #include<random> #include<map> #include<set> #include <bitset> using namespace std; //int mod = 998244353; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, int> pli; typedef pair<ll, ll> pll; typedef long double ld; typedef vector<int>vi; typedef vector<ll>vl; typedef vector<char>vc; typedef vector<string>vs; typedef vector<vector<int>>vii; typedef vector<vector<char>>vvc; typedef vector<vector<ll>>vll; typedef vector< pair<ll, ll>>vpll; typedef vector< pair<ld, ld>>vpld; typedef vector< pair<int, int>>vpi; typedef pair<ld, ld>pld; #define mp make_pair #define pb push_back int mod = 1000000007; int sum(int a, int b) { int c = a + b; if (c >= mod) { c -= mod; } return c; } int dif(int a, int b) { int c = a - b; if (c < 0) { c += mod; } return c; } int mlt(int a, int b) { ll c = a * 1LL * b; return c % mod; } int ibit(int n, int i) { return ((n >> i) & 1); } void outp(vii &ou) { for (int i = 0; i < ou.size(); i++) { for (int j = 0; j < ou[i].size(); j++) { cout << ou[i][j] << ' '; } cout << '\n'; } } int bp(int x, int y) { if (y == 0) { return 1; } int a = 0; if (!(y % 2)) { a = bp(x, y / 2); } return (y % 2) ? mlt(bp(x, y - 1), x) : mlt(a, a); } int obr(int x) { return bp(x, mod - 2); } const int maxn = 10000007; int fact[1000007], ofact[1000007]; void prec() { fact[0] = 1; ofact[0] = 1; for (int i = 1; i < maxn; i++) { fact[i] = mlt(fact[i - 1], i); } //cerr << "sdsds" << endl; ofact[maxn - 1] = obr(fact[maxn - 1]); for (int i = maxn - 2; i > 0; i--) { ofact[i] = mlt(ofact[i + 1], i + 1); } } int c(int a, int b) { return ((a <= b) && (a >= 0)) ? mlt(fact[b], mlt(ofact[a], ofact[b - a])) : 0; } ll gcd(ll x, ll y) { //cerr << x << y << endl; if (x == 0)return y; return(x > y) ? gcd(y, x) : gcd(y%x, x); } void upd(ll x, ll y, ll &curx, ll &cury) { ld x1 = (ld)x / (ld)y; ld cur1 = (ld)curx / ld(cury); if (x1>cur1 + 1e-17) { curx = x / gcd(x, y); cury = y / gcd(x, y); } } ll good(vl &prefs, vi &a, vi&b, int excl, int len, ll summ, vi&ord, vpi &h) { if (len < 0)return 0LL; if (len >= ord.size()) return -1LL; //cerr << summ << endl; ll sum1 = summ - (ll)max(0, b[excl] - a[excl]), psum = prefs[len]; if (ord[excl] < len)psum = psum - max(a[excl], b[excl]) + h[len].first; sum1 += b[excl] - a[excl]; // cerr << excl << ' ' << len << ' ' << summ << ' ' << sum1<<' '<< psum << endl; return max(sum1 - psum, -1LL); } void solve(istream &cin = std::cin, ostream &cout = std::cout) { int n; cin >> n; vpi h(n); vi a(n), b(n), ord(n), aord(n); vl prefs(n + 1); ll summ = 0; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; h[i] = mp(max(a[i], b[i]), i); summ += max(b[i] - a[i], 0); } sort(h.begin(), h.end()); for (int i = 0; i < n; i++) { prefs[i + 1] = prefs[i] + h[i].first; ord[h[i].second] = i; aord[i] = h[i].second; } //cerr <<summ<<' '<< aord[0] << ' '<<b[0]<< ' '<<prefs[0]<<' '<<prefs[1]<<' '<<prefs[2]<<' '<<prefs[3]<<endl; ll curx = 0, cury = 1; for (int k = 0; k < n; k++) { int lb = -1, rb = n - 1; for (int i = 0; i < 50; i++) { int mid = (rb + lb + i % 2) / 2; ll val = good(prefs, a, b, k, mid, summ, ord, h); if (val < 0)rb = mid - 1; else { lb = mid; if (mid >= 0) { // assert(val <= (ll)b[aord[mid]]); ll num = mid * 1LL * b[k] + min(val, (ll)b[k]), den = b[k] * 1LL * n; upd(num, den, curx, cury); // cerr << "dsdsd" <<k<<' '<<val<<' '<< num << ' ' << den << ' ' << mid << ' ' << val << ' ' << b[aord[mid]] << endl; } } } // cerr <<"?! "<< k << ' ' << curx << ' ' << cury << endl; } cout << curx << ' ' << cury << endl; } int main() { //prec(); //cerr << "Here" << endl; solve(); int n; cin >> n; }
#include<bits/stdc++.h> using namespace std; const int maxn=100100; struct node{ int A,B; }a[maxn]; struct val{ long long x,y; void operator = (const int &B){ x=B,y=1; } }; void print(val A){ printf("val=%lld/%lld\n",A.x,A.y); } val operator + (const val &A,const int B){ val ret=A; ret.x=ret.x+B*A.y; return ret; } bool operator < (const val &A,const val &B){ long long v1=A.x/A.y,v2=B.x/B.y; if(v1<v2) return 1; else if(v1>v2) return 0; return (A.x%A.y)*B.y<(B.x%B.y)*A.y; } bool operator < (const node &A,const node &B){ return A.A<B.A; } bool cmp(const node &A,const node &B){ return A.B<B.B; } priority_queue<node> q; void chkmax(val &A,val B){ A=A<B?B:A; } long long gcd(long long x,long long y){ long long r; while(y){ r=x%y; x=y; y=r; } return x; } bool ccp(const node &A,const node &B){ return A.B<B.B; } int main(){ // freopen("D.in","r",stdin); // freopen("D.out","w",stdout); int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%d",&a[i].A,&a[i].B); sort(a+1,a+n+1,cmp); long long tot=0,sum=0; int cnt=0; val ans,st; ans=0,st=0; for(int i=1;i<=n;i++) if(a[i].A<a[i].B) tot+=a[i].B-a[i].A; for(int i=1;i<=n;i++){ while(sum>tot){ sum-=(q.top()).A; cnt--; q.pop(); } if(a[i].A<a[i].B){ if(tot-sum>=a[i].B) st=cnt+1; else{ st=(val){tot-sum,a[i].B}; st=st+cnt; } chkmax(ans,st); tot-=a[i].B-a[i].A; while(sum>tot){ sum-=(q.top()).A; cnt--; q.pop(); } } if(a[i].A<=(tot-sum)) st=cnt+1; else{ st=(val){a[i].A-(tot-sum),a[i].B}; if(st.x>st.y) st.x=0; else st.x=st.y-st.x; st=st+cnt; } chkmax(ans,st); q.push(a[i]); sum+=a[i].A; cnt++; } if(ans.x==0){ printf("0 1\n"); return 0; } // cerr<<3211685239.0/(5528829200/20)<<endl; ans.y*=n; long long r=gcd(ans.x,ans.y); ans.x/=r,ans.y/=r; printf("%lld %lld\n",ans.x,ans.y); return 0; }
#include<bits/stdc++.h> #define a first #define b second #define pb push_back #define SZ(x) ((int)x.size()) #define L(i,u) for (register int i=head[u]; i; i=nxt[i]) #define rep(i,a,b) for (register int i=(a); i<=(b); i++) #define per(i,a,b) for (register int i=(a); i>=(b); i--) using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int,int> Pii; typedef vector<int> Vi; template<class T> inline void read(T &x){ x=0; char c=getchar(); int f=1; while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();} while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f; } template<class T> inline void umin(T &x, T y){x=x<y?x:y;} template<class T> inline void umax(T &x, T y){x=x>y?x:y;} inline ui R() { static ui seed=416; return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13; } const int N = 266666; int n,v[N];Pii s[N]; struct Frac{ ll a,b;// a/b Frac(ll A=0,ll B=1){a=A;b=B;} }ans; ll gcd(ll a, ll b){return !b?a:gcd(b,a%b);} inline Frac Norm(Frac y){ ll g=gcd(y.a,y.b);y.a/=g;y.b/=g;return y; } inline void upd(Frac &x, Frac y){ y=Norm(y);if(1.0*x.a/x.b>1.0*y.a/y.b)x=y; } bool cmp(Pii x, Pii y){return x.a+max(x.b-x.a,0)<y.a+max(y.b-y.a,0);} bool ck(int k){ ll tot=0;rep(i,1,n)tot+=max(s[i].b-s[i].a,0); rep(i,1,k)tot-=v[i];return tot>=0; } int main() { read(n);rep(i,1,n)read(s[i].a),read(s[i].b); sort(s+1,s+n+1,cmp); rep(i,1,n)v[i]=s[i].a+max(s[i].b-s[i].a,0); int L=0,R=n; while(L<R){ int mid=(L+R+1)>>1; if(ck(mid))L=mid;else R=mid-1; } int k=L;assert(k<n); ll ori=0;rep(i,1,n)ori+=max(s[i].b-s[i].a,0); rep(i,1,k)ori-=v[i]; ans.a=1;ans.b=1; rep(i,1,n){ ll tot=ori; if(i<=k)tot=tot+s[i].a-v[k+1];else tot-=max(s[i].b-s[i].a,0); upd(ans,(Frac){s[i].a-tot,s[i].b}); // printf("%d %lld %lld\n",i,s[i].a-tot,s[i].b); } ll x=ans.a,y=ans.b; // printf("%d %lld %lld\n",k,x,y); ans=(Frac){y-x+y*k,n*y};ans=Norm(ans); printf("%lld %lld\n",ans.a,ans.b); return 0; }
#include <iostream> #include <vector> #include <tuple> #include <algorithm> using namespace std; long long N, A[1 << 18], B[1 << 18], Actual_B[1 << 18], S1[1 << 18], S2[1 << 18]; vector<tuple<long long, long long, long long>> E; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } pair<int, int> solve(double pos) { int res = (int)(1.0 * pos); double amari = pos - 1.0 * res; // 微妙なやつを全探索 double best = 1e18; int best_id = -1; for (int i = 0; i < N; i++) { long long A1 = S1[res]; if (i < res) A1 = S1[res + 1] - A[i]; long long A2 = S2[res + 1]; if (i >= res + 1) A2 = S2[res] - (A[i] - B[i]); double alpha = 1.0 * A1 + 1.0 * A2; double beta = amari * A[i] + (1.0 - amari) * (A[i] - Actual_B[i]); if (best > alpha + beta) { best = alpha + beta; best_id = i; if (best < 0) { best += 0; } } } if (best <= 0) return make_pair(best_id, res); return make_pair(-1, -1); } int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> A[i] >> B[i]; Actual_B[i] = B[i]; if (A[i] > B[i]) B[i] = A[i]; E.push_back(make_tuple(B[i], A[i], Actual_B[i])); } sort(E.begin(), E.end()); for (int i = 0; i < N; i++) { B[i] = get<0>(E[i]); A[i] = get<1>(E[i]); Actual_B[i] = get<2>(E[i]); } for (int i = 1; i <= N; i++) S1[i] = S1[i - 1] + A[i - 1]; for (int i = N - 1; i >= 0; i--) S2[i] = S2[i + 1] + (A[i] - B[i]); // 二分探索により境界を検出 double cl = 0, cr = N, cm; pair<int, int> latest = make_pair(-1, -1); for (int i = 0; i < 77; i++) { cm = (cl + cr) / 2; pair<int, int> I = solve(cm); if (I.first >= 0) { cl = cm; latest = I; } else { cr = cm; } } if (latest == make_pair(-1, -1)) { cout << "0 1" << endl; return 0; } // 順番が決まったら? long long V1 = 0, cnts = 0; for (int i = 0; i < N; i++) { if (i == latest.first) continue; if (cnts < latest.second) V1 += A[i]; else V1 += (A[i] - B[i]); cnts++; } V1 += (A[latest.first] - Actual_B[latest.first]); long long Katamuki = Actual_B[latest.first]; long long Ans1 = 1LL * latest.second * Katamuki - V1; long long Ans2 = N * Katamuki; long long GCD = gcd(Ans1, Ans2); cout << Ans1 / GCD << " " << Ans2 / GCD << endl; return 0; }
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define rnt re int #define re register #define LL inline ll #define I inline int #define V inline void #define B inline bool #define ll long long int #define isnum(ch) ('0'<=ch&&ch<='9') #define FOR(i,a,b) for(rnt i=a;i<=b;i++) #define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++) char _buf[100000],*_op(_buf),*_ed(_buf); I getint(){ rnt _s=0;re char _ch=gc; while(!isnum(_ch))_ch=gc; while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc; return _s; } const int N=1e5+1; int n,k; ll S,up=1,dw=1; struct node{ ll x,y; V input(){x=getint(),y=getint();} B operator<(const node&u)const{ return x>u.x; } }a[N]; LL gcd(ll x,ll y){return !y?x:gcd(y,x%y);} int main(){ n=getint(); FOR(i,1,n)a[i].input(),S+=a[i].x,a[i].x=max(a[i].x,a[i].y); sort(a+1,a+1+n);ll now=0,dis; while(now<S)now+=a[++k].x; FOR(i,1,n){ dis=S-now+a[min(i,k)].x; if(dis<=a[i].y&&dis*dw<a[i].y*up) up=dis,dw=a[i].y; } up=(dw-up)+dw*(n-k),dw*=n,now=gcd(up,dw); cout<<up/now<<' '<<dw/now; return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long #define rep(i,n) for(int i=0;i<(n);i++) #define reps(i,a,b) for(int i=(a);i<(b);i++) #define pb push_back #define eb emplace_back #define all(v) (v).begin(),(v).end() #define fi first #define se second using vint=vector<int>; using pint=pair<int,int>; using vpint=vector<pint>; template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;} template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;} template<class A,class B> ostream& operator<<(ostream& ost,const pair<A,B>&p){ ost<<"{"<<p.first<<","<<p.second<<"}"; return ost; } template<class T> ostream& operator<<(ostream& ost,const vector<T>&v){ ost<<"{"; for(int i=0;i<v.size();i++){ if(i)ost<<","; ost<<v[i]; } ost<<"}"; return ost; } struct Fraction{ int64_t gcd(int64_t a,int64_t b){ return b?gcd(b,a%b):a; } int64_t a,b; Fraction():a(0),b(1){} Fraction(int64_t a):a(a),b(1){} Fraction(int64_t a,int64_t b):a(a),b(b){ if(b<0)a*=-1,b*=-1; int64_t g=gcd((a>0?a:-a),b); a/=g; b/=g; } bool operator==(const Fraction& f)const{return a==f.a&&b==f.b;} bool operator!=(const Fraction& f)const{return a!=f.a||b!=f.b;} Fraction& operator+=(const Fraction& f){ int64_t c=a*f.b+b*f.a; int64_t d=b*f.b; int64_t g=gcd((c>0?c:-c),d); a=c/g; b=d/g; return *this; } Fraction& operator-=(const Fraction& f){ int64_t c=a*f.b-b*f.a; int64_t d=b*f.b; int64_t g=gcd((c>0?c:-c),d); a=c/g; b=d/g; return *this; } Fraction& operator*=(const Fraction& f){ int64_t c=a*f.a; int64_t d=b*f.b; int64_t g=gcd((c>0?c:-c),d); a=c/g; b=d/g; return *this; } Fraction& operator/=(const Fraction& f){ int64_t c=a*f.b; int64_t d=b*f.a; if(d<0)c*=-1,d*=-1; int64_t g=gcd((c>0?c:-c),d); a=c/g; b=d/g; return *this; } Fraction operator+(const Fraction& f)const{return Fraction(*this)+=f;} Fraction operator-(const Fraction& f)const{return Fraction(*this)-=f;} Fraction operator*(const Fraction& f)const{return Fraction(*this)*=f;} Fraction operator/(const Fraction& f)const{return Fraction(*this)/=f;} bool operator<(const Fraction& f)const{return (Fraction(*this)-f).a<0;} bool operator>(const Fraction& f)const{return (Fraction(*this)-f).a>0;} bool operator<=(const Fraction& f)const{return (Fraction(*this)-f).a<=0;} bool operator>=(const Fraction& f)const{return (Fraction(*this)-f).a>=0;} Fraction operator+()const{return Fraction(*this);} Fraction operator-()const{return Fraction()-Fraction(*this);} }; signed main(){ int N; scanf("%lld",&N); map<int,vint>bucket; int cap=0; rep(i,N){ int a,b; scanf("%lld%lld",&a,&b); if(a<=b){ cap+=b-a; bucket[b].pb(b); } else{ bucket[b].pb(a); } } Fraction ans; priority_queue<int>que; int sum=0; for(auto &uku:bucket){ int b=uku.first; vint &lis=uku.second; sort(all(lis)); for(auto u:lis){ if(sum+u<=cap){ sum+=u; que.push(u); continue; } if(u==b){ chmax(ans,Fraction(cap-sum,b)+que.size()); } else if(cap+(b-u)-sum>=0){ chmax(ans,Fraction(cap+b-u-sum,b)+que.size()); } que.push(u); sum+=u; sum-=que.top(); que.pop(); } } ans/=N; cout<<ans.a<<" "<<ans.b<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; int n,a[111111],b[111111],cnt,mx,nw; pair<int,int> arr[111111]; long long sum,fuck,x,y,d,pp; long long gcd(long long a,long long b) { if (!b) return a; return gcd(b,a%b); } struct frac { long long x,y; bool operator > (const frac &u) const { return ((double)x/(double)y>(double)u.x/(double)u.y); } }cur,ass; int main() { scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%d%d",&a[i],&b[i]); sum+=a[i]; } for (int i=1;i<=n;i++) { arr[i]=make_pair(max(a[i],b[i]),i); } sort(arr+1,arr+n+1); reverse(arr+1,arr+n+1); for (int i=1;i<=n;i++) { fuck+=arr[i].first; if (fuck>=sum) { cnt=i; break; } } x=n-cnt;y=n; d=gcd(x,y);x/=d;y/=d; cur.x=x;cur.y=y; for (int i=1;i<=n;i++) { nw=b[arr[i].second]; if (i<cnt) pp=max(0ll,sum-fuck+arr[i].first); else pp=max(0ll,sum-fuck+arr[cnt].first); if (pp>=nw) continue; x=1ll*nw*(n-cnt)+nw-pp;y=1ll*nw*n; d=gcd(x,y);x/=d;y/=d; ass.x=x;ass.y=y; if (ass>cur) cur=ass; } /* x=n-cnt;y=n; d=gcd(x,y);x/=d;y/=d; cur.x=x;cur.y=y; mx=-1e9; for (int i=cnt+1;i<=n;i++) mx=max(mx,b[arr[i].second]); for (int i=1;i<=cnt;i++) { nw=max(mx,b[arr[i].second]); pp=max(0ll,sum-fuck+arr[i].first); if (pp>=nw) continue; x=1ll*nw*(n-cnt)+nw-pp;y=1ll*nw*n; d=gcd(x,y);x/=d;y/=d; ass.x=x;ass.y=y; if (ass>cur) cur=ass; }*/ printf("%lld %lld\n",cur.x,cur.y); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; const int maxn=1E5+5; ll n,sum,pre[maxn]; struct pt { ll x,y; bool operator<(const pt&A)const { return max(x,y)<max(A.x,A.y); } }a[maxn]; inline int get(int pos) {/* int l=1,r=n,p=0,d=max(a[pos].x,a[pos].y); while(l<=r) { int mid=l+r>>1; long long uf=pre[mid];if(mid>=pos)uf-=d; if(uf<=sum)p=mid,l=mid+1; else r=mid-1; }return p; */ int l=0,r=n,mid; int d=max(a[pos].x,a[pos].y); while(l<r) { mid=(l+r+1)>>1; ll s=pre[mid]; if(mid>=pos) s-=d; if(s<=sum) l=mid; else r=mid-1; } return l; } ll gcd(ll x,ll y) { if(y==0) return x; return x%y==0?y:gcd(y,x%y); } int main() { ios::sync_with_stdio(false); cin>>n; for(int i=1;i<=n;++i) { cin>>a[i].x>>a[i].y; sum+=max((ll)0,a[i].y-a[i].x); } sort(a+1,a+n+1); for(int i=1;i<=n;++i) pre[i]=pre[i-1]+max(a[i].x,a[i].y); ll ax=0,ay=1; for(int i=1;i<=n;++i) { int pos=get(i); ll s=sum-pre[pos]; if(pos>=i) --pos,s+=max(a[i].x,a[i].y); if(a[i].y>a[i].x) s-=a[i].y-a[i].x;//!!!!!!!!!!!!! ll x=(ll)pos*a[i].y+min(s+a[i].y-a[i].x,a[i].y); ll y=n*a[i].y; if(x<=0) continue; ll z=gcd(x,y); x/=z,y/=z; if((ld)ay*x>(ld)y*ax) ax=x,ay=y; } cout<<ax<<" "<<ay<<endl; return 0; }
#include<iostream> #include<cstdio> #include<algorithm> #include<cassert> #include<cmath> #include<vector> #include<map> #include<set> #include<string> #include<queue> #include<stack> using namespace std; #define MOD 1000000007 #define MOD2 998244353 #define INF ((1<<30)-1) #define LINF (1LL<<60) #define EPS (1e-10) typedef long long Int; typedef pair<Int, Int> P; Int n, a, b, small_sum; vector<P> bars; P max_p(P a, P b){ if(double(a.first)/a.second > double(b.first) / b.second)return a; else return b; int a_integer = a.first / a.second; int b_integer = b.first / b.second; if(a_integer > b_integer)return a; if(a_integer < b_integer)return b; a.first %= a.second; b.first %= b.second; cout << a.first << " " << a.second << " " << b.first << " " << b.second << endl; if(a.first * b.second > a.second * b.first)return a; else return b; } P ans = P(0, 1); Int gcd(Int a, Int b){ if(a == 0)return b; return gcd(b % a, a); } P prime(P x){ a = x.first; b = x.second; Int g = gcd(a, b); return P(a/g,b/g); } void clean_up(multiset<Int> &small, multiset<Int> &big){ if(small.empty() || big.empty())return; while(true){ Int small_big = *small.rbegin(); Int big_small = *big.begin(); if(small_big <= big_small)break; small.erase(prev(small.end())); big.erase(big.begin()); small.insert(big_small); big.insert(small_big); small_sum += big_small - small_big; } } int main(){ cin >> n; Int difsum = 0; vector<Int> as; for(Int i = 0;i < n;i++){ cin >> a >> b; bars.push_back(P(b,a)); difsum += max(0ll, b-a); } sort(bars.begin(), bars.end()); Int whole = 0; multiset<Int> small, big; for(Int i = 0;i < n;i++){ Int b = bars[i].first, a = bars[i].second; if(b-a < 0)difsum += b-a; while(true){ if(big.empty())break; auto it = big.begin(); if(difsum < *it + small_sum)break; whole++; small_sum += *it;small.insert(*it); big.erase(it); } Int bottom = difsum - small_sum; Int top = b - bottom; if(bottom >= 0 && top >= 0){ ans = max_p(ans, P(whole*(bottom+top)+bottom,n*(bottom + top))); // cout << b << " " << a << " " << difsum << " " << small_sum << " " << whole<< endl; } difsum -= b-a; big.insert(a); clean_up(small, big); } ans = prime(ans); cout << ans.first << " " << ans.second << endl; return 0; }
#include<cstdio> #include<cmath> #include<algorithm> #include<cstdlib> #include<cstring> using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0' && ch<='9')x=x*10+ch-'0',ch=getchar(); return x*f; } int stack[20]; inline void write(int x) { if(x<0){putchar('-');x=-x;} if(!x){putchar('0');return;} int top=0; while(x)stack[++top]=x%10,x/=10; while(top)putchar(stack[top--]+'0'); } inline void pr1(int x){write(x),putchar(' ');} inline void pr2(int x){write(x),puts("");} struct node { int x,y; }a[100010]; bool cmp(node a,node b){return max(a.x,a.y)<max(b.x,b.y);} int n; long long sum,s[100010]; inline int erfen(int pos) { int l=1,r=n,p=0,d=max(a[pos].x,a[pos].y); while(l<=r) { int mid=l+r>>1; long long uf=s[mid];if(mid>=pos)uf-=d; if(uf<=sum)p=mid,l=mid+1; else r=mid-1; }return p; } inline long long gcd(long long x,long long y) { if(x==0)return y; else return gcd(y%x,x); } int main() { //freopen("a.in","r",stdin); //freopen("a.out","w",stdout); n=read(); for(int i=1;i<=n;i++)a[i].x=read(),a[i].y=read(); sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++) { s[i]=s[i-1]+max(a[i].x,a[i].y); if(a[i].y>a[i].x)sum+=a[i].y-a[i].x; } long long ax=0,ay=1,x,y,ul; for(int i=1;i<=n;i++) { x=erfen(i);long long uf=sum-s[x];if(x>=i)x--,uf+=max(a[i].x,a[i].y); if(a[i].y>a[i].x)uf-=a[i].y-a[i].x; x=x*a[i].y+min(uf+a[i].y-a[i].x,1LL*a[i].y),y=1LL*n*a[i].y;ul=gcd(x,y); if(x<=0)continue; x/=ul,y/=ul; long long he=gcd(ay,y); if(1.0*ay/he*x>1.0*y/he*ax)ax=x,ay=y; } printf("%lld %lld\n",ax,ay); return 0; }
#include<iostream> #include<string> #include<cstdio> #include<vector> #include<cmath> #include<algorithm> #include<functional> #include<iomanip> #include<queue> #include<ciso646> #include<utility> #include<set> #include<cassert> using namespace std; //#define int long long using ll = long long; using P = pair<int, int>; using LP = pair<ll, ll>; const ll INF = (1e+18) + 100; const ll mod = 1000000007; #define rep(i, n) for(int i=0;i<n;++i) #define rep1(i,n) for(int i=1;i<=n;i++) #define repl(i, l, n) for(int i=l;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 stop char nyaa;cin>>nyaa; typedef long double ld; const ld eps = 1e-8; ll gcd(ll a, ll b) { if (a < b)swap(a, b); while (b) { ll r = a % b; a = b; b = r; } return a; } bool isl(pair<ll, LP> a, pair<ll, LP> b) { if (a.first != b.first)return a.first < b.first; return a.second.first*b.second.second < a.second.second*b.second.first; } void solve() { int n; cin >> n; vector<ll> bs; ll sum = 0; vector<LP> w; rep(i, n) { int a, b; cin >> a >> b; if (a <= b) { sum += a; bs.push_back(b); } else { w.push_back({ a,b }); } } sort(bs.begin(), bs.end(), greater<ll>()); vector<ll> rb(bs.size() + 1); rep(i, bs.size()) { rb[i + 1] = rb[i] + bs[i]; } if (bs.size() == 0) { cout << "0 1" << endl; return; } sort(w.begin(), w.end()); if (w.size() == 0) { //assert(false); int id = upper_bound(rb.begin(), rb.end(), sum) - rb.begin(); id--; ll rest = sum - rb[id]; pair<ll, LP> memo = { bs.size() - id - 1,{bs[id]-rest,bs[id]} }; LP ans = { memo.second.first + memo.first*memo.second.second,memo.second.second }; ans.second *= n; ll g = gcd(ans.first, ans.second); ans.first /= g; ans.second /= g; cout << ans.first << " " << ans.second << endl; return; } else if (w.size() == 1) { ll suma = sum; int id = upper_bound(rb.begin(), rb.end(), suma) - rb.begin(); id--; ll rest = suma - rb[id]; pair<ll, LP> z1 = { bs.size() - id - 1,{ bs[id]-rest,bs[id] } }; //cout << z1.first << " " << z1.second.first<<" "<<z1.second.second << endl; suma = sum + w[0].first; pair<ll, LP> z2; if (suma >= rb[bs.size()] + w[0].second) { z2 = { 0,{ 0,1 } }; } else { id = lower_bound(rb.begin(), rb.end(), suma - (w[0].second - 1)) - rb.begin(); rest = suma - rb[id]; if (rest < 0) { id = upper_bound(rb.begin(), rb.end(), suma) - rb.begin(); id--; z2.first = 1 + bs.size() - id - 1; ll rest = suma - rb[id]; z2.second = { bs[id] - rest,bs[id] }; } else { z2.first = 1 + bs.size() - id-1; z2.second = { w[0].second - rest,w[0].second }; } } LP ans; if (isl(z1, z2)) { ans = { z2.second.first + z2.first*z2.second.second,z2.second.second }; } else { ans = { z1.second.first + z1.first*z1.second.second,z1.second.second }; } ans.second *= n; ll g = gcd(ans.first, ans.second); ans.first /= g; ans.second /= g; cout << ans.first << " " << ans.second << endl; return; } ll suma = sum; int id = upper_bound(rb.begin(), rb.end(), suma) - rb.begin(); id--; ll rest = suma - rb[id]; pair<ll, LP> memo = { bs.size() - id - 1,{ bs[id]-rest,bs[id] } }; vector<ll> rw(w.size() + 1); rep(i, w.size()) { rw[i + 1] = rw[i] + w[i].first; } rep(i, w.size()) { int le = -1, ri = w.size() - 1; while (ri - le > 1) { int m = (le + ri) / 2; ll suma = sum + rw[m] + w[i].first; if (i < m) { suma -= w[i].first; suma += w[m].first; } if (suma >= w[i].second + rb[bs.size()]) { ri = m-1; continue; } pair<ll, LP> c1, c2; id = lower_bound(rb.begin(), rb.end(), suma - (w[i].second - 1)) - rb.begin(); rest = suma - rb[id]; if (rest < 0) { id = upper_bound(rb.begin(), rb.end(), suma) - rb.begin(); id--; c1.first = m + 1 + bs.size() - id - 1; ll rest = suma - rb[id]; c1.second = { bs[id] - rest,bs[id] }; } else { c1.first = m + 1 + bs.size() - id - 1; c1.second = { w[i].second - rest,w[i].second }; } suma = sum + rw[m + 1] + w[i].first; if (i < m + 1) { suma -= w[i].first; suma += w[m + 1].first; } if (suma >= w[i].second + rb[bs.size()]) { ri = m; continue; } id = lower_bound(rb.begin(), rb.end(), suma - (w[i].second - 1)) - rb.begin(); rest = suma - rb[id]; if (rest < 0) { id = upper_bound(rb.begin(), rb.end(), suma) - rb.begin(); id--; c2.first = m + 1 + 1 + bs.size() - id - 1; ll rest = suma - rb[id]; c2.second = { bs[id] - rest,bs[id] }; } else { c2.first = m + 1 + 1 + bs.size() - id - 1; c2.second = { w[i].second - rest,w[i].second }; } if (isl(c1, c2)) { le = m; } else { ri = m; } } ll suma = sum + rw[ri] + w[i].first; if (i < ri) { suma -= w[i].first; suma += w[ri].first; } if (suma < w[i].second + rb[bs.size()]) { //cout << "! " << suma << endl; id = lower_bound(rb.begin(), rb.end(), suma - (w[i].second - 1)) - rb.begin(); pair<ll, LP> z; rest = suma - rb[id]; if (rest < 0) { id = upper_bound(rb.begin(), rb.end(), suma) - rb.begin(); id--; z.first = ri + 1 + bs.size() - id - 1; ll rest = suma - rb[id]; z.second = { bs[id] - rest,bs[id] }; } else { z.first = ri + 1 + bs.size() - id - 1; z.second = { w[i].second - rest,w[i].second }; } //cout << z.first << " " << z.second.first<<" "<<z.second.second << endl; if (isl(memo, z)) { memo = z; } } ri = 0; suma = sum + rw[ri] + w[i].first; if (i < ri) { suma -= w[i].first; suma += w[ri].first; } if (suma >= w[i].second + rb[bs.size()])continue; //cout << "! " << suma << endl; id = lower_bound(rb.begin(), rb.end(), suma - (w[i].second - 1)) - rb.begin(); pair<ll, LP> z; rest = suma - rb[id]; if (rest < 0) { id = upper_bound(rb.begin(), rb.end(), suma) - rb.begin(); id--; z.first = ri + 1 + bs.size() - id - 1; ll rest = suma - rb[id]; z.second = { bs[id] - rest,bs[id] }; } else { z.first = ri + 1 + bs.size() - id - 1; z.second = { w[i].second - rest,w[i].second }; } if (isl(memo, z)) { memo = z; } } LP ans = { memo.second.first + memo.first*memo.second.second,memo.second.second }; ans.second *= n; ll g = gcd(ans.first, ans.second); ans.first /= g; ans.second /= g; cout << ans.first << " " << ans.second << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); //cout << fixed << setprecision(10); solve(); //stop return 0; }
#include "bits/stdc++.h" using namespace std; using ll = long long; using vll = std::vector<ll>; using vvll = std::vector<vll>; using vvvll = std::vector<vvll>; using dd = double; using vdd = std::vector<dd>; using vvdd = std::vector<vdd>; using vvvdd = std::vector<vvdd>; constexpr ll INF = 1LL << 60; constexpr dd EPS = 1e-11; //cin,cout高速化のおまじない+桁数指定 struct Fast{ Fast(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(std::numeric_limits<double>::max_digits10); } } fast; #define REPS(i, S, E) for (ll i = (S); i <= (E); i++) #define REP(i, N) REPS(i, 0, N-1) #define DEPS(i, S, E) for (ll i = (E); i >= (S); i--) #define DEP(i, N) DEPS(i, 0, N-1) #define EACH(e, v) for (auto&& e : v) template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; }return false; } template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; }return false; } template<class T> inline T MaxE(vector<T>&v,ll S,ll E){T m=v[S]; REPS(i,S,E)chmax(m,v[i]); return m;}//v[S]~v[E]の最大値 template<class T> inline T MinE(vector<T>&v,ll S,ll E){T m=v[S]; REPS(i,S,E)chmin(m,v[i]); return m;}//v[S]~v[E]の最小値 template<class T> inline T MaxE(vector<T> &v, ll N) { return MaxE(v, 0, N - 1); } //先頭N個中の最大値 template<class T> inline T MinE(vector<T> &v, ll N) { return MinE(v, 0, N - 1); } template<class T> inline T MaxE(vector<T> &v) { return MaxE(v, (ll)v.size()); } template<class T> inline T MinE(vector<T> &v) { return MinE(v, (ll)v.size()); } template<class T> inline ll MaxI(vector<T>&v,ll S,ll E){ll m=S; REPS(i,S,E){if(v[i]>v[m])m=i;} return m;} template<class T> inline ll MinI(vector<T>&v,ll S,ll E){ll m=S; REPS(i,S,E){if(v[i]<v[m])m=i;} return m;} template<class T> inline ll MaxI(vector<T> &v, ll N) { return MaxI(v, 0, N - 1); } template<class T> inline ll MinI(vector<T> &v, ll N) { return MinI(v, 0, N - 1); } template<class T> inline ll MaxI(vector<T> &v) { return MaxI(v, (ll)v.size()); } template<class T> inline ll MinI(vector<T> &v) { return MinI(v, (ll)v.size()); } template<class T> inline T Sum(vector<T> &v, ll S, ll E){ T s=v[S]; REPS(i, S+1, E)s+=v[i]; return s; } template<class T> inline T Sum(vector<T> &v, ll N) { return Sum(v, 0, N - 1); } template<class T> inline T Sum(vector<T> &v) { return Sum(v, v.size()); } template<class T> inline T POW(T a, ll n){ T r=1; for (; n>0; n>>=1, a*=a){ if (n&1)r*=a; } return r; } inline ll POW(int a, ll n){ return POW((ll)a, n); } inline ll MSB(ll a){for(ll o=63,x=-1;;){ll m=(o+x)/2; if(a<(1LL<<m))o=m; else x=m; if(o-x==1)return x;}} inline ll CEIL(ll a, ll b){ return (a+b-1)/b; } template<class T=ll> inline vector<T> cinv(ll N){ vector<T> v(N); REP(i, N)cin>>v[i]; return move(v);} template<class T=ll, class S=ll> inline vector<pair<T, S>> cinv2(ll N){ vector<pair<T, S>> v(N); REP(i,N){cin>>v[i].first>>v[i].second;} return move(v); } template<class T=ll,class S=ll,class R=ll> inline vector<tuple<T, S, R>> cinv3(ll N){ vector<tuple<T,S,R>> v(N); REP(i,N){cin>>get<0>(v[i])>>get<1>(v[i])>>get<2>(v[i]);} return move(v);} template<class T=ll,class S=ll,class R=ll,class Q=ll> inline vector<tuple<T,S,R,Q>> cinv4(ll N){ vector<tuple<T,S,R,Q>> v(N); REP(i,N){cin>>get<0>(v[i])>>get<1>(v[i])>>get<2>(v[i])>>get<3>(v[i]);} return move(v);} template<class T> inline void coutv(vector<T> &v, char deli=' '){ ll N=(ll)v.size(); REP(i,N){cout << v[i] << ((i==N-1)?'\n':deli);} } template<class T> void bye(T a){cout << a << '\n'; exit(0);} struct mll{ static ll MOD; ll val; mll(ll v = 0): val(v % MOD){ if (val < 0) val += MOD; } mll operator - () const { return -val; } mll operator + (const mll &b) const { return val + b.val; } mll operator - (const mll &b) const { return val - b.val; } mll operator * (const mll &b) const { return val * b.val; } mll operator / (const mll &b) const { return mll(*this) /= b; } mll operator + (ll b) const { return *this + mll(b); } mll operator - (ll b) const { return *this - mll(b); } mll operator * (ll b) const { return *this * mll(b); } friend mll operator + (ll a, const mll &b) { return b + a; } friend mll operator - (ll a, const mll &b) { return -b + a; } friend mll operator * (ll a, const mll &b) { return b * a; } mll &operator += (const mll &b) { val=(val+b.val)%MOD; return *this; } mll &operator -= (const mll &b) { val=(val+MOD-b.val)%MOD; return *this; } mll &operator *= (const mll &b) { val=(val*b.val)%MOD; return *this; } mll &operator /= (const mll &b) { ll c=b.val, d=MOD, u=1, v=0; while (d){ ll t = c / d; c -= t * d; swap(c, d); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } mll &operator += (ll b) { return *this += mll(b); } mll &operator -= (ll b) { return *this -= mll(b); } mll &operator *= (ll b) { return *this *= mll(b); } mll &operator /= (ll b) { return *this /= mll(b); } bool operator == (const mll &b) { return val == b.val; } bool operator != (const mll &b) { return val != b.val; } bool operator == (ll b) { return *this == mll(b); } bool operator != (ll b) { return *this != mll(b); } friend bool operator == (ll a, const mll &b) { return mll(a) == b.val; } friend bool operator != (ll a, const mll &b) { return mll(a) != b.val; } friend ostream &operator << (ostream &os, const mll &a) { return os << a.val; } friend istream &operator >> (istream &is, mll &a) { return is >> a.val; } static mll Combination(ll a, ll b){ chmin(b, a-b); if (b<0) return mll(0); mll c = 1; REP(i, b) c *= a-i; REP(i, b) c /= i+1; return c; } }; ll mll::MOD = (ll)(1e9 + 7);// 998244353LL;//(ll)(1e9 + 7); using vmll = std::vector<mll>; using vvmll = std::vector<vmll>; using vvvmll = std::vector<vvmll>; using vvvvmll = std::vector<vvvmll>; ll Gcd(ll num1, ll num2){ ll numA, numB; if (num1 > num2) { numA = num2; numB = num1; } else { numA = num1; numB = num2; } if (numA == 0) return numB; while (1) { ll numC = numB % numA; if (numC == 0) return numA; numB = numA; numA = numC; } } void output(ll p, ll q){ ll P, Q; if (p == 0){ P = 0; Q = 1; } else{ ll gcd = Gcd(p, q); P = p / gcd; Q = q / gcd; } cout << P << " " << Q << '\n'; } void solve() { ll N; cin >> N; auto AB = cinv2(N); vll C(N), v(N); REP(i, N){ ll A = AB[i].first; ll B = AB[i].second; C[i] = (B-A > 0) ? (B-A): 0; v[i] = std::max(A, B); } ll allC = Sum(C); if (allC == 0) { output(0, 1); return; } vector<tuple<ll, ll, ll>> vAB; REP(i, N) vAB.emplace_back(v[i], AB[i].first, AB[i].second); std::sort(vAB.begin(), vAB.end()); ll n = 0; ll sumv = 0, sumvv; REP(i, N){ ll v = std::get<0>(vAB[i]); ll A = std::get<1>(vAB[i]); ll B = std::get<2>(vAB[i]); if (sumv + v > allC){ n = i; sumvv = sumv+v; break; } sumv += v; } if (sumv == allC) { output(n, N); return; } ll diffV = allC - sumv; double maxW = 0.0; ll maxI; ll maxp, maxq; REPS(i, n, N-1){ ll v = std::get<0>(vAB[i]); ll A = std::get<1>(vAB[i]); ll B = std::get<2>(vAB[i]); ll p = (A<=B) ? diffV : diffV-A+B; ll q = B; double w = p / (double)q; if (maxW < w){ maxW = w; maxI = i; maxp = p; maxq = q; } } REPS(i, 0, n-1){ ll v = std::get<0>(vAB[i]); ll A = std::get<1>(vAB[i]); ll B = std::get<2>(vAB[i]); ll sumv2 = sumvv - v; ll diffV2 = allC - sumv2; if (diffV2 <= 0) continue; ll p = (A<=B) ? diffV2 : diffV2-A+B; ll q = B; double w = p / (double)q; if (maxW < w){ maxW = w; maxI = i; maxp = p; maxq = q; } } output(n*maxq+maxp, N*maxq); } int main(){ solve(); return 0; }
/* * @Author: RBQRBQ * @Date: 2020-04-06 20:28:58 * @LastEditors: RBQRBQ * @LastEditTime: 2020-04-08 19:16:55 */ #include<bits/stdc++.h> using namespace std; typedef long long ll; #define int ll typedef vector<int> VI; typedef pair<int,int> pii; namespace IO{ template<typename T>inline void read(T &x){ x=0;ll f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();} while(isdigit(ch)){x=x*10+ch-48;ch=getchar();} x=x*f; } } using namespace IO; typedef pair<ll,ll> frac; ll N,S=0; vector<frac> T; bool mp(frac A,frac B) { return (A.first*B.second<A.second*B.first); } ll gcd(ll x,ll y) { return (!y)?x:gcd(y,x%y); } signed main() { read(N); for(int i=1;i<=N;i++) { ll x,y; read(x),read(y); S+=x; T.push_back(make_pair(std::max(x,y),y)); } sort(T.begin(),T.end(),greater<frac>()); ll pos=0; ll point=-1; int ca=0; for(auto it=T.begin();it!=T.end();it++) { if((pos+it->first) >S) break; pos+=it->first; point=it-T.begin(); ca++; } if(point==N-1){cout<<"0 1"<<endl;return 0;} frac ans(0,1); for(int i=0;i<T.size();++i) { if(i+1>ca) { if(pos+T[i].second>=S) { frac res(pos+T[i].second-S,T[i].second); ans=(mp(ans,res))?res:ans; } } else { if(pos-T[i].first+T[point+1].first+T[i].second>=S) { frac res(pos-T[i].first+T[point+1].first+T[i].second-S,T[i].second); ans=(mp(ans,res))?res:ans; } } } ans.first+=ans.second*(N-ca-1);ans.second*=N; ll _f=gcd(ans.first,ans.second); cout<<ans.first/_f<<" "<<ans.second/_f<<endl; return 0; }
// #pragma GCC target("avx2") // CPU 処理並列化 // #pragma GCC optimize("O3") // CPU 処理並列化 // #pragma GCC optimize("unroll-loops") // 条件処理の呼び出しを減らす #include<stdio.h> #include<math.h> #include<algorithm> #include<queue> #include<deque> #include<stack> #include<string> #include<string.h> #include<vector> #include<set> #include<map> #include<bitset> #include<stdlib.h> #include<cassert> #include<time.h> #include<bitset> #include<numeric> #include<unordered_set> #include<complex> using namespace std; const long long mod=1000000007; const long long inf=mod*mod; const long long d2=(mod+1)/2; const double EPS=1e-10; const double INF=1e+10; const double PI=acos(-1.0); const int C_SIZE = 5121000; namespace{ long long fact[C_SIZE]; long long finv[C_SIZE]; long long inv[C_SIZE]; long long Comb(int a,int b){ if(a<b||b<0)return 0; return fact[a]*finv[b]%mod*finv[a-b]%mod; } void init_C(int n){ fact[0]=finv[0]=inv[1]=1; for(int i=2;i<n;i++){ inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod; } for(int i=1;i<n;i++){ fact[i]=fact[i-1]*i%mod; finv[i]=finv[i-1]*inv[i]%mod; } } long long pw(long long a,long long b){ if(a<0LL)return 0; if(b<0LL)return 0; long long ret=1; while(b){ if(b%2)ret=ret*a%mod; a=a*a%mod; b/=2; } return ret; } long long pw_mod(long long a,long long b,long long M){ if(a<0LL)return 0; if(b<0LL)return 0; long long ret=1; while(b){ if(b%2)ret=ret*a%M; a=a*a%M; b/=2; } return ret; } int pw_mod_int(int a,int b,int M){ if(a<0)return 0; if(b<0)return 0; int ret=1; while(b){ if(b%2)ret=(long long)ret*a%M; a=(long long)a*a%M; b/=2; } return ret; } int ABS(int a){return max(a,-a);} long long ABS(long long a){return max(a,-a);} double ABS(double a){return max(a,-a);} int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; } } // ここから編集しろ long long p[110000]; long long q[110000]; long long gcd(long long a,long long b){ while(a){ b%=a;swap(a,b); }return b; } struct wolf{ long long bb,bs; }; inline bool operator<(const wolf&a,const wolf&b){ return (__int128)a.bs*b.bb<(__int128)a.bb*b.bs; } pair<long long,int>P[110000]; long long bit[110000]; long long sum(int a,int b){ if(a)return sum(0,b)-sum(0,a-1); long long ret=0; for(;b>=0;b=(b&(b+1))-1)ret+=bit[b]; return ret; } void add(int a,long long b){ for(;a<110000;a|=a+1)bit[a]+=b; } int main(){ int a;scanf("%d",&a); long long rem=0; long long cnt=0; long long A=0; for(int i=0;i<a;i++){ scanf("%lld%lld",p+i,q+i); P[i]=make_pair(max(p[i],q[i]),i); A+=p[i]; } std::sort(P,P+a); wolf ret; ret.bs=0; ret.bb=1; for(int i=0;i<a;i++){ add(i,P[i].first); } for(int i=0;i<a;i++){ add(i,-P[i].first); int left=-1; int right=a; long long S=A-q[P[i].second]; while(left+1<right){ int M=(left+right)/2; long long tmp=sum(M,a); if(tmp>=S){ left=M; }else right=M; } long long am=A-sum(left,a); if(am>q[P[i].second])continue; if(am<0)am=0; long long cnt=left; if(i>=left)cnt++; wolf tmp; // printf("%d: %lld %d %lld\n",i,am,right,cnt); tmp.bs=cnt*q[P[i].second]-am; tmp.bb=q[P[i].second]*a; if(ret<tmp)ret=tmp; add(i,P[i].first); } long long g=gcd(ret.bs,ret.bb); ret.bs/=g; ret.bb/=g; printf("%lld %lld\n",ret.bs,ret.bb); }
#include<bits/stdc++.h> using namespace std; const int N=100010; struct node{ int a,b; bool operator<(const node q)const{ return a>q.a; } }s[N]; int n; long long sum=0; long long gcd(long long x,long long y){ if(y==0) return x; return gcd(y,x%y); } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ int x,y; scanf("%d %d",&x,&y); sum+=x; s[i].a=max(x,y);s[i].b=y; } sort(s+1,s+1+n); long long as=0; int k=0; while(1){ k++; as+=s[k].a; if(as>=sum) break; } long long ansx=1,ansy=1; for(int i=1;i<=n;i++){ long long now=sum-(as-s[min(i,k)].a); if(now<=s[i].b && now*ansy<1ll*s[i].b*ansx) ansx=now,ansy=s[i].b; } ansx=ansy-ansx+(n-k)*ansy; ansy*=n; printf("%lld %lld",ansx/gcd(ansx,ansy),ansy/gcd(ansx,ansy)); }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author Egor Kulikov */ #include <iostream> #include <fstream> #include <bits/stdc++.h> // // Created by kulikov on 10/17/2019. // #ifndef JHELPER_EXAMPLE_PROJECT_INPUT_H #define JHELPER_EXAMPLE_PROJECT_INPUT_H // // Created by egor on 30.10.2019. // #ifndef JHELPER_EXAMPLE_PROJECT_GENERAL_H #define JHELPER_EXAMPLE_PROJECT_GENERAL_H using namespace std; typedef long long longint; typedef vector<int> vi; typedef pair<int, int> pii; const int MAX_INT = 2147483647; const double PI = atan(1) * 4; const int DX_KNIGHT[] = {2, 1, -1, -2, -2, -1, 1, 2}; const int DY_KNIGHT[] = {1, 2, 2, 1, -1, -2, -2, -1}; const int DX4[] = {1, 0, -1, 0}; const int DY4[] = {0, 1, 0, -1}; bool isValidCell(int x, int y, int rows, int cols) { return x >= 0 && y >= 0 && x < rows && y < cols; } void decreaseByOne() { } template <typename T, class...Vargs> void decreaseByOne(vector<T>& arr, Vargs...arrs) { for (int& i : arr) { i--; } decreaseByOne(arrs...); } #endif //JHELPER_EXAMPLE_PROJECT_GENERAL_H class Input { public: enum ErrorType { OK, END_OF_FILE, UNEXPECTED_SYMBOL, INVALID_CALL }; private: istream& in; bool exhausted = false; ErrorType error = OK; int get(); template<typename T> T readInteger(); int skipWhitespace(); public: Input(istream& in); int readInt(); longint readLong(); string readString(); vector<int> readIntArray(int size); template<typename T> T readType(); template<typename T, typename U> pair<T, U> readType(); template<typename T> vector<T> readArray(int size); template<typename T1, typename T2> tuple<vector<T1>, vector<T2> > readArrays(int size); template<typename T1, typename T2, typename T3> tuple<vector<T1>, vector<T2>, vector<T3> > readArrays(int size); template<typename T1, typename T2, typename T3, typename T4> tuple<vector<T1>, vector<T2>, vector<T3>, vector<T4> > readArrays(int size); template<typename T1, typename T2, typename T3, typename T4, typename T5> tuple<vector<T1>, vector<T2>, vector<T3>, vector<T4>, vector<T5> > readArrays(int size); template<typename T> vector<vector<T> > readTable(int rows, int cols); string readLine(); }; inline bool isWhitespace(int c) { return isspace(c) || c == EOF; } int Input::skipWhitespace() { int c; do { c = get(); if (exhausted) { error = END_OF_FILE; return c; } } while (isWhitespace(c)); return c; } Input::Input(std::istream &in) : in(in) { } inline int Input::get() { int result = in.get(); if (result == EOF) { exhausted = true; } return result; } template<typename T> T Input::readType() { error = INVALID_CALL; return nullptr; } template<typename T> T Input::readInteger() { error = OK; int c = skipWhitespace(); if (error != OK) { return 0; } int sgn = 1; if (c == '-') { sgn = -1; c = get(); } T res = 0; do { if (!isdigit(c)) { error = UNEXPECTED_SYMBOL; return 0; } res *= 10; res += c - '0'; c = get(); } while (!isWhitespace(c)); return res * sgn; } template<> int Input::readType() { return readInteger<int>(); } template<> longint Input::readType() { return readInteger<longint>(); } template<> char Input::readType() { error = OK; int c = skipWhitespace(); if (error != OK) { return 0; } return c; } template<> string Input::readType() { error = OK; int c = skipWhitespace(); if (error != OK) { return ""; } vector<char> res; do { if (error != OK) { return ""; } res.push_back(c); } while (!isWhitespace(c = get())); return string(res.begin(), res.end()); } inline int Input::readInt() { return readType<int>(); } inline longint Input::readLong() { return readType<longint>(); } template<typename T> vector<T> Input::readArray(int size) { vector<T> res; res.reserve(size); for (int i = 0; i < size; i++) { res.push_back(readType<T>()); if (error != OK) { res.clear(); return res; } } return res; } vector<int> Input::readIntArray(int size) { return readArray<int>(size); } template<typename T1, typename T2> tuple<vector<T1>, vector<T2> > Input::readArrays(int size) { vector<T1> v1; vector<T2> v2; v1.reserve(size); v2.reserve(size); for (int i = 0; i < size; ++i) { v1.push_back(readType<T1>()); v2.push_back(readType<T2>()); } return make_tuple(v1, v2); } string Input::readString() { return readType<string>(); } template<typename T> vector<vector<T>> Input::readTable(int rows, int cols) { vector<vector<T> > result; result.reserve(rows); for (int i = 0; i < rows; ++i) { result.push_back(readArray<T>(cols)); } return result; } string Input::readLine() { error = OK; int c = skipWhitespace(); if (error != OK) { return ""; } int length = 0; vector<char> res; do { if (error != OK) { return ""; } res.push_back(c); if (!isWhitespace(c)) { length = res.size(); } c = get(); } while (c != '\n' && c != '\r' && c != EOF); return string(res.begin(), res.begin() + length); } template<typename T1, typename T2, typename T3> tuple<vector<T1>, vector<T2>, vector<T3> > Input::readArrays(int size) { vector<T1> v1; vector<T2> v2; vector<T3> v3; v1.reserve(size); v2.reserve(size); v3.reserve(size); for (int i = 0; i < size; ++i) { v1.push_back(readType<T1>()); v2.push_back(readType<T2>()); v3.push_back(readType<T3>()); } return make_tuple(v1, v2, v3); } template<typename T1, typename T2, typename T3, typename T4> tuple<vector<T1>, vector<T2>, vector<T3>, vector<T4> > Input::readArrays(int size) { vector<T1> v1; vector<T2> v2; vector<T3> v3; vector<T4> v4; v1.reserve(size); v2.reserve(size); v3.reserve(size); v4.reserve(size); for (int i = 0; i < size; ++i) { v1.push_back(readType<T1>()); v2.push_back(readType<T2>()); v3.push_back(readType<T3>()); v4.push_back(readType<T4>()); } return make_tuple(v1, v2, v3, v4); } template<typename T1, typename T2, typename T3, typename T4, typename T5> tuple<vector<T1>, vector<T2>, vector<T3>, vector<T4>, vector<T5> > Input::readArrays(int size) { vector<T1> v1; vector<T2> v2; vector<T3> v3; vector<T4> v4; vector<T5> v5; v1.reserve(size); v2.reserve(size); v3.reserve(size); v4.reserve(size); v5.reserve(size); for (int i = 0; i < size; ++i) { v1.push_back(readType<T1>()); v2.push_back(readType<T2>()); v3.push_back(readType<T3>()); v4.push_back(readType<T4>()); v5.push_back(readType<T5>()); } return make_tuple(v1, v2, v3, v4, v5); } #endif //JHELPER_EXAMPLE_PROJECT_INPUT_H // // Created by egor on 31.10.2019. // #ifndef JHELPER_EXAMPLE_PROJECT_OUTPUT_H #define JHELPER_EXAMPLE_PROJECT_OUTPUT_H class Output { private: ostream& out; template<typename T> void printSingle(const T& value); template<typename T> void printSingle(const vector<T>& value); template<typename T, typename U> void printSingle(const pair<T, U>& value); public: Output(ostream& out); void print(); template<typename T, typename...Targs>void print(const T& first, const Targs... args); template<typename...Targs>void printLine(const Targs... args); void flush(); }; Output::Output(ostream &out) : out(out) { out << setprecision(12); } void Output::print() { } template<typename T, typename... Targs> void Output::print(const T& first, const Targs... args) { printSingle(first); if (sizeof...(args) != 0) { out << ' '; print(args...); } } template<typename T> void Output::printSingle(const T& value) { out << value; } template<typename... Targs> void Output::printLine(const Targs... args) { print(args...); out << '\n'; } void Output::flush() { out.flush(); } template<typename T> void Output::printSingle(const vector<T>& array) { unsigned int size = array.size(); for (int i = 0; i < size; ++i) { out << array[i]; if (i + 1 != size) { out << ' '; } } } template<typename T, typename U> void Output::printSingle(const pair<T, U>& value) { out << value.first << ' ' << value.second; } #endif //JHELPER_EXAMPLE_PROJECT_OUTPUT_H // // Created by egor on 31.10.2019. // #ifndef JHELPER_EXAMPLE_PROJECT_ALGO_H #define JHELPER_EXAMPLE_PROJECT_ALGO_H template <class V> inline void sort(V& v) { sort(v.begin(), v.end()); } template <typename T, class C> inline void sort(vector<T>& v, C c) { sort(v.begin(), v.end(), c); } template <typename T> inline void unique(vector<T>& v) { v.resize(unique(v.begin(), v.end()) - v.begin()); } template <typename T> inline T accumulate(const vector<T>& v) { return accumulate(v.begin(), v.end(), T(0)); } vi createOrder(int n) { vi order(n); for (int i = 0; i < n; i++) { order[i] = i; } return order; } template <typename T> inline void reverse(vector<T>& v) { reverse(v.begin(), v.end()); } template <typename T> inline T minElement(const vector<T>& v) { return *min_element(v.begin(), v.end()); } template <typename T> inline T maxElement(const vector<T>& v) { return *max_element(v.begin(), v.end()); } template <typename T> inline vector<vector<T> > makeArray(int a, int b) { return vector<vector<T> >(a, vector<T>(b)); } template <typename T> inline vector<vector<vector<T> > > makeArray(int a, int b, int c) { return vector<vector<vector<T> > >(a, makeArray<T>(b, c)); } template <typename T> inline vector<vector<T> > makeArray(int a, int b, T init) { return vector<vector<T> >(a, vector<T>(b, init)); } template <typename T> inline vector<vector<vector<T> > > makeArray(int a, int b, int c, T init) { return vector<vector<vector<T> > >(a, makeArray<T>(b, c, init)); } template <typename T> inline void addAll(vector<T>& v, const vector<T>& toAdd) { v.insert(v.end(), toAdd.begin(), toAdd.end()); } template <typename T> inline void fill(vector<T>& v, const T& val) { fill(v.begin(), v.end(), val); } #endif //JHELPER_EXAMPLE_PROJECT_ALGO_H // // Created by egor on 01.11.2019. // #ifndef JHELPER_EXAMPLE_PROJECT_NUMBERS_H #define JHELPER_EXAMPLE_PROJECT_NUMBERS_H // // Created by egor on 31.10.2019. // #ifndef JHELPER_EXAMPLE_PROJECT_MODULO_H #define JHELPER_EXAMPLE_PROJECT_MODULO_H const int MOD7 = 1000000007; const int MOD9 = 1000000009; const int MODF = 998244353; int mod = MOD7; class ModuloInt { private: longint n; public: ModuloInt() : n(0) {} ModuloInt(int n) { n %= mod; if (n < 0) { n += mod; } this->n = n; } ModuloInt(const ModuloInt& n) = default; ModuloInt& operator +=(const ModuloInt& other); ModuloInt& operator -=(const ModuloInt& other); ModuloInt& operator *=(const ModuloInt& other); ModuloInt operator -(); friend ostream&operator <<(ostream& out, const ModuloInt& val); }; ModuloInt &ModuloInt::operator+=(const ModuloInt& other) { n += other.n; if (n >= mod) { n -= mod; } return *this; } ModuloInt &ModuloInt::operator-=(const ModuloInt& other) { n -= other.n; if (n < 0) { n += mod; } return *this; } ModuloInt &ModuloInt::operator*=(const ModuloInt& other) { n *= other.n; n %= mod; return *this; } ModuloInt operator +(const ModuloInt& a, const ModuloInt& b) { return ModuloInt(a) += b; } ModuloInt operator -(const ModuloInt& a, const ModuloInt& b) { return ModuloInt(a) -= b; } ModuloInt operator *(const ModuloInt& a, const ModuloInt& b) { return ModuloInt(a) *= b; } ostream& operator <<(ostream& out, const ModuloInt& val) { return out << val.n; } ModuloInt ModuloInt::operator-() { if (n == 0) { return 0; } return ModuloInt(mod - n); } #endif //JHELPER_EXAMPLE_PROJECT_MODULO_H template <typename T> T gcd(T a, T b) { a = abs(a); b = abs(b); while (b != 0) { a = a % b; swap(a, b); } return a; } template <typename T> T power(const T& a, longint b) { if (b == 0) { return 1; } if ((b & 1) == 0) { T res = power(a, b >> 1); return res * res; } else { return power(a, b - 1) * a; } } template <typename T> vector<T> generateFactorial(int length) { vector<T> result(length); if (length > 0) { result[0] = 1; } for (int i = 1; i < length; i++) { result[i] = result[i - 1] * i; } return result; } template <typename T> vector<T> generateReverse(int length) { vector<T> result(length); if (length > 1) { result[1] = 1; } for (int i = 2; i < length; i++) { result[i] = -(mod / i) * result[mod % i]; } return result; } template <typename T> vector<T> generatePowers(T base, int length) { vector<T> result(length); if (length > 0) { result[0] = 1; } for (int i = 1; i < length; i++) { result[i] = result[i - 1] * base; } return result; } template <typename T> vector<T> generateReverseFactorial(int length) { auto result = generateReverse<T>(length); if (length > 0) { result[0] = 1; } for (int i = 1; i < length; i++) { result[i] *= result[i - 1]; } return result; } template <typename T> class Combinations { private: vector<T> fact; vector<T> revFactorial; public: Combinations(int length) { fact = generateFactorial<T>(length); revFactorial = generateReverseFactorial<T>(length); } public: T c(int n, int k) { if (k < 0 || k > n) { return 0; } return fact[n] * revFactorial[k] * revFactorial[n - k]; } T factorial(int n) { return fact[n]; } T reverseFactorial(int n) { return revFactorial[n]; } }; #endif //JHELPER_EXAMPLE_PROJECT_NUMBERS_H using namespace std; class DBalanceBeam { public: void solve(std::istream& inp, std::ostream& outp) { Input in(inp); Output out(outp); int n = in.readInt(); vector<pair<longint, longint> > beams(n); for (int i = 0; i < n; ++i) { longint a = in.readInt(); longint b = in.readInt(); beams[i] = make_pair(a, b); } longint delta = 0; vector<pair<longint, longint> > bad; multiset<longint, greater<longint> > take; longint sum = 0; for (const auto& p : beams) { if (p.first <= p.second) { delta += p.second - p.first; take.insert(p.second); sum += p.second; } else { bad.push_back(p); } } if (take.empty() || delta == 0) { out.printLine(0, 1); return; } while (sum >= delta + *take.begin()) { sum -= *take.begin(); take.erase(take.begin()); } longint last = *take.begin(); longint remaining = last - (sum - delta); sort(bad, [](const pair<longint, longint>& x, const pair<longint, longint>& y) -> bool {if (x.first != y.first) {return x.first < y.first;} return x.second > y.second;}); int alpha = 0; for (const auto& p : bad) { longint a = p.first; if (last <= a) { break; } if (remaining >= a) { remaining -= a; take.insert(p.second); alpha++; continue; } longint wasLast = last; longint wasRemaining = remaining; take.erase(take.begin()); take.insert(p.second); longint newLast = *take.begin(); longint need = a - remaining; if (newLast >= a) { last = newLast; remaining = last - need; alpha++; continue; } /* if (longint(need) * wasLast + longint(newLast) * wasRemaining < longint(newLast) * wasLast) { last = newLast; remaining = last - need; } else {*/ take.erase(take.find(p.second)); take.insert(wasLast); remaining = wasRemaining; last = wasLast; break; // } // alpha++; } longint bestRemaining = remaining; longint bestLast = last; for (int i = alpha; i < bad.size(); i++) { longint a = bad[i].first; const pair<long, long>& p = bad[i]; longint wasLast = last; longint wasRemaining = remaining; take.erase(take.begin()); take.insert(p.second); longint newLast = *take.begin(); longint need = a - remaining; longint newRemaining = newLast - need; if (newRemaining * bestLast > bestRemaining * newLast) { bestRemaining = newRemaining; bestLast = newLast; } take.erase(take.find(p.second)); take.insert(wasLast); remaining = wasRemaining; last = wasLast; } last = bestLast; remaining = bestRemaining; longint p = longint(take.size() - 1) * last + remaining; longint q = longint(last) * n; longint g = gcd(p, q); p /= g; q /= g; out.printLine(p, q); } }; int main() { std::ios::sync_with_stdio(false); DBalanceBeam solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
#include <bits/stdc++.h> #define N 100005 #define ll long long #define For(i,x,y) for(int i=(x);i<=(y);++i) #define pll pair<ll,ll> #define fr first #define sd second using namespace std; pll a[N]; ll ansx=0,ansy=1,sum[N],S=0; bool cmp(const pll &a,const pll &b){ return a.fr>b.fr; } ll calc(int x,int y){ return y<=x?sum[x]-a[y].fr:sum[x]; } ll gcd(ll x,ll y){ return x%y?gcd(y,x%y):y; } int main(){ int n; scanf("%d",&n); For(i,1,n){ scanf("%lld%lld",&a[i].fr,&a[i].sd); S+=a[i].fr; a[i].fr=max(a[i].fr,a[i].sd); } sort(a+1,a+n+1,cmp); For(i,1,n) sum[i]=sum[i-1]+a[i].fr; For(i,1,n){ int l=0,r=n,num=-1; while(l<=r){ int mid=(l+r)>>1; if(calc(mid,i)>=S-a[i].sd) num=mid,r=mid-1; else l=mid+1; } if(num<0) continue; if(calc(num,i)>=S) continue; ll nowx=1ll*(1ll*n-(num-(i<=num)))*a[i].sd-S+calc(num,i),nowy=1ll*n*a[i].sd; ll p=gcd(nowx,nowy);nowx/=p,nowy/=p; if(1.0*nowx*ansy>1.0*nowy*ansx) ansx=nowx,ansy=nowy; } printf("%lld %lld\n",ansx,ansy); }
/* * @Author: RBQRBQ * @Date: 2020-04-06 20:28:58 * @LastEditors: RBQRBQ * @LastEditTime: 2020-04-08 19:09:22 */ #include<bits/stdc++.h> using namespace std; typedef long long ll; #define int ll typedef vector<int> VI; typedef pair<int,int> pii; namespace IO{ template<typename T>inline void read(T &x){ x=0;ll f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();} while(isdigit(ch)){x=x*10+ch-48;ch=getchar();} x=x*f; } } using namespace IO; typedef pair<ll,ll> frac; ll N,S=0; vector<frac> T; bool mp(frac A,frac B) { return (A.first*B.second<A.second*B.first); } ll gcd(ll x,ll y) { return (!y)?x:gcd(y,x%y); } signed main() { read(N); for(int i=1;i<=N;i++) { ll x,y; read(x),read(y); S+=x; T.push_back(make_pair(std::max(x,y),y)); } sort(T.begin(),T.end(),greater<frac>()); ll pos=0; ll point=0; int ca=0; for(auto it=T.begin();it!=T.end();it++) { if((pos+it->first) >S) break; pos+=it->first; point=it-T.begin(); ca++; } if(ca==N){cout<<"0 1"<<endl;return 0;} frac ans(0,1); for(int i=0;i<T.size();++i) { if(i+1>ca) { if(pos+T[i].second>=S) { frac res(pos+T[i].second-S,T[i].second); ans=(mp(ans,res))?res:ans; } } else { if(pos-T[i].first+T[point+1].first+T[i].second>=S) { frac res(pos-T[i].first+T[point+1].first+T[i].second-S,T[i].second); ans=(mp(ans,res))?res:ans; } } } ans.first+=ans.second*(N-ca-1);ans.second*=N; ll _f=gcd(ans.first,ans.second); cout<<ans.first/_f<<" "<<ans.second/_f<<endl; return 0; }
#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<class T> T gcd(T n,T m){ if(n>m) return gcd(m,n); else if(n==0) return m; else return gcd(m-n*(m/n),n); } struct rat{ ll p,q; rat(ll p_=0,ll q_=1){ ll k=gcd(abs(p_),abs(q_)); p=(p_/k)*(q_/abs(q_)); q=abs(q_)/k; } bool operator==(const rat& x){ return p==x.p&&q==x.q; } bool operator<(const rat& x){ return p*x.q<x.p*q; } bool operator>(const rat& x){ return p*x.q>x.p*q; } rat& operator=(const rat& x){ p=x.p;q=x.q; return *this; } rat operator+(const rat& x){ rat y(p*x.q+x.p*q,q*x.q); return y; } rat operator-(const rat& x){ rat y(p*x.q-x.p*q,q*x.q); return y; } rat operator*(const rat& x){ rat y(p*x.p,q*x.q); return y; } rat operator/(const rat& x){ rat y(p*x.q,q*x.p); return y; } rat& operator+=(const rat& x){ *this=*this+x; return *this; } rat& operator-=(const rat& x){ *this=*this-x; return *this; } rat& operator*=(const rat& x){ *this=*this*x; return *this; } rat& operator/=(const rat& x){ *this=*this/x; return *this; } }; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); istream& in(cin); ostream& out(cout); ll S{}; int n; in>>n; vector<int> a(n),b(n),p(n),v(n); vector<ll> s(n+1); fr(i,n) in>>a[i]>>b[i],p[i]=i,S+=a[i]; sort(p.begin(),p.end(),[&](int i,int j){ return max(a[i],b[i])>max(a[j],b[j]); }); for(int i=0,j=p[0];i<n;++i,j=p[i]){ s[i+1]=s[i]+max(a[j],b[j]); v[j]=i; } using ld=long double; ld x=0,nx; ll ap=0,aq=1; for(auto i:p){ int j=lower_bound(s.begin(),s.end(),S-b[i])-s.begin(); if(v[i]>=j){ if(S-s[j]<0) continue; nx=(ld)((ll)(n-j)*b[i]-S+s[j])/((ll)n*b[i]); if(nx>x){ x=nx; rat r((ll)(n-j)*b[i]-S+s[j],(ll)n*b[i]); ap=r.p,aq=r.q; } } else{ j=lower_bound(s.begin(),s.end(),S+max(a[i],b[i])-b[i])-s.begin(); if(S-s[j]+max(a[i],b[i])<0) continue; nx=(ld)((ll)(n-j+1)*b[i]-S+s[j]-max(a[i],b[i]))/((ll)n*b[i]); if(nx>x){ x=nx; rat r((ll)(n-j+1)*b[i]-S+s[j]-max(a[i],b[i]),(ll)n*b[i]); ap=r.p,aq=r.q; } } } cout<<ap<<" "<<aq<<endl; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define inf (1LL<<60) typedef long long ll; #define rep(i,n) for(int i = 0; i < (int)(n); ++i) template<typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } int main(){ ll n; cin >> n; vector<pair<ll,ll> > a; ll s = 0; rep(i,n){ ll c,d; cin >> c >> d; if(c<d){ s += d-c; a.push_back(make_pair(d,inf)); }else{ a.push_back(make_pair(c,d)); } } ll sm = s; if(s==0){ cout << 0 << " " << 1 << endl; return 0; } sort(a.begin(),a.end()); ll k = 0; ll pid = -1; ll id = -1; ll sk = 0; ll sk1 = 0; rep(i,n){ if(s>=a[i].first){ k++; s-=a[i].first; }else{ sk = s; sk1 = sk - a[i].first; break; } } ll x = k; ll y = n; ll z = gcd(k,n); x /=z; y /=z; ll xx,yy,zz; //cerr << sk << " " << sk1 << endl; rep(i,n){ // cerr << a[i].first << " " << a[i].second << endl; // cerr << x << " " << y << endl; if(a[i].second==inf){ if(i<k){ if(sk1 + a[i].first >0){ xx = a[i].first*k + sk1+a[i].first; yy = a[i].first*n; }else{ continue; } }else{ if(sk >0){ xx = a[i].first*k + sk; yy = a[i].first*n; }else{ continue; } } zz = gcd(xx,yy); xx /=zz; yy /=zz; if((__uint128_t)(x) * (__uint128_t)(yy) < (__uint128_t)(y) * (__uint128_t)(xx) ){ x = xx; y = yy; } }else{ if(i<k){ if(sk1 + a[i].second >0){ xx = a[i].second*k + sk1 + a[i].second; yy = a[i].second*n; }else{ continue; } }else{ if(sk - a[i].first+a[i].second >0){ xx = a[i].second*k + sk - a[i].first+a[i].second; yy = a[i].second*n; }else{ continue; } } zz = gcd(xx,yy); xx /=zz; yy /=zz; if((__uint128_t)(x) * (__uint128_t)(yy) < (__uint128_t)(y) * (__uint128_t)(xx) ){ x = xx; y = yy; } } } cout << x << " " << y << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef bool boolean; #define ll long long #define ld long double template <typename T> boolean vmax(T& a, T b) { return (a < b) ? (a = b, true) : (false); } const int N = 1e5 + 5; const ll llf = 1e17; const int inf = (signed) (~0u >> 2); ll gcd(ll a, ll b) { return (b) ? (gcd(b, a % b)) : (a); } typedef class Fraction { public: ll a, b; Fraction() : a(0), b(1) { } Fraction(ll a, ll b) : a(a), b(b) { } boolean operator < (Fraction B) const { return ((ld) a) * B.b - ((ld) b) * B.a < 0; } boolean operator > (Fraction B) const { return ((ld) a) * B.b - ((ld) b) * B.a > 0; } } Fraction; #define pii pair<int, int> typedef class Item { public: int a, b, id; Item() { } Item(int a, int b, int id) : a(a), b(b), id(id) { } int maxval() const { return max(a, b); } boolean operator < (Item i) const { return maxval() < i.maxval(); } } Item; int n; vector<Item> E; ll _presum[N], *presum; int main() { scanf("%d", &n); ll _sum = 0; for (int i = 1, a, b; i <= n; i++) { scanf("%d%d", &a, &b); E.emplace_back(a, b, i); _sum += max(b - a, 0); } sort(E.begin(), E.end()); Fraction ans (0, 1), I (1, 1); presum = _presum + 1; presum[-1] = 0; for (int i = 0; i < n; i++) presum[i] = presum[i - 1] + E[i].maxval(); for (int j = 0; j < n; j++) { ll sum = _sum, ts; Item& e = E[j]; if (e.a > e.b) sum += e.b - e.a; int l = 0, r = n - 1, mid; #define calc(p) (presum[p] - (p >= j) * e.maxval()) while (l <= r) { mid = (l + r) >> 1; ts = calc(mid); if (ts <= sum) { l = mid + 1; } else { r = mid - 1; } } Fraction tmp (sum - calc(l - 1), e.b); tmp = min(tmp, I); tmp.a += tmp.b * (l - (l > j)); ans = max(ans, tmp); } ll g = gcd(ans.a, ans.b *= n); ans.a /= g; ans.b /= g; printf("%lld %lld\n", ans.a, ans.b); return 0; }
#include <bits/stdc++.h> using namespace std; using lint = long long; using pi = pair<int, int>; using frac = pair<lint, lint>; const int MAXN = 100005; int n; pi a[MAXN]; lint sum[MAXN]; bool cmp(frac a, frac b){ return (__int128)a.first * b.second < (__int128)b.first * a.second; } lint gcd(lint x, lint y){ return y ? gcd(y, x%y) : x; } int main(){ scanf("%d",&n); for(int i=0; i<n; i++) scanf("%d %d",&a[i].first,&a[i].second); sort(a, a + n, [&](const pi &p, const pi &q){ return max(p.first, p.second) > max(q.first, q.second); }); frac dap(n, 1); auto upd_val = [&](lint n, lint a, lint b){ // assert(a <= b); a += n * b; dap = min(dap, frac(a, b), cmp); }; lint sumA = 0; for(int i=1; i<=n; i++){ sumA += a[i-1].first; sum[i] = sum[i-1] + max(a[i-1].first, a[i-1].second); } for(int i=0; i<n; i++){ if(sum[i] >= sumA - a[i].second){ auto lb = lower_bound(sum, sum + i + 1, sumA - a[i].second) - sum; lint tmp = sumA - sum[lb]; if(tmp <= 0){ upd_val(lb, 0, 1); } else{ upd_val(lb, tmp, a[i].second); } } else{ lint newval = sumA + max(a[i].first, a[i].second); auto lb = lower_bound(sum + i + 1, sum + n + 1, newval - a[i].second) - sum; lint tmp = newval - sum[lb]; if(tmp <= 0) upd_val(lb - 1, 0, 1); else{ if(tmp <= a[i].second) upd_val(lb - 1, tmp, a[i].second); } } } dap.first = (n * dap.second - dap.first); dap.second *= n; { lint g = gcd(dap.first, dap.second); dap.first /= g; dap.second /= g; } printf("%lld %lld\n", dap.first, dap.second); }
#include <bits/stdc++.h> #define get suka #define to_str blyad #define ll long long #define db long double #define x first #define y second #define mp make_pair #define pb push_back #define all(a) a.begin(), a.end() using namespace std; struct Piece{int a; int b; int index;}; bool cmp(Piece &a, Piece &b){ return (a.b < b.b); } bool cmp2(Piece &a, Piece &b){ return (a.a < b.a); } struct Vertex{ll sum; int cnt;}; vector<Vertex> rmq; bool more(ll a, ll b, ll c, ll d){ __int128_t A = a, B = b, C = c, D = d; return (A*D >= B*C); } __int128_t gcd(__int128_t x, __int128_t y){ if (y==0) return x; return gcd(y, x%y); } string to_str(__int128_t x){ string res = ""; if (x==0) return "0"; while (x > 0){ res += (char) ('0' + x%10); x /= 10; } reverse(res.begin(), res.end()); return res; } pair<ll, int> get(int i, int l, int r, ll delta){ if (r-l==1){ if (rmq[i].cnt == 0) return {0, 0}; if (rmq[i].sum > delta) return {0, 0}; return {rmq[i].sum, 1}; } int mid = (l+r)/2; if (rmq[2*i+1].sum > delta){ return get(2*i+1, l, mid, delta); } ll S = rmq[2*i+1].sum; int c = rmq[2*i+1].cnt; pair<ll, int> res = get(2*i+2, mid, r, delta-S); return {res.first+S, res.second+c}; } void change(int i, int l, int r, int index, ll val){ if (r-l==1){ rmq[i] = {val, 1}; return; } int mid = (l+r)/2; if (index < mid) change(2*i+1, l, mid, index, val); else change(2*i+2, mid, r, index, val); rmq[i].sum = rmq[2*i+1].sum + rmq[2*i+2].sum; rmq[i].cnt = rmq[2*i+1].cnt + rmq[2*i+2].cnt; } int main(){ #ifdef LOCAL freopen("A_input.txt", "r", stdin); //freopen("A_output.txt", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<Piece> v; for (int i=0; i < n; ++i){ int a, b; cin >> a >> b; v.push_back({a, b, i}); } sort(v.begin(), v.end(), cmp2); rmq.assign(4*n, {0, 0}); vector<int> posa(n); for (int i=0; i < n; ++i){ posa[v[i].index] = i; } sort(v.begin(), v.end(), cmp); ll delta = 0; ll A = 0, e = 0, q = 1; for (int i=0;i<n;++i){ if (v[i].b >= v[i].a) delta += (v[i].b - v[i].a); } for (int i=0; i < n; ++i){ if (v[i].b >= v[i].a){ delta -= (v[i].b - v[i].a); pair<ll, int> p = get(0, 0, n, delta+(v[i].b - v[i].a)); ll C = v[i].b-v[i].a-(p.first - delta), zn = v[i].b; //cout << i << " " << p.first << " " << p.second << " " << delta << " " << C << " " << zn << endl; if (C >= zn){ C = 0, zn = 1, p.second++; } if (p.second > A){ A = p.second, e = C, q = zn; } else if (p.second == A){ if (more(C, zn, e, q)){ e = C, q = zn; } } } else{ pair<ll, int> p = get(0, 0, n, delta+(v[i].b - v[i].a)); ll C = v[i].b-v[i].a-(p.first - delta), zn = v[i].b; if (C >= 0){ //cout << i << " " << p.first << " " << p.second << " " << delta << " " << C << " " << zn << endl; if (C >= zn){ C = 0, zn = 1, p.second++; } if (p.second > A){ A = p.second, e = C, q = zn; } else if (p.second == A){ if (more(C, zn, e, q)){ e = C, q = zn; } } } } int I = v[i].index; change(0, 0, n, posa[I], v[i].a); } //cout << A << " " << e << " " << q << endl; __int128_t a = A, E = e, Q = q; E += Q*a; Q *= n; __int128_t g = gcd(E, Q); E /= g, Q /= g; string T = to_str(E), B = to_str(Q); cout << T << " " << B; }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; const int maxn=1E5+5; ll n,sum,pre[maxn]; struct pt { ll x,y; bool operator<(const pt&A)const { return max(x,y)<max(A.x,A.y); } }a[maxn]; inline int get(int pos) { int l=0,r=n,mid;// !!!!!!!!!!!!!!! int d=max(a[pos].x,a[pos].y); while(l<r) { mid=(l+r+1)>>1; ll s=pre[mid]-(mid>=pos?d:0); if(s<=sum) l=mid; else r=mid-1; } return l; } ll gcd(ll x,ll y) { if(y==0) return x; return x%y==0?y:gcd(y,x%y); } int main() { ios::sync_with_stdio(false); cin>>n; for(int i=1;i<=n;++i) { cin>>a[i].x>>a[i].y; sum+=max((ll)0,a[i].y-a[i].x); } sort(a+1,a+n+1); for(int i=1;i<=n;++i) pre[i]=pre[i-1]+max(a[i].x,a[i].y); ll ax=0,ay=1; for(int i=1;i<=n;++i) { int pos=get(i); ll s=sum-pre[pos]; if(pos>=i) --pos,s+=max(a[i].x,a[i].y); if(a[i].y>a[i].x) s-=a[i].y-a[i].x;//!!!!!!!!!!!!! ll x=(ll)pos*a[i].y+min(s+a[i].y-a[i].x,a[i].y); ll y=n*a[i].y; if(x<=0) continue; ll z=gcd(x,y); x/=z,y/=z; if((ld)ay*x>(ld)y*ax) ax=x,ay=y; } cout<<ax<<" "<<ay<<endl; return 0; }
#include <cstdio> #include <vector> #include <algorithm> #include <functional> long long gcd(long long a,long long b) { return b==0?a:gcd(b,a%b); } int main() { int n; scanf("%d",&n); std::vector<long long> A(n),B(n); for(int i=0;i<n;i++){ scanf("%lld%lld",&A[i],&B[i]); } std::vector<std::pair<long long,int> > P(n); for(int i=0;i<n;i++){ P[i]=std::make_pair(std::max(A[i],B[i]),i); } std::sort(P.begin(),P.end(),std::greater<std::pair<long long,int> >()); std::vector<long long> SP(n+1); SP[0]=0ll; for(int i=0;i<n;i++){ SP[i+1]=SP[i]+P[i].first; } long long S=0ll; for(int i=0;i<n;i++) S+=A[i]; long long ansp=0,ansq=1; for(int t=0;t<n;t++){ int k=P[t].second; int c; long long s; if(SP[t]+B[k]>=S){ c=std::lower_bound(SP.begin(),SP.end(),S-B[k])-SP.begin(); s=SP[c]; } else{ c=std::lower_bound(SP.begin(),SP.end(),S-B[k]+P[t].first)-SP.begin()-1; s=SP[c+1]-P[t].first; } long long q=B[k]; long long p=B[k]*(n-1-c)+(B[k]-std::max(0ll,S-s)); if(ansp/ansq<p/q||(ansp/ansq==p/q&&(ansp-ansp/ansq*ansq)*q<(p-p/q*q)*ansq)){ ansp=p; ansq=q; } } long long g=gcd(ansp,ansq*n); printf("%lld %lld\n",ansp/g,ansq*n/g); return 0; }
#include<bits/stdc++.h> using namespace std; int n,a[111111],b[111111],cnt,mx,nw; pair<int,int> arr[111111]; long long sum,fuck,x,y,d,pp; long long gcd(long long a,long long b) { if (!b) return a; return gcd(b,a%b); } struct frac { long long x,y; bool operator > (const frac &u) const { return ((double)x/(double)y>(double)u.x/(double)u.y); } }cur,ass; int main() { scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%d%d",&a[i],&b[i]); sum+=a[i]; } for (int i=1;i<=n;i++) { arr[i]=make_pair(max(a[i],b[i]),i); } sort(arr+1,arr+n+1); reverse(arr+1,arr+n+1); for (int i=1;i<=n;i++) { fuck+=arr[i].first; if (fuck>=sum) { cnt=i; break; } } /*x=n-cnt;y=n; d=gcd(x,y);x/=d;y/=d; cur.x=x;cur.y=y; for (int i=1;i<=n;i++) { nw=b[arr[i].second]; if (i<cnt) pp=max(0ll,sum-fuck+arr[i].first); else pp=max(0ll,sum-fuck+arr[cnt].first); if (pp>=nw) continue; x=1ll*nw*(n-cnt)+nw-pp;y=1ll*nw*n; d=gcd(x,y);x/=d;y/=d; ass.x=x;ass.y=y; if (ass>cur) cur=ass; }*/ x=n-cnt;y=n; d=gcd(x,y);x/=d;y/=d; cur.x=x;cur.y=y; mx=-1e9; for (int i=cnt+1;i<=n;i++) mx=max(mx,b[arr[i].second]); for (int i=1;i<=cnt;i++) { nw=max(mx,b[arr[i].second]); pp=max(0ll,sum-fuck+arr[i].first); if (pp>=nw) continue; x=1ll*nw*(n-cnt)+nw-pp;y=1ll*nw*n; d=gcd(x,y);x/=d;y/=d; ass.x=x;ass.y=y; if (ass>cur) cur=ass; } printf("%lld %lld\n",cur.x,cur.y); return 0; }
#include<bits/stdc++.h> using namespace std; long long a1[13]={1,2,4,7,12,20,29,38,52,101},a2[13]={1,2,4,7,12,20,30,39,67,101},n,an[15][15],no=1; int main(){ cin>>n; for (int i=1;i<=n;i++)an[i][i]=0; for (int i=1;i<=n;i++){ for (int j=i+1;j<=n;j++)an[i][j]=an[j][i]=no*a1[j-i-1]; no*=a2[n-i]; } for (int i=1;i<=n;i++){ for (int j=1;j<=n;j++)cout<<an[i][j]<<' '; cout<<endl; } return 0; }
#include<iostream> #define fr(i,a,b) for(int i=a;i<=b;i++) int f[]={0,1,2,4,7,12,20,29,38,52,73},n; int main() { std::cin>>n; long d[11][11]={0},p=1; fr(i,2,n){fr(j,1,i-1)d[i][j]=d[j][i]=p*f[j];p+=d[i][i-1]+d[i][i-2]-d[i-1][i-2];} fr(i,1,n)fr(j,1,n)std::cout<<d[i][j]<<" \n"[j==n]; return 0; }
//This Code was made by Chinese_zjc_. #include <iostream> #include <fstream> #include <iomanip> #include <algorithm> #include <vector> #include <bitset> #include <cmath> #include <queue> #include <stack> #include <string> #include <cstring> #include <cstdio> #include <cstdlib> #include <map> #include <set> #include <ctime> // #include<windows.h> #define int long long #define double long double using namespace std; const double PI = acos(-1); const double eps = 0.0000000001; const int INF = 0x3fffffffffffffff; int n, w[11][11] = { {}, {0, 0, 1, 2, 7, 45, 512, 9925, 321381, 15870550, 1069877351}, {0, 1, 0, 4, 14, 90, 1024, 19850, 642762, 31741100, 2139754702}, {0, 2, 4, 0, 28, 180, 2048, 39700, 1285524, 63482200, 4279509404}, {0, 7, 14, 28, 0, 315, 3584, 69475, 2249667, 111093850, 7489141457}, {0, 45, 90, 180, 315, 0, 6144, 119100, 3856572, 190446600, 12838528212}, {0, 512, 1024, 2048, 3584, 6144, 0, 198500, 6427620, 317411000, 21397547020}, {0, 9925, 19850, 39700, 69475, 119100, 198500, 0, 9320049, 460245950, 31026443179}, {0, 321381, 642762, 1285524, 2249667, 3856572, 6427620, 9320049, 0, 603080900, 40655339338}, {0, 15870550, 31741100, 63482200, 111093850, 190446600, 317411000, 460245950, 603080900, 0, 56703499603}, {0, 1069877351, 2139754702, 4279509404, 7489141457, 12838528212, 21397547020, 31026443179, 40655339338, 56703499603, 0}, }; signed main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cout << w[i][j] << ' '; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef vector<int> vi; #define pb push_back #define eb emplace_back #define mp make_pair #define fi first #define se second #define rep(i,n) rep2(i,0,n) #define rep2(i,m,n) for(int i=m;i<(n);i++) #define ALL(c) (c).begin(),(c).end() #define dump(x) cout << #x << " = " << (x) << endl constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); } 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<<"{"; rep(i, v.size()) { if (i) os<<","; os<<v[i]; } os<<"}"; return os; } const int maxn = 10; ll d[maxn][maxn]; int main() { vector<ll> dp(maxn); dp[0] = 1; dp[1] = 2; for (int i = 2; i < maxn; ++i) { set<ll> st; rep(j, i) rep(k, j) { st.insert(dp[k] + dp[j]); } ll c = dp[i-1] + 1; while (true) { bool ok = true; rep(j, i) { if (st.count(c + dp[j]) || st.count(c)) { ok = false; break; } } if (ok) { break; } c++; } dp[i] = c; } //cout << dp << endl; int N; cin >> N; d[0][1] = d[1][0] = 1; for (int i = 2; i < N; ++i) { ll c = 0; vi pm; rep(j, i) pm.pb(j); vector<ll> cst; do { ll s = 0; rep(j, (int)pm.size()-1) { s += d[pm[j]][pm[j+1]]; } cst.pb(s); c = max(c, s); } while (next_permutation(ALL(pm))); ++c; //dump(cst); //sort(ALL(cst)); //cst.erase(unique(ALL(cst)), cst.end()); //dump(cst.size()); //cerr << i << " " << c << endl; rep(j, i) d[j][i] = d[i][j] = c * dp[j]; } rep(i, N) { rep(j, N) { printf("%lld%c", d[i][j], j == N-1 ? '\n' : ' '); } } return 0; }
#include <bits/stdc++.h> #define mod 1000000007 #define mod998 998244353 #define sp ' ' #define intmax 2147483647 #define llmax 9223372036854775807 #define mkp make_pair typedef long long ll; using namespace std; const ll x[10] = { 1,2,4,7,12,20,33,54,88,143 }; int N; ll a[10][10], b; int main() { cin >> N; b = 1; for (int i = 0; i < N; ++i) { for (int j = i + 1; j < N; ++j) { a[i][j] = a[j][i] = x[j - i - 1] * b + 1; } b *= x[9 - i] - 1; } a[9][8] = a[8][9] = 1; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { cout << a[i][j]; if (j != N - 1)cout << sp; } cout << endl; } /* vector<int>v{ 0,1,2,3,4,5,6,7,8,9 }; map<ll, vector<int>>mp; do { if (v.front() > v.back())continue; ll t = 0; for (int i = 0; i < 9; ++i) { t += a[v[i]][v[i + 1]]; } if (t > 1e11) { cout << t << endl; for (int i : v) { cout << i << sp; } cout << endl; } if (mp.find(t) != mp.end()) { cout << t << endl; for (int i : mp[t]) { cout << i << sp; } cout << endl; for (int i : v) { cout << i << sp; } cout << endl; } else { mp[t] = v; } } while (next_permutation(v.begin(), v.end()));*/ }
#include <bits/stdc++.h> #define ll long long #define lld long double #define MOD 1000000007 #define CAP 100000001 #define inf 1000000000000000000LL #define pii pair<ll,ll> #define f first #define s second #define pb push_back #define mp make_pair #define endl '\n' #define sz(v) v.size() #define all(v) v.begin(),v.end() #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); using namespace std; ll power(ll x,ll y, ll md=MOD){ll res = 1;x%=md;while(y>0){if(y&1)res = (res*x)%md;x = (x*x)%md;y = y>>1;}return res%md;} #define dbg(x) cout <<#x<<":"<<x<<endl; ll max1(ll a, ll b, ll c=-inf, ll d=-inf) {ll mx1=(a>b)?a:b, mx2=(c>d)?c:d; return ((mx1>mx2)?mx1:mx2);} #define int ll signed main() { fast; ll a[10][10]; for(int i=0;i<10;i++) a[i][i]=0; ll n; cin>>n; ll aa[n]; for(int i=0;i<n;i++) aa[i]=i; map<ll,ll> mp1; ll v[]={1, 2, 4, 7, 12, 20, 29, 38, 52}; ll mul=1; for(int i=0;i<n;i++){ ll m1; for(int j=0;j<n;j++){ if(j>i){ a[i][j]=a[j][i]=v[j-i-1]*mul; m1=v[j-i-1]; } //assert(a[i][j]<=10000000000); } mul*=v[n-1-i-1]+v[n-1-i-2]; } for(int i=0;i<n;i++){ for(int j=0;j<n;j++) cout<<a[i][j]<<" "; cout<<endl; } }
#include<bits/stdc++.h> #define int long long using namespace std; const int N=15; const int a[11]={0,1,2,4,7,12,20,29,38,52,73}; int read() { int s=0; char c=getchar(),lc='+'; while (c<'0'||'9'<c) lc=c,c=getchar(); while ('0'<=c&&c<='9') s=s*10+c-'0',c=getchar(); return lc=='-'?-s:s; } void write(int x) { if (x<0) { putchar('-'); x=-x; } if (x<10) putchar(x+'0'); else { write(x/10); putchar(x%10+'0'); } } void print(int x,char c='\n') { write(x); putchar(c); } int e[N][N],p[N]; signed main() { memset(e,0,sizeof(e)); int n=read(),Max=0; for (int i=1;i<=n;i++) { for (int j=1;j<i;j++) e[i][j]=e[j][i]=a[j]*(Max+1); for (int j=1;j<=i;j++) p[j]=j; do { int sum=0; for (int j=1;j<i;j++) sum+=e[p[j]][p[j+1]]; Max=max(Max,sum); }while (next_permutation(p+1,p+i+1)); } for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) print(e[i][j],' '); putchar('\n'); } return 0; }
#include<iostream> int A[9] = {1,2,4,7,12,20,29,38,52}; long long B[11][11] , N; int main(){ std::cin >> N; long long MX = 1; for(int i = 2 ; i <= N ; ++i){ for(int j = 1 ; j < i ; ++j) B[i][j] = B[j][i] = MX * A[j - 1]; MX += B[i][i - 1] + B[i][i - 2] - B[i - 1][i - 2]; } for(int i = 1 ; i <= N ; ++i) for(int j = 1 ; j <= N ; ++j) std::cout << B[i][j] << " \n"[j == N]; return 0; }
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int N=15; const int a[N]={0,1,2,4,7,12,20,29,38,52,73}; int n,p[N]; long long g[N][N]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { for(int j=1;j<=i;j++) p[j]=j; long long M=0; do { long long s=0; for(int j=1;j<i;j++) s+=g[p[j]][p[j+1]]; M=max(M,s); } while(next_permutation(p+1,p+i+1)); for(int j=1;j<i;j++) g[i][j]=g[j][i]=(M+1)*a[j]; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) printf("%lld ",g[i][j]); printf("\n"); } return 0; }
#include <iostream> #include <vector> using namespace std; typedef long long ll; int main(){ ll w[11][11] = {}; w[0][1] = w[1][0] = 1; w[2][0] = w[0][2] = 2; w[2][1] = w[1][2] = 3; w[3][0] = w[0][3] = 4; w[3][1] = w[1][3] = 8; w[3][2] = w[2][3] = 13; ll l = 23; for(int k = 4 ; k < 10 ; k ++){ vector<ll> vec; vec.push_back(1); vec.push_back(2); while(vec.size() < k){ vec.push_back(vec[vec.size()-2]+vec[vec.size()-1]+1); } int cnt = 0; for(int i = 1 ; i <= k-2 ; i ++){ cnt += i; } for(int t = 0 ; t < k ; t ++){ w[k][t] = w[t][k] = (l-cnt)*vec[t]; } l = (l-cnt)*(vec[vec.size()-2]+vec[vec.size()-1]+1)-12; } int n; cin >> n; for(int i = 0 ; i < n ; i ++){ for(int j = 0 ; j < n ; j ++){ printf("%lld%c",w[i][j],(j==n-1)?'\n':' '); } } }
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; #define ll long long int n,p[11],a[11]={0,1,2,4,7,12,20,29,38,52,73}; ll g[11][11],M; int main() { scanf("%d",&n); for(int i=2;i<=n;++i) { for(int j=1;j<i;++j)g[i][j]=g[j][i]=(M+1)*a[j]; if(i==n)break; for(int j=1;j<=i;++j)p[j]=j; do { ll s=0; for(int j=1;j<i;++j)s+=g[p[j]][p[j+1]]; M=max(M,s); }while(next_permutation(&p[1],&p[i+1])); } for(int i=1;i<=n;++i,puts("")) for(int j=1;j<=n;++j) printf("%lld ",g[i][j]); return 0; }
#include<bits/stdc++.h> #define For(i,x,y) for (register int i=(x);i<=(y);i++) #define FOR(i,x,y) for (register int i=(x);i<(y);i++) #define Dow(i,x,y) for (register int i=(x);i>=(y);i--) #define Debug(v) for (auto i:v) printf("%lld ",i);puts("") #define mp make_pair #define fi first #define se second #define pb push_back #define ep emplace_back #define siz(x) ((int)(x).size()) #define all(x) (x).begin(),(x).end() #define fil(a,b) memset((a),(b),sizeof(a)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pa; typedef pair<ll,ll> PA; typedef vector<int> poly; inline ll read(){ ll x=0,f=1;char c=getchar(); while ((c<'0'||c>'9')&&(c!='-')) c=getchar(); if (c=='-') f=-1,c=getchar(); while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); return x*f; } const int N = 11; const int f[11] = {0,1,2,4,7,12,20,29,38,52,73}; int n,p[N]; ll mx,a[N][N]; int main(){ n=read(); For(i,2,n){ FOR(j,1,i) a[j][i]=a[i][j]=(mx+1)*f[j]; For(j,1,i) p[j]=j; do { if (p[1]>p[i]) continue; ll ret=0; For(j,2,i) ret+=a[p[j-1]][p[j]]; mx=max(mx,ret); }while (next_permutation(p+1,p+1+i)); } For(i,1,n){ For(j,1,n) printf("%lld ",a[i][j]); puts(""); } }
#include <bits/stdc++.h> using i64 = long long; int main() { int n; std::cin >> n; std::vector<i64> a { 1 }; std::set<i64> s { 1 }; for (int i = 1; i <= n; i++) { i64 x = a.back(); bool ok = false; while (!ok) { x++; while (s.count(x)) x++; ok = true; for (int j = 0; j < i; j++) { if (s.count(a[j] + x)) { ok = false; break; } } } s.insert(x); for (int j = 0; j < i; j++) { s.insert(a[j] + x); } a.push_back(x); } // for (int i = 0; i < a.size(); i++) std::cout << a[i] << std::endl; std::vector<std::vector<i64>> ret(n, std::vector<i64>(n)); i64 m = 0; for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { ret[i][j] = ret[j][i] = (m + 1) * a[j]; } std::vector<int> p; for (int j = 0; j <= i; j++) p.push_back(j); do { i64 k = 0; for (int j = 0; j < i; j++) k += ret[p[j]][p[j + 1]]; m = std::max(m, k); } while (std::next_permutation(p.begin(), p.end())); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) std::cout << ret[i][j] << " "; std::cout << std::endl; } return 0; }
#include<iostream> #define fr(i,a,b) for(int i=a;i<=b;i++) int f[]={0,1,2,4,7,12,20,29,38,52,73},n; int main() { std::cin>>n;////////////////////// long d[11][11]={0},p=1; fr(i,2,n){fr(j,1,i-1)d[i][j]=d[j][i]=p*f[j];p+=d[i][i-1]+d[i][i-2]-d[i-1][i-2];} fr(i,1,n)fr(j,1,n)std::cout<<d[i][j]<<" \n"[j==n]; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long LL; const int N=10; const int a[N+9]={0,1,2,4,7,12,20,29,38,52,73}; int n,p[N+9]; LL ans[N+9][N+9],mx; void into(){ scanf("%d",&n); } void Get_ans(){ for (int i=2;i<=n;++i){ for (int j=1;j<i;++j) ans[i][j]=ans[j][i]=(mx+1)*a[j]; if (i==n) break; for (int j=1;j<=i;++j) p[j]=j; for (;2333;){ LL sum=0; for (int j=1;j<i;++j) sum+=ans[p[j]][p[j+1]]; mx=max(mx,sum); if (!next_permutation(p+1,p+i+1)) break; } } } void work(){ Get_ans(); } void outo(){ for (int i=1;i<=n;++i){ for (int j=1;j<=n;++j) printf("%lld ",ans[i][j]); puts(""); } } int main(){ into(); work(); outo(); return 0; }
#include <iostream> #include <vector> #include <cstring> 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; } const long long w[10] = {1, 2, 4, 7, 12, 20, 29, 38, 52, 73}; long long dp[(1<<10)+1][11]; long long calc(vector<vector<long long> > &G, int N) { memset(dp, 0, sizeof(dp)); for (int bit = 0; bit < (1<<N); ++bit) { for (int v = 0; v < N; ++v) { if (!(bit & (1<<v))) continue; for (int nv = 0; nv < N; ++nv) { if (bit & (1<<nv)) continue; int nbit = bit | (1<<nv); chmax(dp[nbit][nv], dp[bit][v] + G[v][nv]); } } } long long res = 0; for (int v = 0; v < N; ++v) chmax(res, dp[(1<<N)-1][v]); return res; } int main() { int N; cin >> N; vector<vector<long long> > G(N, vector<long long>(N, 0)); G[0][1] = 1; G[1][0] = 1; for (int n = 2; n < N; ++n) { long long M = calc(G, n); for (int v = 0; v < n; ++v) G[v][n] = G[n][v] = (M+1) * w[v]; //cout << n << ": " << M << endl; } //cout << calc(G, N) << endl; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) cout << G[i][j] << " "; cout << endl; } }
#include<algorithm>/*{{{*/ #include<cctype> #include<cassert> #include<cmath> #include<cstdio> #include<cstring> #include<cstdlib> #include<ctime> #include<iostream> #include<map> #include<queue> #include<set> #include<vector> using namespace std; typedef long long lld; typedef long double lf; typedef unsigned long long uld; typedef pair<int,int> pii; #define fi first #define se second #define pb push_back #define mk make_pair #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define ROF(i,a,b) for(int i=(a);i>=(b);--i) namespace RA{ int r(int p){return 1ll*rand()*rand()%p;} int r(int L,int R){return r(R-L+1)+L;} } namespace IO{ char nc(){ static char bf[100000],*p1=bf,*p2=bf; return p1==p2&&(p2=(p1=bf)+ fread(bf,1,100000,stdin),p1==p2)?EOF:*p1++; } int rd(){ int res=0; char c=getchar(); while(!isdigit(c))c=getchar(); while(isdigit(c))res=res*10+c-'0',c=getchar(); return res; } }/*}}}*/ /******************heading******************/ #define int lld using namespace RA; const int N=11; int w[N][N]; int a[]={0,1,2,4,7,12,20,29,38,52,73}; int getmax(int x){ int p[N],res=0; FOR(i,1,x)p[i]=i; do{ int s=0; FOR(i,1,x-1)s+=w[p[i]][p[i+1]]; res=max(res,s); }while(next_permutation(p+1,p+x+1)); return res; } void make(int k){ int m=getmax(k-1); m++; FOR(i,1,k-1)w[i][k]=w[k][i]=a[i]*m; } int n; signed main(){ scanf("%lld",&n); FOR(i,2,n)make(i); FOR(i,1,n)FOR(j,1,n)printf("%lld%c",w[i][j]," \n"[j==n]); return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pi; typedef pair<pi, pi> pp; typedef pair<ll, ll> pl; const double EPS = 1e-9; const ll MOD = 1000000007; const int inf = 1 << 30; const ll linf = 1LL << 60; const double PI = 3.14159265358979323846; int n; ll num[10]; ll num2[10]; ll out[10][10]; ll hoge[10]; int main() { cin >> n; if (n == 2) { cout << 0 << " " << 1 << endl; cout << 1 << " " << 0 << endl; return 0; } num[0] = 1; for (int i = 1; i <= n-1; i++) { for (int j = num[i-1]+i;; j++) { bool ok = true; for (int k = 0; k < i; k++) { for (int l = k+1; l < i; l++) { if (num[k]+num[l] == j) { ok = false; break; } for (int z = 0; z < i; z++) { if (num[k]+num[l] == j+num[z]) { ok = false; break; } } } } if (ok) { num[i] = j; break; } } } num2[0] = 1; for (int i = 1; i <= n-1; i++) { for (int j = num2[i-1]+i;; j++) { bool ok = true; for (int k = 0; k < i; k++) { for (int l = k+1; l < i; l++) { for (int z = 0; z < i; z++) { if (num2[k]+num2[l] == j+num2[z]) { ok = false; break; } } } } if (ok) { num2[i] = j; break; } } } ll base = 1; ll m_c = 0; ll acc = 0; /* rep(i,n) cerr << num[i] << " "; cerr << endl; */ rep(i,n) { if (i == 0) { for (int j = 1; j < n; j++) { out[i][j] = num2[j-1]; } } else { ll c = 0; rep(j,n) { if (i == j) { continue; } if (out[j][i] != 0) continue; out[i][j] = base*num[c]; m_c = max(m_c,out[i][j]); c++; } } ll sum = 0; rep(j,n) hoge[j] = 0; for (int j = i; j >= 0; j--) { int ccnt = 0; for (int k = n-1; k > j; k--) { if (hoge[k] <= 1 && hoge[j] <= 1) { sum += out[j][k]; hoge[k]++; hoge[j]++; ccnt++; } if (ccnt == 2) break; } } base = sum+1; //if (c >= 2) base = base*(num[c-2]+num[c-1]+1); //else if (c == 1) base = base*(num[c-1]+1); //base = base*(num[4]+num[3]+1); //cerr << base << endl; } cerr << m_c << endl; rep(i,n) { rep(j,n) { out[i][j] = max(out[i][j], out[j][i]); cout << out[i][j]; if (j != n-1) cout << " "; else cout << endl; } } int hoge[10]; rep(i,10) hoge[i] = i; unordered_map<ll,int> checker; ll val = 0; do{ ll cnt = 0; rep(i,9) cnt += out[hoge[i]][hoge[i+1]]; if (checker[cnt] > 1) { cerr << "fail" << endl; break; } checker[cnt]++; val = max(cnt, val); } while(next_permutation(hoge, hoge+n)); cerr<< val << endl; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep1(i,n) for(int i=1;i<=(int)(n);i++) #define all(c) c.begin(),c.end() #define pb push_back #define fs first #define sc second #define chmin(x,y) x=min(x,y) #define chmax(x,y) x=max(x,y) using namespace std; template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){ return o<<"("<<p.fs<<","<<p.sc<<")"; } template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){ o<<"{"; for(const T& v:vc) o<<v<<","; o<<"}"; return o; } using ll = long long; template<class T> using V = vector<T>; template<class T> using VV = vector<vector<T>>; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); } #ifdef LOCAL #define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl #else #define show(x) true #endif template<class T> T rnd(T l,T r){ //[l,r) using D = uniform_int_distribution<T>; static random_device rd; static mt19937 gen(rd()); return D(l,r-1)(gen); } template<class T> T rnd(T n){ //[0,n) return rnd(0,n); } void test(){ cin.tie(0); ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !! cout << fixed << setprecision(20); V<ll> fib(70); fib[0] = 1, fib[1] = 2; for(int i=2;i<70;i++) fib[i] = fib[i-1] + fib[i-2] + 1; int N = 10; // V<int> r(N*(N-1)/2); rep(i,r.size()) r[i] = i; // random_shuffle(all(r)); VV<ll> a(N,V<ll>(N)); V<ll> magic = {1,45,1332,22344,181944,667800,1292400,1693440,1814400}; { auto getst = [&](){ V<ll> st; V<int> o(N); rep(j,N) o[j] = j; do{ ll sm = 0; rep(i,N-1){ int x = o[i], y = o[i+1]; if(x > y) swap(x,y); sm += a[x][y]; } st.pb(sm); }while(next_permutation(all(o))); sort(all(st)); st.erase(unique(all(st)),st.end()); return st; }; rep(i,N-2){ V<ll> st = getst(); ll X = (*st.rbegin() - *st.begin()) + 1; ll c = X; show(st.size()); V<ll> use; rep(j,N-1-i) use.pb(fib[j]*c); for(int j=i+1;j<N;j++){ a[i][j] = use[N-1-j]; // a[i][j] = use[j-(i+1)]; cout << a[i][j] << " "; } cout << endl; } } while(true){ ll fix; cin >> fix; a[7][8] = fix; V<int> o(N); rep(i,N) o[i] = i; set<ll> st; do{ if(o.front() > o.back()) continue; auto f = [&](int i,int j){ if(i>j) swap(i,j); return a[i][j]; }; ll sm = 0; rep(i,N-1){ sm += f(o[i],o[i+1]); } st.insert(sm); }while(next_permutation(all(o))); int num = 1; rep1(i,N) num *= i; num /= 2; show(*st.rbegin()); if((int)st.size() == num){ cout << "OK!" << endl; rep(i,N){ cout << "{"; rep(j,N){ cout << max(a[i][j],a[j][i])+1; if(j == N-1) cout <<"},"<<endl; else cout<<","; } } return; }else{ cout << "NG" << endl; } } } int main(){ VV<ll> a = { {1,89,55,34,21,13,8,5,3,2}, {89,1,7669,4687,2841,1705,995,569,285,143}, {55,7669,1,407221,246801,148081,86381,49361,24681,12341}, {34,4687,407221,1,13204801,7922881,4621681,2640961,1320481,660241}, {21,2841,246801,13204801,1,257499097,150207807,85833033,42916517,21458259}, {13,1705,148081,7922881,257499097,1,2928568987,1673467993,836733997,418366999}, {8,995,86381,4621681,150207807,2928568987,1,19193325393,9596662697,4798331349}, {5,569,49361,2640961,85833033,1673467993,19193325393,1,55000000001,30937585342}, {3,285,24681,1320481,42916517,836733997,9596662697,55000000001,1,1}, {2,143,12341,660241,21458259,418366999,4798331349,30937585342,1,1} }; // for(int N=2;N<=10;N++){ // V<int> o(N); rep(i,N) o[i] = i; // set<ll> st; // do{ // if(o.front() > o.back()) continue; // auto f = [&](int i,int j){ // if(i>j) swap(i,j); // return a[i][j]; // }; // ll sm = 0; // rep(i,N-1){ // sm += f(o[i],o[i+1]); // } // st.insert(sm); // }while(next_permutation(all(o))); // int num = 1; rep1(i,N) num *= i; num /= 2; // show(*st.rbegin()); // if((int)st.size() == num){ // cout << "OK!" << endl; // } // } int N; cin >> N; rep(i,N){ a[i][i] = 0; rep(j,N) cout << a[i][j] << " "; cout << endl; } }
#include<iostream> #define REP(i,j,k) for(int i=j;i<=k;++i) int A[9]={1,2,4,7,12,20,29,38,52}; long long B[11][11],N; int main(){ std::cin>>N; long long MX=1; REP(i,2,N){REP(j,1,i-1){B[i][j]=B[j][i]=MX*A[j - 1];}MX+=B[i][i-1]+B[i][i-2]-B[i-1][i-2];} REP(i,1,N) REP(j,1,N) std::cout << B[i][j] << " \n"[j == N]; }
#include <bits/stdc++.h> #define fi first #define se second #define rep(i,n) for(int i = 0; i < (n); ++i) #define rrep(i,n) for(int i = 1; i <= (n); ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) #define srep(i,s,t) for (int i = s; i < t; ++i) #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define maxs(x,y) (x = max(x,y)) #define mins(x,y) (x = min(x,y)) #define limit(x,l,r) max(l,min(x,r)) #define lims(x,l,r) (x = max(l,min(x,r))) #define isin(x,l,r) ((l) <= (x) && (x) < (r)) #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define pcnt __builtin_popcountll #define uni(x) x.erase(unique(rng(x)),x.end()) #define snuke srand((unsigned)clock()+(unsigned)time(NULL)); #define show(x) cout<<#x<<" = "<<x<<endl; #define PQ(T) priority_queue<T,v(T),greater<T> > #define bn(x) ((1<<x)-1) #define dup(x,y) (((x)+(y)-1)/(y)) #define newline puts("") #define v(T) vector<T> #define vv(T) v(v(T)) using namespace std; typedef long long int ll; typedef unsigned uint; typedef unsigned long long ull; typedef pair<int,int> P; typedef tuple<int,int,int> T; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; typedef vector<T> vt; inline int getInt() { int x; scanf("%d",&x); return x;} template<typename T>inline istream& operator>>(istream&i,v(T)&v) {rep(j,sz(v))i>>v[j];return i;} template<typename T>string join(const v(T)&v) {stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);} template<typename T>inline ostream& operator<<(ostream&o,const v(T)&v) {if(sz(v))o<<join(v);return o;} template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v) {return i>>v.fi>>v.se;} template<typename T1,typename T2>inline ostream& operator<<(ostream&o,const pair<T1,T2>&v) {return o<<v.fi<<","<<v.se;} template<typename T>inline ll suma(const v(T)& a) { ll res(0); for (auto&& x : a) res += x; return res;} const double eps = 1e-10; const ll LINF = 1001002003004005006ll; const int INF = 1001001001; #define dame { puts("-1"); return 0;} #define yn {puts("Yes");}else{puts("No");} const int MX = 200005; typedef v(vl) vvl; int main() { int n; scanf("%d",&n); vvl g(n,vl(n)); ll d = 1; srep(x,1,n) { vi a; set<int> s; int l = 1; rep(m,x) { while (s.count(l)) ++l; a.pb(l); s.insert(l); rep(i,m) { s.insert(l+a[i]); rep(j,m) s.insert(l+a[i]-a[j]); } } rep(i,x) g[i][x] = g[x][i] = a[i]*d; while (sz(a) < 2) a.pb(1); reverse(rng(a)); d *= a[0]+a[1]; } rep(i,n) cout<<g[i]<<endl; return 0; set<ll> s; vi a(n); rep(i,n) a[i] = i; do { if (a[0] > a.back()) continue; ll tot = 0; rep(i,n-1) tot += g[a[i]][a[i+1]]; if (tot >= 1e11) cout<<tot<<endl; if (s.count(tot)) cout<<a<<endl; s.insert(tot); } while (next_permutation(rng(a))); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; #define fi first #define se second #define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++) #define rep(i,n) repl(i,0,n) #define all(x) (x).begin(),(x).end() #define dbg(x) cout<<#x"="<<x<<endl #define mmax(x,y) (x>y?x:y) #define mmin(x,y) (x<y?x:y) #define maxch(x,y) x=mmax(x,y) #define minch(x,y) x=mmin(x,y) #define uni(x) x.erase(unique(all(x)),x.end()) #define exist(x,y) (find(all(x),y)!=x.end()) #define bcnt __builtin_popcountll #define INF 1e16 #define mod 1000000007 vector<ll> fib; vector<vector<ll>> dfs(ll n){ if(n==1){ return vector<vector<ll>>(1,vector<ll>(1,0)); } vector<vector<ll>> pre=dfs(n-1); vector<vector<ll>> res(n,vector<ll>(n,0)); vector<ll> vs; rep(i,n-1)rep(j,n-1)res[i][j]=pre[i][j]; rep(i,n-1)vs.push_back(i); ll mx=-1; do{ ll sum=0; rep(i,n-2){ sum+=res[vs[i]][vs[i+1]]; } maxch(mx,sum); }while(next_permutation(all(vs))); mx++; rep(i,n-1){ res[i][n-1]=res[n-1][i]=fib[i]*mx; } return res; } int main(){ cin.tie(0); ios::sync_with_stdio(false); fib.resize(10); fib[0]=1; fib[1]=2; repl(i,2,10){ ll mn=-1; repl(j,fib[i-1]+1,10000){ map<ll,bool> used; bool ok=true; rep(k,i){ used[fib[k]]=true; rep(l,k)used[fib[k]+fib[l]]=true; } if(used[j])ok=false; used[j]=true; rep(k,i){ if(used[fib[k]+j])ok=false; used[fib[k]+j]=true; } if(ok){ mn=j; break; } } fib[i]=mn; } ll N; cin>>N; vector<vector<ll>> res=dfs(N); rep(i,N){ rep(j,N){ cout<<res[i][j]<<" "; } cout<<endl; } return 0; }
// And in the end, the love you take is equal to the love you make. // Paul McCartney (the Beatles). The End. Abbey Road. #include <bits/stdc++.h> using namespace std; using LL = long long; namespace _buff { const size_t BUFF = 1 << 19; char ibuf[BUFF], *ib = ibuf, *ie = ibuf; char getc() { if (ib == ie) { ib = ibuf; ie = ibuf + fread(ibuf, 1, BUFF, stdin); } return ib == ie ? -1 : *ib++; } } LL read() { using namespace _buff; LL ret = 0; bool pos = true; char c = getc(); for (; (c < '0' || c > '9') && c != '-'; c = getc()) { assert(~c); } if (c == '-') { pos = false; c = getc(); } for (; c >= '0' && c <= '9'; c = getc()) { ret = (ret << 3) + (ret << 1) + (c ^ 48); } return pos ? ret : -ret; } int main() { const vector<int> arr {1, 2, 4, 7, 12, 20, 29, 38, 52, 73}; int n = read(); vector<vector<LL> > w(n, vector<LL>(n)); auto get_max = [&](int n) { vector<int> vc(n); iota(vc.begin(), vc.end(), 0); LL ret = 0; do { LL cur = 0; for (unsigned i = 1; i < vc.size(); ++i) { cur += w[vc[i - 1]][vc[i]]; } ret = max(ret, cur); } while (next_permutation(vc.begin(), vc.end())); return ret; }; LL m = 0; for (int i = 1; i < n; ++i) { for (int j = 0; j < i; ++j) { w[i][j] = w[j][i] = (m + 1) * arr[j]; } m = get_max(i + 1); } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cout << w[i][j] << "\n "[j < n - 1]; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define forn(i, n) for (int i = 0; i < (int)(n); ++i) typedef long long LL; typedef pair<int, int> PII; int n; int a[15]; int b[15]; int c[15][15]; int need; void go(int ind) { set<int> bad; forn(i, ind) { bad.insert(a[i]); forn(j, i) { bad.insert(a[i] + a[j]); } } int ways = 0; int beg = 1; if (ind > 0) { beg = a[ind - 1]; } for (int j = beg; ; ++j) { if (bad.count(j)) { continue; } bool ok = true; forn(i, ind) if (bad.count(a[i] + j)) { ok = false; break; } if (!ok) { continue; } ++ways; a[ind] = j; if (ind == need - 1) { if (a[ind] + a[ind - 1] < b[ind] + b[ind - 1]) { memcpy(b, a, sizeof b); } } else { go(ind + 1); } if (ways == 3) { break; } } } LL ans[10][10]; int main() { for (need = 1; need <= 9; ++need) { forn(i, need) b[i] = 1234; go(0); forn(i, need) c[need][i] = b[i]; int cur = 1 + b[need - 1]; if (need > 1) { cur += b[need - 2]; } } cin >> n; LL mul = 1; for (int i = n - 1; i >= 1; --i) { forn(j, i) { ans[i][j] = ans[j][i] = c[i][j] * mul; } if (i > 1) { mul *= c[i][i - 2] + c[i][i - 1] + 1; } } forn(i, n) { forn(j, n) cout << ans[i][j] << ' '; cout << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<double, double> pdd; typedef pair<pii, int> ppiii; int scale[10]{1,2,4,7,12,20,29,38,52,73}; ll dp[10][10]; void dfs(int curr, int mask, int v, ll currW, ll& ret) { if(__builtin_popcount(mask) == v) { ret = max(ret, currW); return; } for(int i = 0; i < v; i++) { if((mask&(1<<i)) == 0) { dfs(i, mask | (1<<i), v, currW + dp[curr][i], ret); } } } void solve() { int n; cin >> n; dp[0][1] = 1; dp[1][0] = 1; for(int v = 2; v < n; v++) { ll ret = 0; for(int i = 0; i < v; i++) { dfs(i, 1<<i, v, 0, ret); } for(int k = 0; k < v; k++) { dp[v][k] = (ret+1) * scale[k]; dp[k][v] = (ret+1) * scale[k]; } } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { cout << dp[i][j]; if(j == n-1) cout << "\n"; else cout << " "; } } } void casesolve() { int t; cin >> t; for(int i = 1; i <= t; i++) { cout << "Case #" << i << ": "; solve(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); // casesolve(); }
#include<bits/stdc++.h> using namespace std; long long g[15][15],ans,s; int i,j,vis[15],n,f[15]={0,1,2,4,7,12,20,29,38,52,73}; void dfs(int l,int i,int n,long long s) { int j; if(i>n) { ans=max(ans,s); return; } for(j=1;j<=n;++j) if(vis[j]==0) { vis[j]=1; dfs(j,i+1,n,s+g[l][j]); vis[j]=0; } } int main() { scanf("%d",&n); for(i=2;i<=n;++i) { dfs(0,1,i,0); s=1; for(j=1;j<i;++j) g[j][i]=g[i][j]=(ans+1)*f[j]; } for(i=1;i<=n;++i) { for(j=1;j<=n;++j) cout<<g[i][j]<<' '; cout<<endl; } }
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int w[11] = {0, 1, 2, 4, 7, 12, 20, 29, 38, 52, 73}; template <typename Tp> inline void getint(Tp &num){ register int ch, neg = 0; while(!isdigit(ch = getchar())) if(ch == '-') neg = 1; num = ch & 15; while(isdigit(ch = getchar())) num = num * 10 + (ch & 15); if(neg) num = -num; } int N, p[11]; ll ed[11][11], M = 0; int main(){ getint(N); for(register int i = 2; i <= N; i++){ for(register int j = 1; j < i; j++) ed[i][j] = ed[j][i] = (M + 1) * w[j]; if(i == N) break; for(register int j = 1; j <= i; j++) p[j] = j; do { register ll len = 0; for(register int j = 1; j < i; j++) len += ed[p[j]][p[j + 1]]; M = max(M, len); } while(next_permutation(p + 1, p + i + 1)); } for(register int i = 1; i <= N; i++, puts("")) for(register int j = 1; j <= N; j++) printf("%lld ", ed[i][j]); return 0; }
#include<bits/stdc++.h> #define pa pair<int,int> #define CLR(a,x) memset(a,x,sizeof(a)) #define MP make_pair #define fi first #define se second using namespace std; typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef long double ld; const int maxn=12; inline char gc(){ return getchar(); static const int maxs=1<<16;static char buf[maxs],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,maxs,stdin),p1==p2)?EOF:*p1++; } inline ll rd(){ ll x=0;char c=gc();bool neg=0; while(c<'0'||c>'9'){if(c=='-') neg=1;c=gc();} while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+c-'0',c=gc(); return neg?(~x+1):x; } int f[maxn]={0,1,2,3,6,10,17,30,48,79},N,a[maxn]; ll ans[maxn][maxn]; map<ll,int> mp; int main(){ //freopen("","r",stdin); N=rd(); ll now=1; for(int i=1;i<=N;i++){ int n=0; for(int j=1;j<=N&&n<N;j++){ if(ans[i][j]||i==j) continue; ans[i][j]=ans[j][i]=f[++n]*now; } now*=f[n]+f[n-1]+1; } for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) printf("%lld%c",ans[i][j]," \n"[j==N]); return 0; for(int i=1;i<=N;i++) a[i]=i; ll ma=0; do{ ll now=0; for(int i=1;i<N;i++) now+=ans[a[i]][a[i+1]]; ma=max(ma,now); // for(int i=1;i<=N;i++) printf("%d ",a[i]); // printf(":%lld\n",now); assert(mp[now]<=1); // assert(now<=1e11); mp[now]+=1; }while(next_permutation(a+1,a+N+1)); printf("%lld\n",ma); return 0; }
#include<bits/stdc++.h> using namespace std; int A[9] = {1,2,4,7,12,20,29,38,52}; long long arr[11][11] , N; int main(){ cin >> N; long long MX = 1; for(int i = 2 ; i <= N ; ++i){ for(int j = 1 ; j < i ; ++j) arr[i][j] = arr[j][i] = MX * A[j - 1]; MX += arr[i][i - 1] + arr[i][i - 2] - arr[i - 1][i - 2]; } for(int i = 1 ; i <= N ; ++i) for(int j = 1 ; j <= N ; ++j) cout << arr[i][j] << " \n"[j == N]; return 0; }
#include<bits/stdc++.h> #define LL long long #define db double using namespace std; const int N=500+10,M=100+10; int rd() { int x=0,w=1;char ch=0; while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+(ch^48);ch=getchar();} return x*w; } LL f[12],sq[12],an[12][12],nx; int n,vs[233]; bool ck(int j,int i) { for(int k=0;k<i;++k) if(vs[sq[k]+j]) return 0; return 1; } int p[12],v[12],cn; void dfs(int o,LL ns) { if(o>n){nx=max(nx,ns);return;} for(int i=1;i<=n;++i) if(!v[i]) { v[i]=1,p[o]=i; dfs(o+1,ns+an[p[o-1]][p[o]]); v[i]=0; } } int main() { n=rd(); sq[1]=1,vs[1]=1; for(int i=2,j=2;i<=n;++i,++j) { while(!ck(j,i)) ++j; sq[i]=j; for(int k=0;k<i;++k) vs[sq[k]+sq[i]]=1; } memcpy(f,sq,sizeof(sq)); for(int i=2;i<=n;++i) { for(int j=1;j<i;++j) an[i][j]=an[j][i]=sq[j]; nx=0; for(int j=1;j<=n;++j) v[j]=1,p[1]=j,dfs(2,0),v[j]=0; for(int j=1;j<=n;++j) sq[j]=f[j]*(nx+1); } for(int i=1;i<=n;++i) { for(int j=1;j<=n;++j) printf("%lld ",an[i][j]); puts(""); } return 0; }
#include<algorithm> #include<cstring> #include<cctype> #include<cstdio> #define rep(i,x,y) for(int i=x; i<=y; ++i) using namespace std; const int N=11,dis[]={0,1,2,4,7,12,20,29,38,52,73}; int n,p[N],id[N][N],tot; typedef long long LL; LL mx,ans[N][N]; bool vis[N]; void dfs(int x,int n) { if(x>n) { LL tot=0; rep(i,1,n-1) tot+=ans[p[i]][p[i+1]]; mx=max(mx,tot); return; } rep(i,1,n) if(!vis[i]) vis[i]=1,p[x]=i,dfs(x+1,n),vis[i]=0; } void solve() { ans[1][2]=ans[2][1]=1; rep(i,3,n) { mx=0; dfs(1,i-1); rep(j,1,i-1) ans[i][j]=ans[j][i]=(mx+1)*dis[j]; } rep(i,1,n) { rep(j,1,n) printf("%lld ",ans[i][j]); puts(""); } } int main() { scanf("%d",&n); if(n<10) { rep(i,1,n) rep(j,1,n) if(i<j) id[i][j]=tot++; rep(i,1,n) rep(j,1,n) if(i>j) id[i][j]=id[j][i]; rep(i,1,n) { rep(j,1,n) if(i==j) printf("0 "); else printf("%lld ",1ll<<id[i][j]); puts(""); } return 0; } solve(); return 0; }
#include<bits/stdc++.h> using namespace std; const int N=15; typedef long long ll; int gi() { int x=0,o=1;char ch=getchar(); while(!isdigit(ch)&&ch!='-') ch=getchar(); if(ch=='-') o=-1,ch=getchar(); while(isdigit(ch)) x=x*10+ch-'0',ch=getchar(); return x*o; } const int a[]={0,1,2,4,7,12,20,29,38,52,73}; int n,p[N]; ll G[N][N]; int main() { cin>>n;ll m=0; for(int i=2;i<=n;i++) { for(int j=1;j<i;j++) G[i][j]=G[j][i]=(m+1)*a[j]; if(i==n) break; for(int j=1;j<=i;j++) p[j]=j; do { ll sum=0; for(int j=1;j<i;j++) sum+=G[p[j]][p[j+1]]; m=max(m,sum); } while(next_permutation(p+1,p+i+1)); } for(int i=1;i<=n;i++,cout<<'\n') for(int j=1;j<=n;j++) cout<<G[i][j]<<' '; return 0; }
#include <bits/stdc++.h> using std::cerr;using std::cin;using std::cout;using std::abs;using std::min;using std::max;using std::swap;using std::map;using std::unordered_map;using std::unordered_set;using std::bitset;using std::pair;using std::set;using std::string;using std::vector;using std::sort;using ll=long long;using uint=unsigned int;using pii=pair<int,int>;using pll=pair<ll,ll>;using ull = unsigned long long;using ld=long double;template<typename T>void _dbg(const char*s,T h){cerr<<s<<" = "<<h<<"\n";}template<typename T,typename...Ts>void _dbg(const char*s,T h,Ts...t){int b=0;while(((b+=*s=='(')-=*s==')')!=0||*s!=',')cerr<<*s++;cerr<<" = "<<h<<",";_dbg(s+1,t...);}// break continue pop_back 998244353 #define int ll #define pii pll #define f first #define s second #define pb emplace_back #define forn(i,n) for(int i=0;i<(n);++i) struct init{init(){cin.tie(0);std::iostream::sync_with_stdio(0);cout<<std::fixed<<std::setprecision(10);cerr<<std::fixed<<std::setprecision(5);}~init(){ #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__,__VA_ARGS__) cerr<<"Time elapsed: "<<(double)clock()/CLOCKS_PER_SEC<<"s.\n"; #else #define dbg(...) #endif }}init;template<typename T,typename U>void upx(T&x,U y){if(x<y)x=y;}template<typename T,typename U>void upn(T&x,U y){if(x>y)x=y;} const int N=11; int g[N][N]; int val[N]; int32_t main() { val[0]=1; int n; cin>>n; for(int i=1;i<n;++i){ for(int pot=val[i-1]+1;;++pot){ set<int> s; int cnt=1; s.insert(pot); forn(x,i){forn(y,x){cnt++;s.insert(val[x]+val[y]);}cnt++;s.insert(val[x]+pot);s.insert(val[x]);cnt++;} if(s.size()==cnt){ val[i]=pot; break; } } } memset(g,-1,sizeof g); for(int i=1;i<=n;++i){ vector<int> v(i-1); int mx=0; iota(v.begin(), v.end(),1); do{ int c=0; for(int j=1;j<v.size();++j)c+=g[v[j-1]][v[j]]; upx(mx,c); }while(next_permutation(v.begin(), v.end())); mx++; for(int j=1;j<i;++j)g[j][i]=g[i][j]=mx*val[j-1]; g[i][i]=0; } for(int i=1;i<=n;++i){for(int j=1;j<=n;++j)cout<<g[i][j]<<' ';cout<<'\n';} return 0; }
//////////////////////////////////////////// /// /// /// Template ver. 1 rapel /// /// Fear is Temporary, Regret is Forever /// /// Must Try and Get AC /// /// /// //////////////////////////////////////////// #include <stdio.h> #include <iostream> #include <sstream> #include <iomanip> #include <algorithm> #include <stdlib.h> #include <string.h> #include <string> #include <utility> #include <math.h> #include <complex> #include <assert.h> #include <time.h> //#include <chrono> //#include <random> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <list> #include <bitset> #define FI first #define SE second #define MP make_pair #define PB push_back #define PF push_front #define POB pop_back #define POF pop_front #define endl '\n' using namespace std; typedef long long LL; typedef unsigned long long ULL; void desperate_optimization(int precision){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(precision); } const int N = 10; LL arr[N + 5][N + 5]; set<int> st; set<int> nx; vector<int> vec; void hora(int tc) { int n; cin>>n; st.insert({1, 2, 4, 7, 12, 20, 29, 38, 52, 73}); // for(int i = 1;i < n;i++) { // nx.clear(); // for(auto x: st) { // nx.insert(x); // for(auto y: st) { // if(x == y) continue; // nx.insert(x + y); // } // } // int mx = -1; // int now = 0; // for(auto x: nx) { // if(x != now + 1) { // mx = now + 1; // break; // } // now = x; // } // if(mx == -1) mx = now + 1; // st.insert(mx); // } // for(auto x: st) cout<<x<<endl; LL maxi = 0; vec.PB(0); for(int i = 1;i < n;i++) { int j = 0; for(auto x: st) { if(i == j) break; arr[i][j] = arr[j][i] = (maxi + 1) * x; j++; } vec.PB(i); sort(vec.begin(), vec.end()); do{ LL tots = 0; for(int i = 1;i < vec.size();i++) tots += arr[vec[i - 1]][vec[i]]; maxi = max(maxi, tots); }while(next_permutation(vec.begin(), vec.end())); } for(int i = 0;i < n;i++) { for(int j = 0;j < n;j++) { if(j) cout<<" "; cout<<arr[i][j]; } cout<<endl; } } int main(){ desperate_optimization(10); int ntc = 1; //cin>>ntc; for(int tc = 1;tc <= ntc;tc++) hora(tc); return 0; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; using ll = long long; const ll MOD = 1e9 + 7; const int N = 10; bool visited[N]; vector<ll> vals; ll cost[N][N]; int n, j; void dfs(int i, ll s) { visited[i] = true; int outs = 0; for (int t = 0; t < n; ++t) { if (! visited[t]) { ++outs; dfs(t, s + cost[i][t]); } } if (outs == 0 && i >= j) vals.push_back(s); visited[i] = false; } ll works() { for (j = 0; j < n; ++j) dfs(j, 0); sort(vals.begin(), vals.end()); ll ans = vals.back(); for (int i = 0; i+1 < vals.size(); ++i) { if (vals[i] == vals[i+1]) return -1; } vals.clear(); return ans; } int main() { vector<vector<int>> xr = { {1}, {1,2}, {1,2,4}, {1,2,4,7}, {1,2,4,7,12}, {1,2,4,8,13,18}, {1,2,4,8,14,19,24}, {1,2,4,8,15,24,29,34}, {1,2,4,8,15,24,29,34,46} }; int m; cin >> m; for (int i = 1; i < m; ++i) { n = i; ll b = works() + 1; if (b == -1) return 1; for (j = 0; j < n; ++j) { cost[i][j] = b * xr[i-1][j]; cost[j][i] = cost[i][j]; } } n = m; ll res = works(); if (res == -1) return 1; cerr << res << '\n'; for (int i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { cout << cost[i][j] << ' '; } cout << '\n'; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 5; inline int gi() { char c = getchar(); while (c < '0' || c > '9') c = getchar(); int sum = 0; while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar(); return sum; } const int w[2][9] = {1, 2, 3, 5, 8, 13, 21, 30, 39, 1, 2, 4, 7, 12, 20, 29, 38, 52}; int n, p[15]; ll e[15][15]; ll calc(int n) { ll Max = 0; for (int i = 0; i < n; ++i) p[i] = i; do { ll sum = 0; for (int i = 0; i < n - 1; ++i) sum += e[p[i]][p[i + 1]]; Max = max(Max, sum); } while (next_permutation(p, p + n)); return Max; } int main() { n = gi(); ll M = 0; for (int i = 1; i < n; ++i) { for (int j = 0; j < i; ++j) e[i][j] = e[j][i] = w[1][j] * (M + 1); M = calc(i + 1); //printf("%lld\n", M); } for (int i = 0; i < n; ++i, puts("")) for (int j = 0; j < n; ++j) printf("%lld ", e[i][j]); return 0; }
#include <bits/stdc++.h> using namespace std; #define TRACE(x) x #define WATCH(x) TRACE(cout << #x" = " << x << endl) #define WATCHR(a, b) TRACE(for (auto it=a; it!=b;) cout << *(it++) << " "; cout << endl) #define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());}) #define sz(x) int((x).size()) #define all(x) (x).begin(), (x).end() using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vs = vector<string>; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); int run = 0; vi seen(100); bool find(vi& took, int lim, int inx) { if (inx == sz(took)) { ++run; for (int i = 0; i < sz(took); i++) { if (seen[took[i]] == run) return false; else seen[took[i]] = run; for (int j = 0; j < i; j++) { if (seen[took[i] + took[j]] == run) return false; else seen[took[i] + took[j]] = run; } } return true; } int low = inx ? took[inx - 1] : 0; for (took[inx] = low + 1; took[inx] + low < lim; took[inx]++) { if (find(took, lim, inx + 1)) return true; } return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); vi lim = { 0, 2, 4, 7, 12, 20, 32, 44, 64, 81 }; int N; cin >> N; vvll dist(N, vll(N)); ll mv = 1; for (int n = 1; n < N; n++) { vi took(n); find(took, lim[n], 0); for (int i = 0; i < n; i++) dist[i][n] = mv * took[i]; mv *= lim[n]; } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (j) cout << " "; cout << dist[min(i, j)][max(i, j)]; } cout << "\n"; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; inline int read() { int out=0,fh=1; char jp=getchar(); while ((jp>'9'||jp<'0')&&jp!='-') jp=getchar(); if (jp=='-') fh=-1,jp=getchar(); while (jp>='0'&&jp<='9') out=out*10+jp-'0',jp=getchar(); return out*fh; } const int MAXN=11; const int a[]={0,1,2,4,7,12,20,29,38,52}; int n,p[MAXN]; ll w[MAXN][MAXN],M; void GetM(int x) { M=0; for(int i=1;i<=x;++i) p[i]=i; do { ll res=0; for(int i=1;i<x;++i) res+=w[p[i]][p[i+1]]; M=max(M,res); }while(next_permutation(p+1,p+1+x)); } void solve(int x) { for(int i=1;i<x;++i) w[i][x]=w[x][i]=(M+1)*a[i]; } int main() { n=read(); w[1][2]=w[2][1]=1; M=1; for(int i=3;i<=n;++i) { solve(i); GetM(i); } for(int i=1;i<=n;++i) { for(int j=1;j<=n;++j) printf("%lld ",w[i][j]); puts(""); } return 0; }
#include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<string.h> #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define NDEBUG #define eprintf(...) do {} while (0) #endif #include<cassert> using namespace std; typedef long long LL; typedef vector<int> VI; #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i) template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; } template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; } template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) { for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); } putchar('\n'); } int N; LL W[12][12]; void MAIN() { vector<int> v; v.push_back(1); v.push_back(2); while (v.size() <= 10) { int m = v.rbegin()[0] + v.rbegin()[1]; v.push_back(m+1); } //#ifdef LOCAL // rprintf("%d", v.begin(), v.end()); //#endif scanf("%d", &N); W[0][1] = W[1][0] = 0; for (int s=2; s<N; s++) { REP (i, s) REP (j, s) W[i][j] *= v[s]-1; REP (i, s) W[i][s] = W[s][i] = v[s-1-i]; } REP (i, N) REP (j, N) if (i != j) W[i][j]++; REP (i, N) rprintf("%lld", W[i], W[i]+N); } int main() { int TC = 1; // scanf("%d", &TC); REP (tc, TC) MAIN(); return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define ui unsigned int #define ull unsigned long long #define db long double #define pii pair<int,int> #define pli pair<ll,int> #define X first #define Y second #define mp make_pair #define pb push_back #define vi vector<int> #define vii vector<vi> #define lb lower_bound #define rep(i,a,b) for(int i=(a);i<=(b);++i) #define per(i,b,a) for(int i=(b);i>=(a);--i) #define rep0(i,a,b) for(int i=(a);i<(b);++i) #define fore(i,a) for(int i=0;i<a.size();++i) #define gc() getchar() #define ls x<<1,l,m #define rs x<<1|1,m+1,r inline ll rd() { ll x=0,w=1;char c=gc();while(!isdigit(c)&&c!='-')c=gc(); if(c=='-')c=gc(),w=-1;while(isdigit(c))x=x*10+c-48,c=gc();return x*w; } const int a[11]={0,1,2,4,7,12,20,29,38,52,73}; int n,p[11];ll m,e[11][11]; int main() { n=rd();m=0; rep(i,2,n) { rep(j,1,i-1)e[i][j]=e[j][i]=(m+1)*a[j]; if(i==n)break; m=0;rep(j,1,i)p[j]=j; do { ll s=0;rep(j,1,i-1)s+=e[p[j]][p[j+1]];m=max(m,s); }while(next_permutation(p+1,p+i+1)); } rep(i,1,n) { rep(j,1,n)printf("%lld ",e[i][j]); puts(""); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll=long long; ll w[12][12]; void solve() { int n; cin >> n; vector<int> a = {1, 2, 4, 7, 12, 20, 29, 38, 52, 73}; memset(w, 0, sizeof w); w[1][0] = w[0][1] = 1; ll M = 1; auto upd = [&](int x){ vector<int> v(x); iota(v.begin(), v.end(), 0); ll mx = 0; do { ll sum = 0; for (int i = 1; i < x; i++) { sum += w[v[i-1]][v[i]]; } mx = max(mx, sum); } while (next_permutation(v.begin(), v.end())); M = mx; }; for (int i = 2; i < n; i++) { for (int j = 0; j < i; j++) { w[i][j] = w[j][i] = (M+1) * a[j]; } upd(i+1); //cout << M << ' '; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << w[i][j] << ' '; } cout << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); cout << endl; }
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=(a);i<=(b);++i) using namespace std; typedef long long LL; const int f[]={0,1,2,4,7,12,20,29,38,52,73}; int n,now;LL mx,w[11][11];bool vis[11]; void dfs(int k1,LL k2,int k3){ if(k1>now){ if(k2>mx)mx=k2; return; } rep(i,1,n)if(!vis[i]&&w[k1][i]){ vis[i]=1; dfs(k1+1,k2+w[k3][i],i); vis[i]=0; } } int main(){ scanf("%d",&n); rep(i,1,n){ rep(j,1,i-1)w[i][j]=w[j][i]=1LL*f[j]*(mx+1); now=i; rep(j,1,i)dfs(j,0,0); } rep(i,1,n){rep(j,1,n)printf("%lld ",w[i][j]),assert(w[i][j]<=1e12);puts("");} return 0; }
#include<bits/stdc++.h> using namespace std; const int val[]={0,1,2,4,7,12,20,29,38,52,73}; int n,p[20];long long mxv,ans[20][20]; int main() { cin>>n; for(int i=2;i<=n;i++) { mxv=0; for(int j=1;j<i;j++) p[j]=j; do{ long long s=0; for(int j=1;j<i-1;j++) s+=ans[p[j]][p[j+1]]; mxv=max(mxv,s); }while(next_permutation(p+1,p+i)); for(int j=1;j<i;j++) ans[i][j]=ans[j][i]=(mxv+1)*val[j]; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) printf("%lld ",ans[i][j]); putchar('\n'); } }
// Solution #1 : Max path length = 83 907 014 282 #include <iostream> #include <vector> #include <algorithm> using namespace std; long long N, a[11][11], r = 1, e[10] = { 1, 2, 4, 7, 12, 20, 29, 38, 52 }; long long solve() { vector<long long>E, F; for (int i = 0; i < N; i++) E.push_back(i); do { long long s = 0; for (int i = 0; i < N - 1; i++) { s += a[E[i]][E[i + 1]]; } F.push_back(s); } while (next_permutation(E.begin(), E.end())); sort(F.begin(), F.end()); F.erase(unique(F.begin(), F.end()), F.end()); long long LIM = 1; for (int i = 1; i <= N; i++) LIM *= i; LIM /= 2; //cout << F.size() << " " << LIM << endl; if (F.size() == LIM) return F[F.size() - 1]; return (1LL << 60); } int main() { cin >> N; a[0][1] = 1; a[1][0] = 1; a[0][2] = 2; a[2][0] = 2; a[1][2] = 3; a[2][1] = 3; for (int i = 3; i < N; i++) { long long NN = N; N = i; long long V = solve(); N = NN; r = V + 1; for (int j = 0; j < i; j++) { long long cost = r * e[j]; a[i][j] = cost; a[j][i] = cost; } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (j) cout << " "; cout << a[i][j]; } cout << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long int n,p[12],a[11]={0,1,2,4,7,12,20,29,38,52,73}; int g[12][12]; signed main(){ cin>>n; for(int i=2;i<=n;++i){ int mx=0,tmp=0; for(int j=1;j<i;++j) p[j]=j; do{ tmp=0; for(int j=1;j<i-1;++j) tmp+=g[p[j]][p[j+1]]; mx=max(mx,tmp); }while(next_permutation(p+1,p+i)); for(int j=1;j<i;++j) g[i][j]=g[j][i]=(mx+1)*a[j]; } for(int i=1;i<=n;++i,puts("")) for(int j=1;j<=n;++j) cout<<g[i][j]<<" "; return 0; }
// Copyright: lzt #include<stdio.h> #include<cstring> #include<cstdlib> #include<algorithm> #include<vector> #include<map> #include<set> #include<cmath> #include<iostream> #include<queue> #include<string> #include<ctime> using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef long double ld; typedef unsigned long long ull; typedef pair<long long,long long> pll; #define fi first #define se second #define pb push_back #define mp make_pair #define rep(i,j,k) for(register int i=(int)(j);i<=(int)(k);i++) #define rrep(i,j,k) for(register int i=(int)(j);i>=(int)(k);i--) #define Debug(...) fprintf(stderr, __VA_ARGS__) ll read(){ ll x=0,f=1;char c=getchar(); while(c<'0' || c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0' && c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } int a[] = {0, 1, 2, 4, 7, 12, 20, 29, 38, 52, 73}; ll w[15][15]; void work() { int n = read(); w[1][2] = w[2][1] = 1; rep(i, 3, n) { ll s = 0; int p[15]; rep(x, 1, i - 1) p[x] = x; do { ll nw = 0; rep(x, 1, i - 2) nw += w[p[x]][p[x + 1]]; s = max(s, nw); } while (next_permutation(p + 1, p + i)); s++; rep(x, 1, i - 1) w[x][i] = w[i][x] = s * a[x]; } rep(i, 1, n) { rep(j, 1, n) printf("%lld ", w[i][j]); puts(""); } } int main(){ #ifdef LZT freopen("in","r",stdin); #endif work(); #ifdef LZT Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC); #endif }
#include<bits/stdc++.h> using namespace std; typedef long long LL; const LL a[10]={0,1,2,4,7,12,20,29,38,52}; LL n,f[10]; int main(){ scanf("%lld",&n); f[2]=1; for (LL i=3;i<n;++i) f[i]=f[i-1]*(a[i-1]+a[i-2]+1); for (LL i=1;i<=n;++i){ for (LL j=1;j<=n;++j){ if (i==j) printf("%lld ",0LL); else printf("%lld ",f[max(i,j)-1]*a[min(i,j)]+1); } puts(""); } return 0; }
#include <cstdio> #include <cstdlib> #include <algorithm> #include <vector> #include <cstring> #include <queue> #include <set> #include <map> #include <functional> #include <cmath> #include <cassert> #include <string> #define SIZE 12 #define MX 205 using namespace std; typedef long long int ll; typedef pair <int,int> P; ll ans[SIZE][SIZE]; ll fib[SIZE]; bool use[MX]; void make() { fib[0]=1; fib[1]=2; for(int i=2;i<SIZE;i++) { for(int j=0;j<i;j++) { use[fib[j]]=true; for(int k=j+1;k<i;k++) use[fib[j]+fib[k]]=true; } fib[i]=1; while(1) { bool up=!use[fib[i]]; for(int j=0;j<i;j++) if(use[fib[j]+fib[i]]) up=false; if(up) break; fib[i]++; } } ll mul=1; ans[0][0]=0; for(int i=1;i<SIZE;i++) { ans[i][i]=0; for(int j=0;j<i;j++) ans[i][j]=ans[j][i]=fib[j]*mul; if(i==1) mul*=2LL; else { mul*=fib[i-1]+fib[i-2]; mul++; } } } int main() { make(); int n; scanf("%d",&n); ll mx=0; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { printf("%lld ",ans[i][j]); mx=max(mx,ans[i][j]); }puts(""); } /* vector <ll> vec; vector <int> ord; for(int i=0;i<n;i++) ord.push_back(i); do { ll sum=0; for(int i=0;i+1<n;i++) sum+=ans[ord[i]][ord[i+1]]; vec.push_back(sum); }while(next_permutation(ord.begin(),ord.end())); sort(vec.begin(),vec.end()); vector <ll> vx=vec; vx.erase(unique(vx.begin(),vx.end()),vx.end()); printf("%d %d : %lld\n",vx.size(),vec.size(),vec[vec.size()-1]); //printf("%lld\n",mx);*/ return 0; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; using ll = long long; const ll MOD = 1e9 + 7; const int N = 10; bool visited[N]; vector<ll> vals; ll cost[N][N]; int n, j; void dfs(int i, ll s) { visited[i] = true; int outs = 0; for (int t = 0; t < n; ++t) { if (! visited[t]) { ++outs; dfs(t, s + cost[i][t]); } } if (outs == 0 && i > j) vals.push_back(s); visited[i] = false; } bool works() { for (j = 0; j+1 < n; ++j) dfs(j, 0); sort(vals.begin(), vals.end()); cerr << vals.back() << '\n'; bool ans = true; for (int i = 0; i+1 < vals.size(); ++i) { if (vals[i] == vals[i+1]) ans = false; } vals.clear(); return ans; } int main() { vector<vector<int>> xr = { {1}, {1,2}, {1,2,4}, {1,2,4,7}, {1,2,4,7,12}, {1,2,4,8,13,18}, {1,2,4,8,14,19,24}, {1,2,4,8,15,24,29,34}, {1,7,10,13,21,26,41,43,45} }; cin >> n; ll b = 1; for (int i = 0; i+1 < n; ++i) { for (j = i+1; j < n; ++j) { cost[i][j] = b * xr[n-2-i][j-1-i]; cost[j][i] = cost[i][j]; } if (i+2 < n) b *= (xr[n-2-i][n-2-i] + xr[n-2-i][n-3-i]); } if (works()) { for (int i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { cout << cost[i][j] << ' '; } cout << '\n'; } } else cout << "QAQ\n"; }
#define DEBUG 0 /** * File : F.cpp * Author : Kazune Takahashi * Created : 6/16/2019, 1:52:04 AM * Powered by Visual Studio Code */ #include <iostream> #include <iomanip> #include <algorithm> #include <vector> #include <string> #include <complex> #include <tuple> #include <queue> #include <stack> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <bitset> #include <functional> #include <random> #include <chrono> #include <cctype> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> using namespace std; #define maxs(x, y) (x = max(x, y)) #define mins(x, y) (x = min(x, y)) typedef long long ll; void Yes() { cout << "Yes" << endl; exit(0); } void No() { cout << "No" << endl; exit(0); } const int MAX_SIZE = 1000010; class mint { public: static ll MOD; ll x; mint() : x(0) {} mint(ll x) : x(x % MOD) {} mint operator-() const { return x ? MOD - x : 0; } mint &operator+=(const mint &a) { if ((x += a.x) >= MOD) { x -= MOD; } return *this; } mint &operator-=(const mint &a) { return *this += -a; } mint &operator*=(const mint &a) { (x *= a.x) %= MOD; return *this; } mint &operator/=(const mint &a) { return (*this *= power(MOD - 2)); } mint operator+(const mint &a) const { return mint(*this) += a; } mint operator-(const mint &a) const { return mint(*this) -= a; } mint operator*(const mint &a) const { return mint(*this) *= a; } mint operator/(const mint &a) const { return mint(*this) /= a; } bool operator<(const mint &a) const { return x < a.x; } bool operator==(const mint &a) const { return x == a.x; } const mint power(ll N) { if (N == 0) { return 1; } else if (N % 2 == 1) { return *this * power(N - 1); } else { mint half = power(N / 2); return half * half; } } }; ll mint::MOD = 1e9 + 7; istream &operator>>(istream &stream, mint &a) { return stream >> a.x; } ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; } mint inv[MAX_SIZE]; mint fact[MAX_SIZE]; mint factinv[MAX_SIZE]; void init() { inv[1] = 1; for (int i = 2; i < MAX_SIZE; i++) { inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i); } fact[0] = factinv[0] = 1; for (int i = 1; i < MAX_SIZE; i++) { fact[i] = mint(i) * fact[i - 1]; factinv[i] = inv[i] * factinv[i - 1]; } } mint choose(int n, int k) { if (n >= 0 && k >= 0 && n - k >= 0) { return fact[n] * factinv[k] * factinv[n - k]; } return 0; } ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } // const double epsilon = 1e-10; // const ll infty = 1000000000000000LL; // const int dx[4] = {1, 0, -1, 0}; // const int dy[4] = {0, 1, 0, -1}; int N; ll w[10][10]; int X[10]; vector<ll> V; unordered_set<int> H; void make_vector() { for (auto i = 0; i < N; i++) { for (auto j = 1;; j++) { bool ok = true; if (H.find(j) != H.end()) { ok = false; } for (auto &x : V) { if (H.find(x + j) != H.end()) { ok = false; break; } } if (!ok) { continue; } #if DEBUG == 1 assert(H.find(j) == H.end()); cerr << "H.insert(" << j << ")" << endl; #endif H.insert(j); for (auto &x : V) { #if DEBUG == 1 assert(H.find(x + j) == H.end()); cerr << "H.insert(" << x + j << ")" << endl; #endif H.insert(x + j); } V.push_back(j); #if DEBUG == 1 cerr << "V.push_back(" << j << ")" << endl; #endif break; } } } ll hamilton_path(int K) { ll ans = 0; for (auto i = 0; i < K; i++) { X[i] = i; } do { ll t = 0; for (auto i = 0; i < K - 1; i++) { t += w[X[i]][X[i + 1]]; } maxs(ans, t); } while (next_permutation(X, X + K)); return ans; } int main() { cin >> N; for (auto i = 0; i < N; i++) { w[i][i] = 0; } make_vector(); for (auto i = 1; i < N; i++) { ll M = hamilton_path(i) + 1; for (auto j = 0; j < i; j++) { w[i][j] = w[j][i] = M * V[j]; } } for (auto i = 0; i < N; i++) { for (auto j = 0; j < N; j++) { cout << w[i][j]; if (j < N - 1) { cout << " "; } else { cout << endl; } } } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define all(aaa) aaa.begin(), aaa.end() const int N = 10; int a[N - 1] = {1, 2, 4, 7, 12, 20, 29, 38, 52}; ll w[N][N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; ll mx = 0; for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { w[j][i] = w[i][j] = (mx + 1) * a[j]; } int b[10]; for (int j = 0; j <= i; j++) { b[j] = j; } do { ll cur = 0; for (int j = 0; j < i; j++) { cur += w[b[j]][b[j + 1]]; } mx = max(mx, cur); } while (next_permutation(b, b + i + 1)); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << w[i][j] << " "; } cout << "\n"; } return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <random> #include <chrono> #include <assert.h> using namespace std; using ll = long long; const ll MOD = 1e9 + 7; const int N = 10; bool visited[N]; vector<ll> vals; ll cost[N][N]; int n, j; void dfs(int i, ll s) { visited[i] = true; int outs = 0; for (int t = 0; t < n; ++t) { if (! visited[t]) { ++outs; dfs(t, s + cost[i][t]); } } if (outs == 0 && i > j) vals.push_back(s); visited[i] = false; } bool works() { for (j = 0; j+1 < n; ++j) dfs(j, 0); sort(vals.begin(), vals.end()); bool ans = true; for (int i = 0; i+1 < vals.size(); ++i) { if (vals[i] == vals[i+1]) ans = false; } vals.clear(); return ans; } // rand-function that works properly on windows, and is faster than rand // https://codeforces.com/blog/entry/61587 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template<class T> T rand(T a, T b) { return uniform_int_distribution<T>(a, b)(rng); } const ll MAX_V = 1e10; ll log2(int v) { int res = 1; while(v > 1) { v /= 2; ++res; } return res; } // Find 9 numbers s.t. their sum is distinct int main() { vector<vector<int>> xr = { {1}, {1,2}, {1,2,4}, {1,2,4,7}, {1,2,4,7,12}, {1,2,4,8,13,18}, {1,2,4,8,14,19,24}, {1,2,4,8,15,24,29,34}, {1,7,10,13,21,26,41,43,45} }; cin >> n; while(true) { ll b = 1; for (int i = 0; i+1 < n; ++i) { for (j = i+1; j < n; ++j) { cost[i][j] = b * xr[n-2-i][j-1-i]; cost[j][i] = cost[i][j]; } if (i+2 < n) b *= (xr[n-2-i][n-2-i] + xr[n-2-i][n-3-i]); } /* for (int i = 0; i < n; ++i) { for (j = i+1; j < n; ++j) { if (rand(0, 1)) cost[i][j] = rand(1ll, MAX_V); cost[j][i] = cost[i][j]; } } */ if (works()) { for (int i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { cout << cost[i][j] << ' '; } cout << '\n'; } break; } } }
#include <iostream> #include <vector> #include <algorithm> #include <set> #define llint long long using namespace std; llint n; llint a[15][15]; llint f[105]; int main(void) { cin >> n; a[1][2] = a[2][1] = 1; f[0] = 0, f[1] = 1, f[2] = 2; for(int z = 3; z <= 10; z++){ f[z] = f[z-1]; while(1){ vector<llint> vec; for(int i = 1; i <= z; i++){ vec.push_back(f[i]); for(int j = 1; j <= z; j++){ if(i < j) vec.push_back(f[i]+f[j]); } } sort(vec.begin(), vec.end()); bool flag = true; for(int i = 1; i < vec.size(); i++){ if(vec[i-1] == vec[i]) flag = false; } if(flag) break; f[z]++; } } for(int i = 3; i <= n; i++){ llint perm[10], m = 0; for(int j = 1; j <= i-1; j++) perm[j] = j; do{ llint sum = 0; for(int j = 1; j < i-1; j++) sum += a[perm[j]][perm[j+1]]; m = max(m, sum); }while(next_permutation(perm+1, perm+i)); for(int j = 1; j < i; j++){ a[i][j] = a[j][i] = (m+1)*f[j]; } } for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ cout << a[i][j] << " "; } cout << endl; } return 0; }
#define _USE_MATH_DEFINES #include "bits/stdc++.h" #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rrep(i,a,b) for(int i=(a);i>(b);i--) using namespace std; typedef long long int ll; typedef pair<ll, ll> P; typedef complex<double> com; const int mod = 1e9 + 7; const int MOD = 998244353; const int inf = 2e9; //template class UnionFind { public: vector<int> par; UnionFind(int n) { par = vector<int>(n, -1); } int root(int a) { if (par[a] < 0) { return a; } else { return par[a] = root(par[a]); } } int size(int a) { return -par[root(a)]; } bool connect(int a, int b) { a = root(a); b = root(b); if (a == b) { return false; } if (size(a) < size(b)) { swap(a, b); } par[a] += par[b]; par[b] = a; return true; } }; struct edge { int cost, u, v; }; bool comp(edge& e1,edge& e2){return e1.cost < e2.cost;} #define MAX_E 200010 edge es[MAX_E]; int V,E; int kruskal() { sort(es, es + E, comp); UnionFind uni(V); int res = 0; rep(i, 0, E) { edge e = es[i]; if (uni.root(e.u) != uni.root(e.v)) { uni.connect(e.u, e.v); res += e.cost; } } return res; } ll mpow(ll a,ll b) { ll r = 1; a %= mod; while (b) { r = r * ((b % 2) ? a : 1) % mod; a = a * a % mod, b >>= 1; } return r; } vector<ll> fac, finv; void factor(int N) { fac.resize(N); finv.resize(N); fac[0] = finv[0] = 1; rep(i, 1, N)fac[i] = 1LL * fac[i - 1] * i % mod; finv[N - 1] = mpow(fac[N - 1], mod - 2); for (int i = N - 2; i; --i) finv[i] = 1LL * finv[i + 1] * (i + 1) % mod; } int nCr(int n, int m) { if (m < 0 || n < m) return 0; return 1LL * (1LL * fac[n] * finv[m] % mod) * finv[n - m] % mod; } ll merge_cnt(vector<int>& a) { int n = a.size(); if (n <= 1) return 0; ll cnt = 0; vector<int> b(a.begin(), a.begin() + n / 2); vector<int> c(a.begin() + n / 2, a.end()); cnt += merge_cnt(b); cnt += merge_cnt(c); int ai = 0, bi = 0, ci = 0; while (ai < n) { if (bi < b.size() && (ci == c.size() || b[bi] <= c[ci])) { a[ai++] = b[bi++]; } else { cnt += n / 2 - bi; a[ai++] = c[ci++]; } } return cnt; } void fft(vector<com>& x, bool inv) { int s = x.size(); if (s == 1) return; else { vector<com> even(s / 2), odd(s / 2); rep(i, 0, s / 2) { even[i] = x[i * 2]; odd[i] = x[i * 2 + 1]; } fft(even, inv); fft(odd, inv); com w = 1, w_0 = polar(1.0, (inv ? -1 : 1) * 2LL * M_PI / s); int t = s / 2 - 1; rep(i, 0, s) { x[i] = even[i & t] + w * odd[i & t]; w *= w_0; } } } #define MAX_V 100010 ll d[MAX_V]; void dijkstra(int s,const vector<vector<edge>>& G) { priority_queue<P, vector<P>, greater<P> > que; fill(d, d + MAX_V, inf); d[s] = 0; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (int i = 0; i < G[v].size(); ++i) { edge e = G[v][i]; if (d[e.v] > d[v] + e.cost) { d[e.v] = d[v] + e.cost; que.push(P(d[e.v], e.v)); } } } } bool cross(ll ax, ll ay, ll bx, ll by, ll cx, ll cy, ll dx, ll dy) { ll ta = (cx - dx) * (ay - cy) + (cy - dy) * (cx - ax); ll tb = (cx - dx) * (by - cy) + (cy - dy) * (cx - bx); ll tc = (ax - bx) * (cy - ay) + (ay - by) * (ax - cx); ll td = (ax - bx) * (dy - ay) + (ay - by) * (ax - dx); return tc * td <= 0LL && ta * tb <= 0LL; } int diameter(const vector<vector<edge>>& g) { function<P(int, int)> dfs = [&](int prev, int v) { P r(0, v); rep(i, 0, g[v].size()) { edge e = g[v][i]; if (e.v == prev) continue; P tmp = dfs(v, e.v); tmp.first += e.cost; if (r.first < tmp.first) r = tmp; } return r; }; P r = dfs(-1, 0); P t = dfs(-1, r.second); return t.first; } int bipartite(const vector<vector<int>>& g) { int n = g.size(); vector<int> color(n, -1); int white_cnt = 0; function<bool(int, int, int)> dfs = [&](int u, int prev, int c) { color[u] = c; if (c == 1) white_cnt++; for (auto v : g[u]) if (v != prev) { if (color[v] == -1) { if (!dfs(v, u, 1 - c)) return false; } else if (color[v] != 1 - c) { return false; } } return true; }; if (!dfs(0, -1, 0)) return -1; return white_cnt; } //template end int main() { int n; cin >> n; vector<vector<ll>> ans(n); rep(i, 0, n) ans[i].resize(n, 0); int a[10] = { 1,2,4,7,12,20,29,38,52,101 }; int b[10] = { 1,2,4,7,12,20,30,39,67,101 }; ll maxx = 1, sum = 0; rep(i, 0, n) { rep(j, i+1, n) { ans[j][i]=ans[i][j] = maxx * a[j-i-1]; } maxx *= b[n-i-1]; } rep(i, 0, n){ rep(j, 0, n) { printf("%lld ", ans[i][j]); } printf("\n"); } return 0; }
// https://atcoder.jp/contests/diverta2019-2/tasks/diverta2019_2_f /*<head>*/ // #include "Template.hpp" /*</head>*/ /*<body>*/ /* #region header */ /* #region 1*/ /** * @file Template.hpp * @brief 競技プログラミング用テンプレート * @author btk15049 * @date 2019/05/02 */ #include <bits/stdc++.h> using namespace std; /* #region macro */ #ifdef BTK # define DEBUG if (1) # define CIN_ONLY if (0) #else # define DEBUG if (0) # define CIN_ONLY if (1) #endif /** @def * ALLマクロ */ #define ALL(v) (v).begin(), (v).end() /** @def * 再帰ラムダをするためのマクロ */ #define REC(ret, ...) std::function<ret(__VA_ARGS__)> /* #endregion */ namespace _Template_ { /** * @brief cin高速化処理を行うための構造体 * @details CIN_ONLYマクロで動作が変わる */ struct cww { cww() { CIN_ONLY { ios::sync_with_stdio(false); cin.tie(0); } } } star; /** * @brief change min * @tparam T 型 * @param l 参照 * @param r 非参照 * @return 更新があればtrue */ template <typename T> inline bool chmin(T& l, T r) { bool a = l > r; if (a) l = r; return a; } /** * @brief chminのmax版 * @see chmin */ template <typename T> inline bool chmax(T& l, T r) { bool a = l < r; if (a) l = r; return a; } /** * @brief * vectorに直接cin流すためのやつ * @tparam T * @param is * @param v * @return istream& */ template <typename T> istream& operator>>(istream& is, vector<T>& v) { for (auto& it : v) is >> it; return is; } /** * @brief * rangeを逆向きに操作したいとき用 * @details * ループの範囲は[bg,ed)なので注意 * @see range */ class reverse_range { private: struct I { int x; int operator*() { return x - 1; } bool operator!=(I& lhs) { return x > lhs.x; } void operator++() { --x; } }; I i, n; public: reverse_range(int n) : i({0}), n({n}) {} reverse_range(int i, int n) : i({i}), n({n}) {} I& begin() { return n; } I& end() { return i; } }; /** * @brief * python みたいな range-based for を実現 * @details * ループの範囲は[bg,ed)なので注意 * !つけると逆向きにループが回る (reverse_range) * 空間計算量はO(1) * 使わない変数ができて警告が出がちなので,unused_varとかを使って警告消しするとよい */ class range { private: struct I { int x; int operator*() { return x; } bool operator!=(I& lhs) { return x < lhs.x; } void operator++() { ++x; } }; I i, n; public: range(int n) : i({0}), n({n}) {} range(int i, int n) : i({i}), n({n}) {} I& begin() { return i; } I& end() { return n; } reverse_range operator!() { return reverse_range(*i, *n); } }; /** * @brief * rangeで生まれる使わない変数を消す用(警告消し) */ template <typename T> inline T& unused_var(T& v) { return v; } using LL = long long; } // namespace _Template_ using namespace _Template_; /* #endregion */ /* #endregion */ /*</body>*/ namespace xorshift { unsigned yy = 143132322; inline unsigned rand() { yy = yy ^ (yy << 13); yy = yy ^ (yy >> 17); return yy = yy ^ (yy << 5); } inline unsigned randInt() { return rand(); } } // namespace xorshift LL w[11][11]; int N; int calc() { unordered_map<LL, int> cnt; vector<int> o(N); iota(ALL(o), 0); int c = 0; LL mx = 0; do { LL k = 0; for (int i : range(N - 1)) { k += w[o[i]][o[i + 1]]; } cnt[k]++; if (cnt[k] >= 3) { c++; } chmax(mx, k); } while (next_permutation(ALL(o))); cerr << c << " " << mx << endl; return c; } LL magick_number[] = {1, 2, 4, 7, 12, 20, 29, 38, 52}; LL magick_sum[] = {2, 4, 5, 12, 20, 33, 50, 68, 81}; int main() { cin >> N; LL bottom = 1; for (int i : range(1, N)) { for (int j : range(i)) { w[i][j] = w[j][i] = bottom * magick_number[j]; } bottom *= magick_sum[i - 1]; } // int cur = calc(); for (int i : range(N)) { for (int j : range(N)) { cout << w[i][j] << " "; } cout << endl; } return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <random> #include <chrono> #include <assert.h> using namespace std; using ll = long long; const ll MOD = 1e9 + 7; const int N = 10; bool visited[N]; vector<ll> vals; ll cost[N][N]; int n, j; void dfs(int i, ll s) { visited[i] = true; int outs = 0; for (int t = 0; t < n; ++t) { if (! visited[t]) { ++outs; dfs(t, s + cost[i][t]); } } if (outs == 0 && i > j) vals.push_back(s); visited[i] = false; } bool works() { for (j = 0; j+1 < n; ++j) dfs(j, 0); sort(vals.begin(), vals.end()); bool ans = true; for (int i = 0; i+1 < vals.size(); ++i) { if (vals[i] == vals[i+1]) ans = false; } vals.clear(); return ans; } // rand-function that works properly on windows, and is faster than rand // https://codeforces.com/blog/entry/61587 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template<class T> T rand(T a, T b) { return uniform_int_distribution<T>(a, b)(rng); } const ll MAX_V = 1e10; ll log2(int v) { int res = 1; while(v > 1) { v /= 2; ++res; } return res; } // Find 9 numbers s.t. their sum is distinct int main() { vector<vector<int>> xr = { {1}, {1,2}, {1,2,4}, {1,2,4,7}, {1,2,4,7,12}, {1,2,4,8,13,18}, {1,2,4,8,14,19,24}, {1,2,4,8,15,24,29,34}, {1,7,10,13,21,26,41,43,45} }; cin >> n; while(true) { ll b = 1; for (int i = 0; i+1 < n; ++i) { int ln = n-1-i; for (j = i+1; j < n; ++j) { cost[i][j] = b * xr[ln-1][j-1-i]; cost[j][i] = cost[i][j]; } if (ln >= 2) b *= (1+xr[ln-1][ln-1]+xr[ln-1][ln-2]); } /* for (int i = 0; i < n; ++i) { for (j = i+1; j < n; ++j) { if (rand(0, 1)) cost[i][j] = rand(1ll, MAX_V); cost[j][i] = cost[i][j]; } } */ if (works()) { for (int i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { cout << cost[i][j] << ' '; } cout << '\n'; } break; } } }