text
stringlengths
49
983k
#include<stdio.h> #include<algorithm> using namespace std; int ABS(int a){return max(a,-a);} long long C[3000][3000]; int main(){ int a,b,c,d,e,f; scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f); int m=1; if(2*ABS(e-c)==a)m*=2; if(2*ABS(f-d)==b)m*=2; int P=min(ABS(e-c),a-ABS(e-c)); int Q=min(ABS(d-f),b-ABS(d-f)); C[0][0]=1; int mod=100000007; C[1][0]=C[1][1]=1; for(int i=2;i<3000;i++){ C[i][0]=C[i][i]=1; for(int j=1;j<i;j++){ C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod; } } printf("%lld\n",(C[P+Q][P]*m)%mod); }
//===================================================== // // (setq backup-inhibited t) // (setq auto-save-default nil) // // (column-number-mode t) // (setq-default tab-width 2 indent-tabs-mode nil) // // (global-set-key "\C-h" 'delete-backward-char) // // (setq indent-line-function 'indent-relative-maybe) // (global-set-key "\C-m" 'newline-and-indent) // //===================================================== #include <iostream> #include <vector> #include <string> #include <map> #include <algorithm> #define REP(i,n) for(int i=0;i<(int)(n);i++) using namespace std; int combi[1024][1024]; void init() { REP(i,1023){ REP(j,1023){ combi[i][j] = 0; } } combi[0][0] = 1; REP(i,1023){ REP(j,1023){ combi[i+1][j] += combi[i][j]; combi[i+1][j] %= 100000007; combi[i][j+1] += combi[i][j]; combi[i][j+1] %= 100000007; } } } int main() { init(); int r,c,a1,a2,b1,b2; cin >> r >> c >> a1 >> a2 >> b1 >> b2; b1 = (b1 + r - a1) % r; b2 = (b2 + c - a2) % c; b1 = min(b1, (r - b1) % r); b2 = min(b2, (c - b2) % c); int sum = combi[b1][b2]; if (b1 == r - b1 && r != 1) sum *= 2; sum %= 100000007; if (b2 == c - b2 && c != 1) sum *= 2; sum %= 100000007; cout << sum << endl; return 0; }
#include <bits/stdc++.h> #define int long long using namespace std; typedef long long ll; const ll mod = 1e8+7; ll mod_pow(ll x,ll n){ ll res=1; while(n){ if(n%2) res=res*x%mod; x=x*x%mod; n/=2; } return res; } ll divi(ll a,ll b){ return a * mod_pow(b,mod-2) % mod; } ll factorial(int i){ static vector<ll> k(1e6); if(!k[0]){k[0]=1;for(int i=1;i<(int)k.size();i++)k[i]=i*k[i-1]%mod;} return k[i]; } ll nCr(ll n,ll r){ ll a = mod_pow( factorial(r) * factorial(n-r) % mod ,mod-2); return factorial(n) * a % mod; } typedef pair<int,int> P; int h, w, sy, sx, ty, tx; P cal(int y, int x){ int Y = abs(y - sy); int X = abs(x - sx); return P( Y + X, nCr( Y + X, X )); } void solve(){ map<int,int> ans; for(int i=-1;i<=1;i++){ for(int j=-1;j<=1;j++){ P tmp = cal( ty + i * h, tx + j * w ); ans[tmp.first] += tmp.second; ans[tmp.first] %= mod; } } auto p = ans.begin(); cout<<(*p).second<<endl; } signed main(){ cin>>h>>w; cin>>sy>>sx>>ty>>tx; solve(); return 0; }
#include <iostream> #include <iomanip> #include <sstream> #include <cstdio> #include <string> #include <vector> #include <algorithm> #include <complex> #include <cstring> #include <cstdlib> #include <cmath> #include <cassert> #include <climits> #include <queue> #include <set> #include <map> #include <valarray> #include <bitset> #include <stack> using namespace std; #define REP(i,n) for(int i=0;i<(int)(n);++i) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) #define ALL(c) (c).begin(), (c).end() typedef long long ll; typedef pair<int,int> pii; const int INF = 1<<29; const double PI = acos(-1); const double EPS = 1e-8; const int NUM = 3010; ll combi[NUM][NUM]; void calcCombi(ll mod) { combi[0][0] = 1; REP(i, NUM-1) { combi[i][0] = combi[i][i] = 1; REP(j, i) combi[i+1][j+1] = (combi[i][j] + combi[i][j+1]) % mod; } } int main() { ll mod = 1e8+7; calcCombi(mod); int r, c, y1,x1,y2,x2; cin>>r>>c>>y1>>x1>>y2>>x2; int a = min(abs(y2-y1),y1+r-y2); a = min(a,y2+r-y1); int mul = 1; if (abs(y2-y1) == y1+r-y2 || abs(y2-y1) == y2+r-y1) mul *= 2; int b = min(abs(x2-x1),x1+c-x2); b = min(b,x2+c-x1); if (abs(x2-x1) == x1+c-x2 || abs(x2-x1) == x2+c-x1) mul *= 2; cout << combi[a+b][a] * mul % mod << endl; }
#include<cstdio> #include<algorithm> #include<cstring> #include<string> #include<vector> #include<queue> #define AMA (100000007) using namespace std; int r,c,a1,a2,b1,b2,K; typedef pair<int,int> P; inline int ab(int a){ return a>0?a:-a; } int dx[4][2]={ {1,0}, {-1,0}, {0,1}, {0,-1} }; int dy[4][2]={ {0,1}, {0,-1}, {-1,0}, {1,0} }; int dp[1001][1001]; struct st{ int x,y,c; st(int x,int y,int c) : x(x),y(y),c(c) {} }; int dmx[]={1,0,-1,0}; int dmy[]={0,1,0,-1}; int memo[1001][1001]; int solve(int x,int y,int num){ /* if(memo[x][y]!=num) return 0; if(x==b1 && y==b2){/*printf("%d %d %d %d\n",x,y,st,num); return dp[x][y][st]=1;} if(K<=num) return 0; if(dp[x][y][st]!=-1) return dp[x][y][st]; int ret=0; for(int i=0;i<2;i++){ int nx = (x + dx[st][i])%r; int ny = (y + dy[st][i])%c; if(nx<0) nx=r-1; if(ny<0) ny=c-1; ret+=(solve(nx,ny,st,num+1))%AMA; ret%=AMA; }*/ if(x==b1 && y==b2) return dp[x][y]=1; if(K<=num) return dp[x][y]=0; if(dp[x][y]!=-1) return dp[x][y]; int ret=0; for(int i=0;i<4;i++){ int nx=(x+dmx[i])%r; int ny=(y+dmy[i])%c; if(nx<0) nx=r-1; if(ny<0) ny=c-1; if(memo[nx][ny]==num+1){ ret+=(solve(nx,ny,num+1))%AMA; ret%=AMA; } } return dp[x][y]=ret; } void search(int x,int y){ queue<st> q; q.push(st(x,y,0)); memo[x][y]=0; while(!q.empty()){ st p = q.front(); q.pop(); x = p.x; y = p.y; // if(x==b1 && y==b2) return p.c; for(int i=0;i<4;i++){ int nx=(x+dmx[i])%r; int ny=(y+dmy[i])%c; if(nx<0) nx=r-1; if(ny<0) ny=c-1; if(memo[nx][ny]!=-1) continue; memo[nx][ny]=p.c+1; q.push(st(nx,ny,p.c+1)); } } } int main(){ memset(dp,-1,sizeof(dp)); memset(memo,-1,sizeof(memo)); scanf("%d %d %d %d %d %d",&r,&c,&a1,&a2,&b1,&b2); int res=0; search(a1,a2); K = memo[b1][b2]; // printf("%d\n",K); if(a1==b1 && a2==b2) puts("1"); /* else if(a1==b1 || a2==b2){ for(int i=0;i<2;i++) res+=(solve(a1,a2,i,0))%AMA; printf("%d\n",res); } else{ for(int i=0;i<4;i++) res+=(solve(a1,a2,i,0))%AMA; printf("%d\n",res); }*/ printf("%d\n",(solve(a1,a2,0))%AMA); }
#include<bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) #define MOD 100000007 using namespace std; typedef pair<int,int>P; int d[1000][1000],ans[1000][1000]; int dx[]{1,-1,0,0},dy[]{0,0,1,-1}; int main(){ int r,c,x1,y1,x2,y2;cin>>r>>c>>x1>>y1>>x2>>y2; queue<P>que; memset(d,-1,sizeof(d)); ans[x1][y1]=1;d[x1][y1]=0;que.push(P(x1,y1)); while(!que.empty()){ P p=que.front();que.pop(); rep(i,4){ int nx=p.first+dx[i]+r,ny=p.second+dy[i]+c; while(nx>=r)nx-=r;while(ny>=c)ny-=c; if(d[nx][ny]==-1){ d[nx][ny]=d[p.first][p.second]+1; que.push(P(nx,ny)); } if(d[nx][ny]==d[p.first][p.second]+1) (ans[nx][ny]+=ans[p.first][p.second])%=MOD; } } printf("%d\n",ans[x2][y2]); }
#include<iostream> #define F 1001 using namespace std; int dp[F][F]; int main(){ for(int i=1;i<F;i++)for(int j=1;j<F;j++)dp[i][j]=0; for(int i=0;i<F;i++)dp[0][i]=1; for(int i=0;i<F;i++)dp[i][0]=1; for(int i=1;i<F;i++)for(int j=1;j<F;j++)dp[i][j]=(dp[i-1][j]+dp[i][j-1])%100000007; int r,e,a,b,c,d,ans; cin>>r>>e>>a>>b>>c>>d; a-=c,b-=d; if(a<0)a=-a;if(b<0)b=-b; if(a*2>r)a=r-a;if(b*2>e)b=e-b; ans=dp[a][b]; if(a*2==r)ans*=2;if(b*2==e)ans*=2; cout<<ans%100000007<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define for_(i,a,b) for(int i=a;i<b;++i) typedef long long lint; const lint MOD = (lint)1e8 + 7; lint pascal[1010][1010]; int main() { pascal[0][0] = 1; for_(i,1,1010) { pascal[i][0] = pascal[i][i] = 1; for_(j,1,i) pascal[i][j] = (pascal[i - 1][j] + pascal[i - 1][j - 1]) % MOD; } int r, c, sy, sx, gy, gx; cin >> r >> c >> sy >> sx >> gy >> gx; int t = 1; if (r == abs(gy - sy) * 2) t *= 2; if (c == abs(gx - sx) * 2) t *= 2; int yy = min(abs(gy - sy), r - abs(gy - sy)); int xx = min(abs(gx - sx), c - abs(gx - sx)); cout << (pascal[xx+yy][xx] * t) % MOD << endl; return 0; }
#include <stdio.h> #include <string.h> #include <utility> #include <queue> #define INF 100000007 #define RMAX 1000 #define CMAX 1000 using namespace std; typedef pair<int,int> P; typedef pair<P,int> PP; int main(void){ int r, c, a1, a2, b1, b2, p[RMAX][CMAX], cmin[RMAX][CMAX],x, y, tx, ty, dx[4] = {0,1,0,-1}, dy[4] = {-1,0,1,0}, i, j, cst; queue<PP> que; scanf("%d%d%d%d%d%d",&r,&c,&a1,&a2,&b1,&b2); memset(p,0,sizeof(p)); for(i = 0;i < RMAX;i++){ for(j = 0;j < CMAX;j++) cmin[i][j] = INF; } cmin[a1][a2] = 0; p[a1][a2] = 1; que.push(PP(P(a1,a2),0)); while(que.size()){ x = que.front().first.first, y = que.front().first.second, cst = que.front().second; que.pop(); if(((x == 0 && cst + 1 <= cmin[r - 1][y]) || (x == r - 1 && cst + 1 <= cmin[0][y]))){ if(x == r - 1) tx = 0; else tx = r - 1; if(cst + 1 == cmin[tx][y]){ p[tx][y] = (p[x][y] + p[tx][y]) % INF; } else{ p[tx][y] = p[x][y]; cmin[tx][y] = cst + 1; que.push(PP(P(tx,y),cst + 1)); } } if(((y == 0 && cst + 1 <= cmin[x][c - 1]) || (y == c - 1 && cst + 1 <= cmin[x][0]))){ if(y == c - 1) ty = 0; else ty = c - 1; if(cst + 1 == cmin[x][ty]){ p[x][ty] = (p[x][y] + p[x][ty]) % INF; } else{ p[x][ty] = p[x][y]; cmin[x][ty] = cst + 1; que.push(PP(P(x,ty),cst + 1)); } } for(i = 0;i < 4;i++){ if(x + dx[i] >= 0 && x + dx[i] < r && y + dy[i] >= 0 && y + dy[i] < c && cst + 1 <= cmin[x + dx[i]][y + dy[i]]){ if(cmin[x + dx[i]][y + dy[i]] == cst + 1){ p[x + dx[i]][y + dy[i]] = (p[x][y] + p[x + dx[i]][y + dy[i]]) % INF; } else{ p[x + dx[i]][y + dy[i]] = p[x][y]; cmin[x + dx[i]][y + dy[i]] = cst + 1; que.push(PP(P(x + dx[i],y + dy[i]),cst + 1)); } } } } printf("%d\n",p[b1][b2]); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define all(x) (x).begin(),(x).end() #define pb push_back #define fi first #define se second #define dbg(x) cout<<#x" = "<<((x))<<endl template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;} template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;} const ll mod = 1e8+7; ll mod_pow(ll x, ll n){ ll ret = 1; while(n){ if(n&1) (ret*=x)%=mod; (x*=x)%=mod; n>>=1; } return ret; } ll mod_inv(ll x){ return mod_pow(x,mod-2); } const int N = 2020; ll f[N]; ll C(ll n, ll r){ ll ret = f[n]; (ret*=mod_inv(f[r]))%=mod; (ret*=mod_inv(f[n-r]))%=mod; return ret; } int main(){ f[0]=1; for(ll i=1; i<N; ++i) f[i]=(f[i-1]*i)%mod; int r,c,a1,a2,b1,b2; cin >>r >>c >>a1 >>a2 >>b1 >>b2; int mul = 1; int X = abs(a1-b1); if(X == r-X) mul *= 2; X = min(X,r-X); int Y = abs(a2-b2); if(Y == c-Y) mul *= 2; Y = min(Y,c-Y); cout << (C(X+Y,X)*mul)%mod << endl; return 0; }
#include<iostream> #include<cmath> #define M 100000007 using namespace std; int comb[1100][1100]; int main(){ int r,c,a1,b1,a2,b2; for(int i=0;i<=1000;i++){ for(int j=0;j<=1000;j++)comb[i][j] = 0; comb[i][0] = comb[i][i] = 1; } for(int i=1;i<=1000;i++){ for(int j=1;j<i;j++){ comb[i][j] = (comb[i-1][j-1] + comb[i-1][j]) % M; } } cin >> r >> c >> a1 >> b1 >> a2 >> b2; int path = r+c+1, num = 0; int a = abs(a1-a2), b = abs(b1-b2); if(path>a+b){ path = a+b; num = comb[a+b][a]; }else if(path == a+b){ num += comb[a+b][a]; num %= M; } if(path>r-a+b){ path = r-a+b; num = comb[r-a+b][r-a]; }else if(path == r-a+b){ num += comb[r-a+b][r-a]; num %= M; } if(path>a+c-b){ path = a+c-b; num = comb[a+c-b][a]; }else if(path == a+c-b){ num += comb[a+c-b][a]; num %= M; } if(path>r-a+c-b){ path = r-a+c-b; num = comb[r-a+c-b][r-a]; }else if(path == r-a+c-b){ num += comb[r-a+c-b][r-a]; num %= M; } cout << num % M << endl; }
#include <stdio.h> const int MOD=1e8+7; const int N=2e3+10; typedef long long int ll; ll f[N]; ll mul(ll a,ll b){return (a*b)%MOD;} ll add(ll a,ll b){return (a+b)%MOD;} ll pow(ll a,int t){ ll ans=1; while(t){ if(t&1)ans=mul(ans,a); a=mul(a,a); t>>=1; } return ans; } ll rev(ll n){return pow(n,MOD-2);} ll C(int n,int m){return mul(f[n],mul(rev(f[n-m]),rev(f[m])));} int min(int a,int b){return a>b?b:a;} int abs(int n){return n>0?n:-n;} void out(ll n){ printf("%lld ",n); return ; } int main(){ int r,c,a1,a2,b1,b2; f[0]=f[1]=1; for(int i=2;i<N;i++)f[i]=mul(i,f[i-1]); scanf("%d%d%d%d%d%d",&r,&c,&a1,&b1,&a2,&b2); if((abs(a1-a2)<<1)==r){ if((abs(b1-b2)<<1)==c)printf("%lld\n",mul(4,C((r+c)>>1,r>>1))); else printf("%lld\n",mul(2,C((r>>1)+min(abs(b1-b2),c-abs(b1-b2)),r>>1))); } else{ if((abs(b1-b2)<<1)==c)printf("%lld\n",mul(2,C((c>>1)+min(abs(a1-a2),r-abs(a1-a2)),r>>1))); else printf("%lld\n",C(min(abs(a1-a2),r-abs(a1-a2))+min(abs(b1-b2),c-abs(b1-b2)),min(abs(a1-a2),r-abs(a1-a2)))); } }
#include<iostream> #include<cstdio> #include<string> #include<vector> #include<algorithm> #include<queue> #include<set> #include<unordered_map> #include<string.h> #define int long long #define mod 100000007 using namespace std; int memo[2001][2001]; int saiki(int a, int b) { if (memo[a][b] != -1)return memo[a][b]; int c = 0; if (a == 0 || b == 0)c = 1; else { c += saiki(a - 1, b); c += saiki(a, b - 1); } return memo[a][b] = c%mod; } signed main() { memset(memo, -1, sizeof(memo)); int a, b, c, d, e, f; cin >> a >> b >> c >> d >> e >> f; int g = abs(e - c), h = abs(f - d); int i = a - max(e,c) + min(e,c), j = b - max(f,d) + min(f,d); int k = min(g, i), l = min(h, j); if (c == e) { if (h == j)puts("2"); else puts("1"); return 0; } if (d == f) { if (g == i)puts("2"); else puts("1"); return 0; } int ans = saiki(k, l); if (g == i) { if (h == j) { ans *= 4; } else { ans *= 2; } } else { if (h == j) { ans *= 2; } } cout << ans%mod << endl; }
#include <iostream> #include <limits.h> #include <queue> #include <algorithm> #include <map> #include <cstring> #define dprintf(s,...) printf("%s:%d" s,__func__,__LINE__,__VA_ARGS__) using namespace std; typedef pair<int,int> P; typedef long long int ll; int r,c,a1,a2,b1,b2; int mod=100000007; int extgcd(int a, int b, int &x, int &y){//ax+by=gcd(a,b) int d=a; if(b!=0){ d = extgcd(b, a%b, y, x); y-= (a/b)*x; }else{ x=1; y=0; } return d; } int mod_inv(int a){ int x,y; extgcd(a,mod,x,y); return(mod + x%mod) % mod; } ll conb(int n,int r){ ll ret = 1,tmp = 1; while(r!=0){ int N = n%mod; int R = r%mod; if(N<R) return 0; for(int i=0;i<R;i++) ret = ret*(N-i) % mod; for(int i=0;i<R;i++) tmp = tmp*(i+1) % mod; ret = ret*mod_inv(tmp) % mod; n /= mod; r /= mod; } return ret; } int solve(){ ll ret=0; int t=1; int rr=min(abs(a1-b1), r-abs(a1-b1)); int cc=min(abs(a2-b2), c-abs(a2-b2)); if(r==abs(a1-b1)*2) t*=2; if(c==abs(a2-b2)*2) t*=2; ret = conb(rr+cc, cc)*t % mod; return ret; } int main(){ cin>>r>>c>>a1>>a2>>b1>>b2; cout<<solve()<<endl; return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <climits> #include <cassert> #include <iostream> #include <iomanip> #include <sstream> #include <algorithm> #include <numeric> #include <complex> #include <stack> #include <queue> #include <list> #include <set> #include <map> #include <bitset> #include <functional> #include <iterator> using namespace std; #define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl #define repi(i,a,b) for(int i=int(a);i<int(b);i++) #define peri(i,a,b) for(int i=int(b);i-->int(a);) #define rep(i,n) repi(i,0,n) #define per(i,n) peri(i,0,n) #define iter(c) __typeof__((c).begin()) #define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i) #define all(c) (c).begin(),(c).end() #define mp make_pair typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<string> vs; typedef pair<int,int> pii; const int INFTY=1<<29; const double EPS=1e-9; template<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){ return os<<'('<<p.first<<','<<p.second<<')'; } template<typename T> ostream& operator<<(ostream& os,const vector<T>& a){ os<<'['; rep(i,a.size()) os<<(i?" ":"")<<a[i]; return os<<']'; } const int MOD=1e8+7; int ncr(int n,int r) { static int memo[2001][2001]; if(memo[n][r]==0){ if(r==0 || r==n) memo[n][r]=1; else memo[n][r]=(ncr(n-1,r-1)+ncr(n-1,r))%MOD; } return memo[n][r]; } int main() { for(int r,c,a1,a2,b1,b2;cin>>r>>c>>a1>>a2>>b1>>b2;){ int v1=abs(b1-a1),v2=r-v1; if(v1>v2) swap(v1,v2); int h1=abs(b2-a2),h2=c-h1; if(h1>h2) swap(h1,h2); int res=ncr(v1+h1,v1); if(v1==v2) (res*=2)%=MOD; if(h1==h2) (res*=2)%=MOD; cout<<res<<endl; } return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #define rep(i, n) for(int i = 0; i < (n); i++) #define FOR(i, a, b) for(int i = (a); i < (b); i++) #define all(v) (v).begin(), (v).end() #define rev(s) (s).rbegin(), (s).rend() #define MP make_pair #define X first #define Y second using namespace std; typedef long long ll; typedef pair<int, int> P; int dist(P p){ return p.X+p.Y; } void pp(P p){ cout << "x = " << p.X << ", y = " << p. Y << endl; } int cost[1010][1010]; int main(){ cost[0][0] = 1; rep(i, 1010){ rep(j, 1010){ if(i>0) cost[i][j] += cost[i-1][j]; cost[i][j] %= 100000007; if(j>0) cost[i][j] += cost[i][j-1]; cost[i][j] %= 100000007; } } int r, c, a1, a2, b1, b2; cin >> r >> c >> a1 >> a2 >> b1 >> b2; P d[4]; d[0].X = abs(a1-b1); d[0].Y = abs(a2-b2); d[1].X = d[0].X; d[1].Y = c - d[0].Y; d[2].X = r - d[0].X; d[2].Y = d[0].Y; d[3].X = r - d[0].X; d[3].Y = c - d[0].Y; //rep(i, 4) pp(d[i]); vector<int> sh; int s = 1<<29; rep(i, 4){ s = min(s, dist(d[i])); } rep(i, 4){ if(s == dist(d[i]))sh.push_back(i); } int ans = 0; rep(i, sh.size()){ ans += cost[d[sh[i]].Y][d[sh[i]].X]%100000007; } cout << ans%100000007 << endl; return 0; }
#include <iostream> using namespace std; int getLCM(int s, int t) { int tmp; if (s < t) { tmp = t; t = s; s = tmp; } while (s % t != 0) { tmp = s % t; s = t; t = tmp; } return t; } int main(int argc, char *argv[]) { int r, c, a1, a2, b1, b2; cin >> r >> c >> a1 >> a2 >> b1 >> b2; int hoge = 1; int x; if (a1 > b1) if (a1 - b1 < r - a1 + b1) { x = a1 - b1; } else if (a1 - b1 > r - a1 + b1) { x = r - a1 + b1; } else { x = a1 - b1; hoge *= 2; } else if (b1 - a1 < r - b1 + a1) { x = b1 - a1; } else if (b1 - a1 > r - b1 + a1) { x = r - b1 + a1; } else { x = b1 - a1; hoge *= 2; } int y; if (a2 > b2) if (a2 - b2 < c - a2 + b2) { y = a2 - b2; } else if (a2 - b2 > c - a2 + b2) { y = c - a2 + b2; } else { y = a2 - b2; hoge *= 2; } else if (b2 - a2 < c - b2 + a2) { y = b2 - a2; } else if (b2 - a2 > c - b2 + a2) { y = c - b2 + a2; } else { y = b2 - a2; hoge *= 2; } int *deno, *nume; deno = new int[x]; nume = new int[x]; for (int i = 0; i < x; i++) { nume[i] = x + y - i; deno[i] = x - i; } for (int i = 0; i < x; i++) for (int j = 0; j < x; j++) { int lcm = getLCM(nume[i], deno[j]); nume[i] /= lcm; deno[j] /= lcm; } /* for (int i = 0; i < x; i++) cout << nume[i] << endl; for (int i = 0; i < x; i++) cout << deno[i] << endl; */ long long result = 1; for (int i = 0; i < x; i++) { result *= nume[i]; if (result > 100000007) result %= 100000007; } result = result * hoge % 100000007; cout << result << endl; return 0; }
#include <iostream> #include <algorithm> using namespace std; typedef long long int lli; const int MAX = 1001; const lli MOD = 100000007; int main(){ int h,w,x1,y1,x2,y2; lli C[MAX][MAX]; fill(C[0],C[MAX],1); for(int i=1;i<MAX;i++){ for(int j=1;j<i;j++){ C[i][j] = (C[i-1][j] + C[i-1][j-1]) % MOD; } } while(cin >> w >> h >> x1 >> y1 >> x2 >> y2){ int x,y; lli ansx = 1, ansy = 1; if(abs(x2-x1) == abs(w-x2)+x1) ansx++; x = min(abs(x2-x1), abs(w-x2)+x1); if(x == abs(w-x1)+x2) ansx++; else if(x > abs(w-x1)+x2){ ansx = 1; x = abs(w-x1)+x2; } if(abs(y2-y1) == abs(h-y2)+y1) ansy++; y = min(abs(y2-y1), abs(h-y2)+y1); if(y == abs(h-y1)+y2) ansy++; else if(y > abs(h-y1)+y2){ ansy = 1; y = abs(h-y1)+y2; } cout << C[x+y][x] * ansx * ansy % MOD << endl; } }
#include <iostream> #include <algorithm> using namespace std; typedef unsigned long ul; const static ul LIMIT = 100000007; ul p[1001] = {1}; int main() { short r, c, a1, a2, b1, b2; cin >> r >> c >> a1 >> a2 >> b1 >> b2; short w = min(abs(b1 - a1), r - abs(b1 - a1)); short h = min(abs(b2 - a2), c - abs(b2 - a2)); short n = w + h; ul left, tmp; int i, j; for (i = 1; i <= n; i++) { left = 0; for (j = 0; j <= i; j++) { tmp = p[j]; p[j] = (p[j] + left) % LIMIT; left = tmp; } } cout << (p[w] * (w * 2 == r ? 2 : 1) * (h * 2 == c ? 2 : 1)) % LIMIT << endl; return 0; }
#include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <climits> #include <cfloat> using namespace std; const int MOD = 100000007; void combination(int n, vector<vector<long long> >& comb) { comb.assign(n+1, vector<long long>(n+1, 0)); for(int i=0; i<=n; ++i){ comb[i][0] = 1; for(int j=1; j<=i; ++j){ comb[i][j] = comb[i-1][j-1] + comb[i-1][j]; comb[i][j] %= MOD; } } } int main() { vector<vector<long long> > comb; combination(1000, comb); int h, w, a1, a2, b1, b2; cin >> h >> w >> a1 >> a2 >> b1 >> b2; int dy = min(abs(a1 - b1), h - abs(a1 - b1)); int dx = min(abs(a2 - b2), w - abs(a2 - b2)); long long ret = comb[dy+dx][dy]; if(dy * 2 == h) ret *= 2; if(dx * 2 == w) ret *= 2; ret %= MOD; cout << ret << endl; return 0; }
//Name: Grid //Level: 2 //Category: 数学,動的計画法,DP //Note: /* */ #include <iostream> #include <vector> #include <algorithm> using namespace std; const int MOD = 100000007; int memo[1001][1001]; int comb(int n, int r) { if(r == 0) return 1; if(n == r) return 1; if(n < r) return 0; if(memo[n][r] != 0) return memo[n][r]; return memo[n][r] = (comb(n-1, r) + comb(n-1, r-1)) % MOD; } int main() { cin.tie(0); ios::sync_with_stdio(0); int R, C; pair<int,int> start, goal; while(cin >> R >> C >> start.first >> start.second >> goal.first >> goal.second) { const int left = (start.second - goal.second+C) % C; const int right = (goal.second - start.second+C) % C; const int up = (start.first - goal.first+R) % R; const int down = (goal.first - start.first+R) % R; const int mc = min(left, right); const int mr = min(up, down); int pat = comb(mc+mr, mc); if(left != 0 && left == right) pat = (pat*2) % MOD; if(up != 0 && up == down) pat = (pat*2) % MOD; cout << pat << endl; } return 0; }
#include<iostream> #include<vector> #include<queue> using namespace std; int inf = 100000; int w, h; pair<int, int> ch(pair<int, int> a) { if (a.first == -1) { a.first = w - 1; } else if (a.first == w) { a.first = 0; } if (a.second == -1) { a.second = h - 1; } else if (a.second == h) { a.second = 0; } return a; } class point { public: int fast, num; bool used; point() :fast(inf), num(0), used(false) { } }; int main() { cin >> w >> h; vector<vector<point> > map(w, vector<point>(h)); int mx[]{ 1,0,-1,0 }, my[]{ 0,1,0,-1 }; int a, b, c, d; cin >> a >> b >> c >> d; map[a][b].num = 1; map[a][b].fast = 0; queue<pair<int, int> >que; que.push(pair<int, int>(a, b)); while (!que.empty()) { pair<int,int> du = que.front(); que.pop(); pair<int, int> du2; for (int i = 0; i < 4; ++i) { du2 = ch(pair<int, int>(du.first + mx[i], du.second + my[i])); if (map[du2.first][du2.second].fast > map[du.first][du.second].fast) { map[du2.first][du2.second].num = (map[du2.first][du2.second].num + map[du.first][du.second].num) % 100000007; if (!map[du2.first][du2.second].used) { que.push(du2); map[du2.first][du2.second].fast = map[du.first][du.second].fast + 1; map[du2.first][du2.second].used = true; } } } } cout << map[c][d].num << endl; return 0; }
#include <bits/stdc++.h> #define r(i,n) for(int i=0;i<n;i++) using namespace std; typedef long long ll; const ll M = 100000007; ll extgcd(ll a,ll b,ll& x,ll& y){ ll d = a ; if ( b != 0 ) { d = extgcd( b , a%b , y , x ) ; y -= ( a / b ) * x ; }else{ x = 1 ; y = 0 ; } return d; } ll mod_inverse(ll a,ll m){ ll x , y ; extgcd( a , m , x , y ) ; return ( m + x % m ) % m ; } ll combination(ll N,ll K){ ll res = 1; for( int i = 0; i < K; i++ ) res = (res * ((N - i) % M )) % M; for( int i = 1; i <= K; i++ ) res = (res * mod_inverse(i,M)) % M; return res; } int dx[]={-1,-1,-1,0,0,0,1,1,1}; int dy[]={-1,0,1,-1,0,1,-1,0,1}; int main(){ int sum=0,h,w,a1,a2,b1,b2,MIN=1e9; cin>>h>>w>>a1>>a2>>b1>>b2; r(i,9){ int y=b1+h*dy[i]; int x=b2+w*dx[i]; MIN=min(MIN,abs(y-a1)+abs(x-a2)); } r(i,9){ int y=b1+h*dy[i]; int x=b2+w*dx[i]; if(abs(y-a1)+abs(x-a2)==MIN){ sum+=combination(abs(y-a1)+abs(x-a2),abs(y-a1)); sum%=M; } } cout<<sum<<endl; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <climits> #include <cfloat> #include <ctime> #include <cassert> #include <map> #include <utility> #include <set> #include <iostream> #include <memory> #include <string> #include <vector> #include <algorithm> #include <functional> #include <sstream> #include <complex> #include <stack> #include <queue> #include <numeric> #include <list> #include <iomanip> #include <fstream> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it) #define rforeach(it, c) for (__typeof__((c).rbegin()) it=(c).rbegin(); it != (c).rend(); ++it) #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define CL(arr, val) memset(arr, val, sizeof(arr)) #define COPY(dest, src) memcpy(dest, src, sizeof(dest)) #define ten(n) ((long long)(1e##n)) #define bin(n) (1LL << (n)) template <class T> void max_swap(T& a, const T& b) { a = max(a, b); } template <class T> void min_swap(T& a, const T& b) { a = min(a, b); } template <class T> void sort(vector<T>& c) { sort(c.begin(), c.end()); } template <class T> void uniq(vector<T>& c) { sort(c.begin(), c.end()); c.erase(unique(c.begin(), c.end()), c.end()); } template <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); } template <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; } template <class T, class U> ostream& operator<<(ostream& os, pair<T, U>& p) { os << "( " << p.first << ", " << p.second << " )"; return os; } template <class T> void print(T a, int n, const string& deli = " ", int br = 1) { for (int i = 0; i < n; ++i) { cout << a[i]; if (i + 1 != n) cout << deli; } while (br--) cout << endl; } template <class T> void print(const T& c, const string& deli = " ", int br = 1) { foreach (it, c) { cout << *it; if (++it != c.end()) cout << deli;--it; } while (br--) cout << endl; } template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; } template <class T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; } template <class T> void input(T* a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; } void fast_io() { cin.tie(0); ios::sync_with_stdio(false); } #define trace(x) (cout << #x << ": " << (x) << endl) bool valid(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; } typedef long long ll; typedef pair<int, int> pint; const int dx[] = { 0, 1, 0, -1 }; const int dy[] = { 1, 0, -1, 0 }; const double PI = acos(-1.0); const int mod = 1000000007; int main() { int r, c, x1, y1, x2, y2; cin >> r >> c >> y1 >> x1 >> y2 >> x2; const int mod = ten(8) + 7; int dp[1111][1111] = {}; dp[0][0] = 1; for (int i = 0; i <= r; ++i) { for (int j = 0; j <= c; ++j) { (dp[i + 1][j] += dp[i][j]) %= mod; (dp[i][j + 1] += dp[i][j]) %= mod; } } if (x1 > x2) swap(x1, x2); if (y1 > y2) swap(y1, y2); int xd[] = { x2 - x1, x1 + (c - x2) }; int yd[] = { y2 - y1, y1 + (r - y2) }; int min_d = bin(30); rep(i, 2) rep(j, 2) min_swap(min_d, xd[i] + yd[j]); int res = 0; rep(i, 2) rep(j, 2) if (xd[i] + yd[j] == min_d) (res += dp[xd[i]][yd[j]]) %= mod; cout << res << endl; }
#include <iostream> #include <vector> using namespace std; const int MOD = (int)1e8 + 7; //--------------------------------------------------------------- template<int MOD> struct ModInt { static const int Mod = MOD; unsigned x; ModInt() : x(0) { } ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } int get() const { return (int)x; } ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return ModInt(u); } bool operator==(ModInt that) const { return x == that.x; } bool operator!=(ModInt that) const { return x != that.x; } ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; } }; template<int MOD> ostream& operator<<(ostream& os, const ModInt<MOD>& mi) { return os << mi.get(); } template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) { ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; } template<typename T, int FAC_MAX> struct Comb { vector<T> fac, ifac; Comb() { fac.resize(FAC_MAX + 1, 1); ifac.resize(FAC_MAX + 1, 1); for (int i = 1; i <= FAC_MAX; i++) fac[i] = fac[i - 1] * i; for (int i = 1; i <= FAC_MAX; i++) ifac[i] = T(1) / fac[i]; } T aPb(int a, int b) { if (b < 0 || a < b) return T(0); return fac[a] * ifac[a - b]; } T aCb(int a, int b) { if (b < 0 || a < b) return T(0); return fac[a] * ifac[a - b] * ifac[b]; } T nHk(int n, int k) { if (n == 0 && k == 0) return T(1); if (n <= 0 || k < 0) return 0; return aCb(n + k - 1, k); } T fact(int n) { if (n < 0) return T(0); return fac[n]; } }; typedef ModInt<MOD> mint; //--------------------------------------------------------------- int main() { Comb<mint, 2000> com; int r, c, sx, sy, tx, ty; cin >> r >> c >> sx >> sy >> tx >> ty; int x = min(abs(sx - tx), r - abs(sx - tx)); int y = min(abs(sy - ty), c - abs(sy - ty)); mint k = 1; if (abs(sx - tx) == r - abs(sx - tx)) k *= 2; if (abs(sy - ty) == c - abs(sy - ty)) k *= 2; mint ans = com.aCb(x + y, x); cout << ans * k << endl; }
#include <iostream> #include <algorithm> #include <cstring> using namespace std; const int MOD = 100000007; int dp[1001][1001]; int main(){ int r, c, a1, a2, b1, b2; cin >> r >> c >> a1 >> a2 >> b1 >> b2; dp[0][0] = 1; for(int i = 0; i < r; ++i){ dp[i][0] = 1; } for(int i = 0; i < c; ++i){ dp[0][i] = 1; } for(int i = 1; i < r; ++i){ for(int j = 1; j < c; ++j){ dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % MOD; } } int xpos, xneg, ypos, yneg; if(a1 <= b1){ xpos = b1 - a1; xneg = r - b1 + a1; }else{ xpos = r - a1 + b1; xneg = a1 - b1; } if(a2 <= b2){ ypos = b2 - a2; yneg = c - b2 + a2; }else{ ypos = c - a2 + b2; yneg = a2 - b2; } int answer = dp[min(ypos, yneg)][min(xpos, xneg)]; if(xpos == xneg){ answer = (answer * 2) % MOD; } if(ypos == yneg){ answer = (answer * 2) % MOD; } cout << answer << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int w, h, pat[1002][1002] = {{}}; int x1, x2, y1, y2; pat[1][1] = 1; for(int i = 1; i < 1002; i++){ for(int j = 1; j < 1002; j++){ if(i != 1 || j != 1) (pat[i][j] = pat[i - 1][j] + pat[i][j - 1]) %= 100000007; } } cin >> w >> h >> x1 >> y1 >> x2 >> y2; int diffx = abs(x2 - x1), diffy = abs(y2 - y1); diffx = min( diffx, w - diffx), diffy = min( diffy, h - diffy); int weight = (1 + (w % 2 == 0 && w / 2 == diffx)) * (1 + (h % 2 == 0 && h / 2 == diffy)); cout << (weight * pat[++diffx][++diffy]) % 100000007 << endl; return(0); }
#include<iostream> #include<vector> using namespace std; typedef long long ll; const int mod = 100000007; struct Combination{ int n; vector<vector<int>> dp; Combination(int i){ n = i; dp = vector<vector<int>>(n+1, vector<int>(n+1, 0)); constructTriangle(); } void constructTriangle(){ dp[0][0] = 1; for(int i = 1; i <= n; i++){ dp[i][0] = dp[i-1][0]; for(int j = 1; j <= i; j++){ dp[i][j] = (dp[i-1][j] + dp[i-1][j-1]) % mod; } } } // return aCb int getCombination(int a, int b){ return dp[a][b]; } }; int main(){ Combination comb(1001); int r, c, ax, ay, bx, by; cin >> r >> c >> ax >> ay >> bx >> by; int mind = 1001; for(int i = -1; i <= 1; i++){ for(int j = -1; j <= 1; j++){ int nx = bx + i*r, ny = by + j*c; mind = min(mind, abs(nx-ax) + abs(ny-ay)); } } ll ans = 0; for(int i = -1; i <= 1; i++){ for(int j = -1; j <= 1; j++){ int nx = bx + i*r, ny = by + j*c; int x = abs(nx-ax), y = abs(ny-ay); if(x+y != mind) continue; ans += comb.getCombination(x+y, x); ans %= mod; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long table[1001][1001]; int main(){ int r,c,a1,a2,b1,b2; cin >> r >> c >> a1 >> a2 >> b1 >> b2; long long times = 1; int al = abs(a1-b1); if(al == r-al) times *= 2; al = min(al,r-al); int bl = abs(a2-b2); if(bl == c-bl) times *= 2; bl = min(bl,c-bl); for(int i = 0; i <= al; i++){ table[i][0] = 1; } for(int i = 0; i <= bl; i++){ table[0][i] = 1; } for(int i = 1; i <= al; i++){ for(int j = 1; j <= bl; j++){ table[i][j] = (table[i-1][j] + table[i][j-1]) % 100000007; } } // cout << al << " " << bl << " " << times << endl; cout << ((table[al][bl] * times) % 100000007) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define all(v) (v).begin(), (v).end() #define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__) #define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++) #define rep(i, n) reps(i, 0, n) template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;} template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;} using Pi = pair<int, int>; using Tapris = tuple<int, int, int>; using vint = vector<int>; const int inf = 1LL << 55; const int mod = 1e9 + 7; struct Combinatorics { using int64 = long long; int64 mod; int64 fact[202020]; int64 invfact[202020]; Combinatorics(int64 mod):mod(mod) { fact[0] = invfact[0] = 1; for(int i = 1; i < 202020; ++i) { fact[i] = fact[i-1]*i%mod; invfact[i] = minv(fact[i]); } } int64 mpow(int64 x, int64 n) const { int64 res = 1; while(n > 0) { if(n&1) res = res*x%mod; x = x*x%mod; n >>= 1; } return res; } int64 minv(int64 x) const { return mpow(x, mod-2); } int64 mfact(int64 x) const { return fact[x]; } int64 C(int64 n, int64 r) const { if(r < 0 || n < r) return 0; return fact[n]*invfact[r]%mod*invfact[n-r]%mod; } int64 P(int64 n, int64 r) const { if(r < 0 || n < r) return 0; return fact[n]*invfact[n-r]%mod; } }C(mod); signed main() { cin.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(12); int r, c, ay, ax, by, bx; cin >> r >> c >> ay >> ax >> by >> bx; int By[3][3], Bx[3][3]; reps(i, -1, 2) reps(j, -1, 2) { By[i+1][j+1] = by+i*r; Bx[i+1][j+1] = bx+j*c; } int mod = 1e8+7; Combinatorics comb(mod); int mn = inf; rep(i, 3) rep(j, 3) { chmin(mn, llabs(ay-By[i][j])+llabs(ax-Bx[i][j])); } int ans = 0; rep(i, 3) rep(j, 3) { int d = llabs(ay-By[i][j])+llabs(ax-Bx[i][j]); if(mn == d) { ans += comb.C(mn, llabs(ay-By[i][j])); ans %= mod; } } cout << ans << endl; return 0; }
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <map> #include <stack> #include <queue> #include <algorithm> #include <set> #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,j) FOR(i,0,j) #define mp std::make_pair const int INF = 1 << 24; const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1}; typedef long long ll; typedef unsigned long long ull; typedef std::pair<int,int> P; const int MOD = 100000007; ll fact[2001]; ll extgcd(ll a, ll b, ll &x, ll &y){ ll d = a; if(b != 0){ d = extgcd(b, a%b, y, x); y -= (a/b) * x; }else{ x = 1; y = 0; } return d; } ll mod_inverse(ll a, ll m){ ll x, y; extgcd(a, m, x, y); return (m + x % m) % m; } ll mod_fact(ll n, ll p, ll& e){ e = 0; if(n == 0){return 1;} // 0! = 1 ll res = mod_fact(n/p, p, e); e += n / p; if(n/p % 2 != 0){return res * (p - fact[n%p]) % p;} return res * fact[n%p] % p; } ll mod_comb(ll n, ll k, ll p){ if(n < 0 || k < 0 || n < k){return 0;} ll e1, e2, e3; ll a1 = mod_fact(n, p, e1), a2 = mod_fact(k, p, e2), a3 = mod_fact(n-k, p, e3); if(e1 > e2 + e3){return 0;} return a1 * mod_inverse(a2 * a3 % p, p) % p; } int main(){ fact[0] = 1; FOR(i, 1, 2001){ fact[i] = (fact[i-1] * i) % MOD; } int r, c, a1, a2, b1, b2; std::cin >> r >> c >> a1 >> a2 >> b1 >> b2; if(a1 > b1){std::swap(a1, b1);} if(a2 > b2){std::swap(a2, b2);} int dx[2], dy[2]; dx[0] = b1-a1; dy[0] = b2-a2; dx[1] = a1 + r - b1; dy[1] = a2 + c - b2; int shortest_path = INF; REP(i, 2){ REP(j, 2){ shortest_path = std::min(shortest_path, dx[i]+dy[j]); } } ll res = 0; REP(i, 2){ REP(j, 2){ if(dx[i] + dy[j] == shortest_path){ res = (res + mod_comb(dx[i]+dy[j], std::min(dx[i],dy[j]), MOD)) % MOD; } } } std::cout << res << std::endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll M = 100000007; ll extgcd(ll a,ll b,ll& x,ll& y){ ll d = a ; if ( b != 0 ) { d = extgcd( b , a%b , y , x ) ; y -= ( a / b ) * x ; }else{ x = 1 ; y = 0 ; } return d; } ll mod_inverse(ll a,ll m){ ll x , y ; extgcd( a , m , x , y ) ; return ( m + x % m ) % m ; } ll combination(ll N,ll K){ ll res = 1; for( int i = 0; i < K; i++ ) res = (res * ((N - i) % M )) % M; for( int i = 1; i <= K; i++ ) res = (res * mod_inverse(i,M)) % M; return res; } int main() { int cnt = 0; int mi = M; int r, c, a1, a2, b1, b2; cin >> r >> c >> a1 >> a2 >> b1 >> b2; for ( int i = -1; i <= 1; i++ ) { for ( int j = -1; j <= 1; j++ ) { int c1 = b1+r*i, c2 = b2+c*j; mi = min(mi, abs(c1-a1)+abs(c2-a2)); } } //cout << combination(400, 200) << endl; //cout << mi << endl; for ( int i = -1; i <= 1; i++ ) { for ( int j = -1; j <= 1; j++ ) { int c1 = b1+r*i, c2 = b2+c*j; if ( abs(c1-a1)+abs(c2-a2) == mi ) { cnt += combination(mi, abs(c1-a1)); //cout << cnt << endl; cnt %= M; } } } cout << cnt << endl; return 0; }
//39 #include<iostream> #include<queue> #include<utility> using namespace std; int main(){ int r,cc,a,aa,b,bb; cin>>r>>cc>>a>>b>>aa>>bb; static int mat[1000][1000]={}; static int p[1000][1000]={}; queue<pair<int,pair<int,int> > > que; que.push(make_pair(1,make_pair(a,b))); mat[a][b]=1; while(p[aa][bb]==0||p[aa][bb]==que.front().first){ pair<int,pair<int,int> > c=que.front(); que.pop(); for(int i=0;i<4;i++){ static int d[]={0,1,0,-1,0}; int dy=(c.second.first+d[i]+r)%r; int dx=(c.second.second+d[i+1]+cc)%cc; if(p[dy][dx]==0||p[dy][dx]==c.first){ mat[dy][dx]=(mat[dy][dx]+mat[c.second.first][c.second.second])%100000007; if(p[dy][dx]==0){ p[dy][dx]=c.first; que.push(make_pair(c.first+1,make_pair(dy,dx))); } } } } cout<<mat[aa][bb]<<endl; return 0; }
#include <iostream> using namespace std; const int DIV = 100000007; int r, c, ax, ay, bx, by, dp[1001][1001]; int abs(int n){ return n<0?-n:n; } void set(){ for(int i = 0; i <= 1000; i++) dp[0][i] = 1; for(int i = 0; i < 1000; i++){ dp[i+1][0] = 1; for(int j = 0; j < 1000; j++){ dp[i+1][j+1] = (dp[i][j+1] + dp[i+1][j])%DIV; } } } int main(){ set(); cin >> r >> c >> ax >> ay >> bx >> by; int m = 1, x, y; int dx = abs(bx-ax), dy = abs(by-ay); if( r%2 == 0 && r/2 == dx ) m *= 2; if( r/2 >= dx ) x = dx; else x = r - dx; if( c%2 == 0 && c/2 == dy ) m *= 2; if( c/2 >= dy ) y = dy; else y = c - dy; cout << (m * dp[x][y])%DIV << endl; return 0; }
#include <iostream> #include <vector> #include <string> #include <cstring> #include <algorithm> #include <sstream> #include <map> #include <set> #include <queue> #define REP(i,k,n) for(int i=k;i<n;i++) #define rep(i,n) for(int i=0;i<n;i++) #define INF 1<<30 #define pb push_back #define mp make_pair #define MOD 100000007 using namespace std; typedef long long ll; typedef pair<int,int> P; bool used[1005][1005]; ll cnt[1005][1005]; int depth[1005][1005]; int r, c; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool can(int x, int y) { if(0 <= x && x < r && 0 <= y && y < c) return true; return false; } int main() { int a1, a2, b1, b2; cin >> r >> c >> a1 >> a2 >> b1 >> b2; memset(used, 0, sizeof(used)); used[a1][a2] = true; memset(cnt, 0, sizeof(cnt)); cnt[a1][a2] = 1; memset(depth, 0, sizeof(depth)); queue<P> que; que.push(mp(a1, a2)); int ans = INF; while(que.size()) { P p = que.front(); que.pop(); int x = p.first; int y = p.second; if(x == b1 && y == b2) { break; } rep(i, 4) { int nx = x + dx[i] + r; int ny = y + dy[i] + c; nx %= r; ny %= c; if(!used[nx][ny]) { cnt[nx][ny] += cnt[x][y]; cnt[nx][ny] %= MOD; used[nx][ny] = true; depth[nx][ny] = depth[x][y] + 1; que.push(mp(nx, ny)); } else if(depth[nx][ny] == depth[x][y] + 1) { cnt[nx][ny] += cnt[x][y]; cnt[nx][ny] %= MOD; } } } cout << cnt[b1][b2] << endl; return 0; }
#include<bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) #define MOD 100000007 using namespace std; typedef pair<int,int>P; int d[1000][1000],ans[1000][1000]; int dx[]{1,-1,0,0},dy[]{0,0,1,-1}; int main(){ int r,c,x1,y1,x2,y2;cin>>r>>c>>x1>>y1>>x2>>y2; queue<P>que; memset(d,-1,sizeof(d)); ans[x1][y1]=1;d[x1][y1]=0;que.push(P(x1,y1)); while(!que.empty()){ P p=que.front();que.pop(); rep(i,4){ int nx=p.first+dx[i],ny=p.second+dy[i]; int a=0; if(nx<0||nx>=r){ nx=(nx+r)%r;a++; } if(ny<0||ny>=c){ ny=(ny+c)%c;a++; } if(a==2)continue; if(d[nx][ny]==-1){ d[nx][ny]=d[p.first][p.second]+1; que.push(P(nx,ny)); } if(d[nx][ny]==d[p.first][p.second]+1) (ans[nx][ny]+=ans[p.first][p.second])%=MOD; } } printf("%d\n",ans[x2][y2]); }
#include <iostream> #include <string> #include <cstdio> #include <vector> #include <numeric> #include <cstdlib> #include <cctype> using namespace std; #define ALL(v) begin(v),end(v) #define REP(i,n) for(int i=0;i<(n);++i) #define RREP(i,n) for(int i=(n)-1;i>=0;--i) typedef long long LL; typedef vector<int> vint; const int targets[4][9] = { {1, 1, 2, 2, 2, 1, 8, 8, 8}, {3, 2, 2, 3, 4, 4, 4, 3, 2}, {5, 5, 4, 4, 4, 5, 6, 6, 6}, {7, 8, 8, 7, 6, 6, 6, 7, 8} }; int getdir(char c){ switch(c){ case 'R': return 0; case 'U': return 1; case 'L': return 2; case 'D': return 3; } abort(); } struct solver{ int n; solver(int n_) : n(n_) {} vector<vint> ids[9]; vint trs[4]; vint unit; const char *p; vint mul(const vint &x, const vint &y){ int sz = x.size(); vint ret(sz); REP(i, sz){ ret[i] = y[x[i]]; } return ret; } vint pow(vint x, LL y){ vint a = unit; while(y){ if(y & 1){ a = mul(a, x); } x = mul(x, x); y >>= 1; } return a; } vint parse(){ vint a = unit; vint b; while(1){ if(*p == '('){ ++p; vint x = parse(); ++p; char *endp; LL r = strtoll(p, &endp, 10); p = endp; b = pow(x, r); } else if(isalpha(*p)){ int dir = getdir(*p); ++p; b = trs[dir]; } else{ break; } a = mul(a, b); } return a; } void solve(){ vector<string> in0(n); REP(i, n){ cin >> in0[i]; } string op; cin >> op; int fstdir = -1; for(char c : op){ if(isalpha(c)){ fstdir = getdir(c); break; } } vector<string> in(n, string(n, '.')); if(fstdir == 0){ REP(y, n){ int u = n - 1; RREP(x, n){ if(in0[y][x] != '.'){ in[y][u--] = in0[y][x]; } } } } else if(fstdir == 1){ REP(x, n){ int u = 0; REP(y, n){ if(in0[y][x] != '.'){ in[u++][x] = in0[y][x]; } } } } else if(fstdir == 2){ REP(y, n){ int u = 0; REP(x, n){ if(in0[y][x] != '.'){ in[y][u++] = in0[y][x]; } } } } else{ REP(x, n){ int u = n - 1; RREP(y, n){ if(in0[y][x] != '.'){ in[u--][x] = in0[y][x]; } } } } REP(i, 9){ ids[i].assign(n, vint(n, -1)); } int id = 0; REP(i, n) REP(j, n){ if(in[i][j] != '.'){ ids[0][i][j] = id; ++id; } } int pcnt = id; REP(i, 4){ trs[i].assign(9 * pcnt, -1); } unit.resize(9 * pcnt); iota(ALL(unit), 0); REP(from, 9){ int to, dir; dir = 0; to = targets[dir][from]; id = to * pcnt; REP(y, n){ int u = n - 1; RREP(x, n){ if(ids[from][y][x] >= 0){ int &r = ids[to][y][u]; --u; if(r == -1){ r = id++; } trs[dir][ids[from][y][x]] = r; } } } dir = 1; to = targets[dir][from]; id = to * pcnt; REP(x, n){ int u = 0; REP(y, n){ if(ids[from][y][x] >= 0){ int &r = ids[to][u][x]; ++u; if(r == -1){ r = id++; } trs[dir][ids[from][y][x]] = r; } } } dir = 2; to = targets[dir][from]; id = to * pcnt; REP(y, n){ int u = 0; REP(x, n){ if(ids[from][y][x] >= 0){ int &r = ids[to][y][u]; ++u; if(r == -1){ r = id++; } trs[dir][ids[from][y][x]] = r; } } } dir = 3; to = targets[dir][from]; id = to * pcnt; REP(x, n){ int u = n - 1; RREP(y, n){ if(ids[from][y][x] >= 0){ int &r = ids[to][u][x]; --u; if(r == -1){ r = id++; } trs[dir][ids[from][y][x]] = r; } } } } p = op.c_str(); vint res = parse(); vector<char> val(9 * pcnt); REP(y, n) REP(x, n){ int k = ids[0][y][x]; if(k >= 0){ val[res[k]] = in[y][x]; } } vector<string> ans(n, string(n, '.')); REP(i, 9) REP(y, n) REP(x, n){ int k = ids[i][y][x]; if(k >= 0 && val[k] != 0){ ans[y][x] = val[k]; } } REP(i, n){ cout << ans[i] << '\n'; } } }; int main(){ int n; while(cin >> n && n){ solver(n).solve(); } }
#include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <climits> #include <cfloat> using namespace std; const double INF = DBL_MAX / 1000; const double EPS = 1.0e-10; int L; vector<double> a; double calculateError(int x, int y) { double vMax = 0.0; double vMin = 1.0; for(int i=x; i<=y; ++i){ vMax = max(vMax, a[i]); vMin = min(vMin, a[i]); } double ret = 0.0; for(int i=x; i<=y; ++i){ int j = (int)((a[i] - vMin) * (L - 1) / (vMax - vMin) + EPS); double q1 = vMin + j * (vMax - vMin) / (L - 1); double q2 = vMin + (j + 1) * (vMax - vMin) / (L - 1); double d = min(abs(q1 - a[i]), abs(q2 - a[i])); d = d * d; ret += d; } return ret; } int main() { for(;;){ int n, m; cin >> n >> m >> L; if(n == 0) return 0; L = 1 << L; a.resize(n); for(int i=0; i<n; ++i) cin >> a[i]; vector<vector<double> > error(n, vector<double>(n)); for(int i=0; i<n; ++i){ for(int j=i+1; j<n; ++j){ error[i][j] = calculateError(i, j); } } vector<vector<double> > dp(n+1, vector<double>(m+1, INF)); dp[0][0] = 0.0; for(int i=0; i<n; ++i){ for(int j=0; j<m; ++j){ for(int k=i+1; k<n; ++k){ dp[k+1][j+1] = min(dp[k+1][j+1], dp[i][j] + error[i][k]); } } } printf("%.10f\n", dp[n][m]); } }
#include<iostream> #include<vector> #include<algorithm> #include<cmath> using namespace std; #define REP(i,b,n) for(int i=b;i<n;i++) #define rep(i,n) REP(i,0,n) #define pb push_back const int N = 300; const double inf = 1000; const double eps=1e-10; double cost[N][N]; double dp[N][N]; double le(vector<double> &tmp,double tar){ int l=0,r=tmp.size()-1; double ret=-inf; while(l <= r){ int mid=(l+r)/2; if (fabs(tmp[mid]-tar)<eps)return tmp[mid]; else if (tmp[mid] > tar)r=mid-1; else l=mid+1,ret=tmp[mid]; } return ret; } double ge(vector<double> &tmp,double tar){ int l=0,r=tmp.size()-1; double ret=inf; while(l <= r){ int mid=(l+r)/2; if (fabs(tmp[mid]-tar)<eps)return tmp[mid]; else if (tmp[mid] > tar)r=mid-1,ret=tmp[mid]; else l=mid+1; } return ret; } void pre(int n,double *in,double div,int L){ rep(i,n){ REP(j,i+1,n){ double maxi=0; double mini=10000000; cost[i][j]=0; for(int k=i;k<=j;k++){ mini=min(mini,in[k]); maxi=max(maxi,in[k]); } double tmp=(maxi-mini)/div; vector<double> hoge; REP(k,1,(1<<L)+1){ hoge.pb(mini+(k-1)*tmp); } sort(hoge.begin(),hoge.end()); for(int k=i;k<=j;k++){ double a=le(hoge,in[k]); double b=ge(hoge,in[k]); a=(a-in[k])*(a-in[k]); b=(b-in[k])*(b-in[k]); cost[i][j]+=min(a,b); } } } } double solve(int n,int M){ rep(i,n+1){ rep(j,M+1)dp[i][j]=inf; } dp[0][0]=0; REP(i,2,n+1){ REP(j,1,M+1){ rep(k,i-1){ dp[i][j]=min(dp[i][j],dp[k][j-1]+cost[k][i-1]); } } } return dp[n][M]; } int main(){ //test(); int n,M,L; double in[N]; while(cin>>n>>M>>L && n){ rep(i,n)cin>>in[i]; pre(n,in,(1<<L)-1,L); printf("%.7lf\n",solve(n,M)); } return false; }
#include <cstdio> #include <algorithm> #include <cmath> #define REP(i,n) for(int i=0; i<(int)(n); i++) using namespace std; const double EPS = 1e-9; double memo[260][260]; double data[260]; int n, m, l; double dp[260][260]; inline double dbl(double a){ return a * a; } int main(){ while(scanf("%d%d%d", &n, &m, &l), n + m + l){ REP(i,n) scanf("%lf", data + i); for(int len = 2; len <= n; len++){ for(int start = 0; start + len <= n; start++){ double mx = *max_element(data + start, data + start + len); double mn = *min_element(data + start, data + start + len); double dst = (mx - mn) / ((1 << l) - 1); double gosa = 0.0; if(dst > EPS){ REP(i,len){ double sa = data[start + i] - mn; int j = (int)floor(sa / dst + 0.5); double tmp = 1e10; for(int ii = j - 1; ii <= j + 1; ii++) tmp = min(tmp, dbl((mn + dst * ii) - data[start + i])); gosa += tmp; } } memo[start][start + len] = gosa; } } const double inf = 1e10; REP(i,n + 1) REP(j, m + 1) dp[i][j] = inf; dp[0][0] = 0.0; REP(i,n) REP(j,m) if(dp[i][j] != inf){ for(int len = 2; i + len <= n; len++) dp[i + len][j + 1] = min(dp[i + len][j + 1], dp[i][j] + memo[i][i + len]); } printf("%.8f\n", dp[n][m]); } return 0; }
#include<iostream> #include<vector> #include<algorithm> #include<cmath> using namespace std; #define REP(i,b,n) for(int i=b;i<n;i++) #define rep(i,n) REP(i,0,n) #define pb push_back const int N = 300; const double inf = 1000; const double eps=1e-10; double cost[N][N]; double dp[N][N]; double le(vector<double> &tmp,double tar){ int l=0,r=tmp.size()-1; double ret=-inf; while(l <= r){ int mid=(l+r)/2; if (fabs(tmp[mid]-tar)<eps)return tmp[mid]; else if (tmp[mid] > tar)r=mid-1; else l=mid+1,ret=tmp[mid]; } return ret; } double ge(vector<double> &tmp,double tar){ int l=0,r=tmp.size()-1; double ret=inf; while(l <= r){ int mid=(l+r)/2; if (fabs(tmp[mid]-tar)<eps)return tmp[mid]; else if (tmp[mid] > tar)r=mid-1,ret=tmp[mid]; else l=mid+1; } return ret; } void pre(int n,double *in,double div,int L){ rep(i,n){ REP(j,i+1,n){ double maxi=0; double mini=10000000; cost[i][j]=0; for(int k=i;k<=j;k++){ mini=min(mini,in[k]); maxi=max(maxi,in[k]); } double tmp=(maxi-mini)/div; vector<double> hoge; REP(k,1,(1<<L)+1){ hoge.pb(mini+(k-1)*tmp); } sort(hoge.begin(),hoge.end()); //rep(k,hoge.size())cout<<hoge[k]<<" " ;cout <<" !!!"<< endl; for(int k=i;k<=j;k++){ double a=le(hoge,in[k]); double b=ge(hoge,in[k]); //cout << a <<" " << in[k] <<" " << b << endl; a=fabs(a-in[k]); b=fabs(b-in[k]); if (a<b)cost[i][j]+=a*a; else cost[i][j]+=b*b; //a=(a-in[k])*(a-in[k]); //b=(b-in[k])*(b-in[k]); //cost[i][j]+=min(a,b); } } } } double solve(int n,int M){ rep(i,n+1){ rep(j,M+1)dp[i][j]=inf; } dp[0][0]=0; REP(i,2,n+1){ REP(j,1,M+1){ rep(k,i-1){ //cout << k<<" " << i-1 <<" " << cost[k][i-1] << endl; dp[i][j]=min(dp[i][j],dp[k][j-1]+cost[k][i-1]); } } } return dp[n][M]; } int main(){ //test(); int n,M,L; double in[N]; while(cin>>n>>M>>L && n){ rep(i,n)cin>>in[i]; pre(n,in,(1<<L)-1,L); printf("%.7lf\n",solve(n,M)); } return false; }
#include <iostream> #include <vector> #include <algorithm> #include <iomanip> #include <cmath> using namespace std; const double INF = 1e18; const int lim = 200000; int main(){ int n; cin >> n; vector<int> a(n); for(int i=0; i<n; i++) cin >> a[i]; vector<double> dp(lim+1, 0); for(int i=0; i<n; i++){ vector<double> ndp(lim+1); for(int j=0; j<=lim; j++){ ndp[j] = max(dp[j], fabs(a[i]-j)/a[i]); } for(int j=1; j<=lim; j++){ for(int k=j*2; k<=lim; k+=j){ ndp[k] = min(ndp[k], ndp[j]); } } dp = ndp; } double ans = INF; for(int i=0; i<=lim; i++){ ans = min(ans, dp[i]); } cout << fixed << setprecision(10); cout << ans << endl; return 0; }
//#include <bits/stdc++.h> #include <iostream> #include <algorithm> #include <bitset> #include <vector> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <stack> #include <queue> #include <deque> #include <cstring> #include <string> #include <utility> #include <array> #include <complex> #include <valarray> #include <cassert> #include <cmath> #include <functional> #include <iomanip> #include <chrono> #include <random> #include <numeric> using namespace std; //#define int long long typedef long long ll; typedef unsigned long long ull; //typedef unsigned __int128 HASH; typedef pair<int,int> pii; typedef pair<ll, ll> pll; typedef pair<ull, ull> pullull; typedef pair<ll,int> plli; typedef pair<double,int> pdi; typedef pair<long double, int> pdbi; typedef pair<int,pii> pipii; typedef pair<ll,pll> plpll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpii; typedef vector<vector<int>> mat; #define rep(i,n) for (int i=0;i<(n);i++) #define rep2(i,a,b) for (int i=(a);i<(b);i++) #define rrep(i,n) for (int i=(n);i>0;i--) #define rrep2(i,a,b) for (int i=(a);i>b;i--) #define pb push_back #define fi first #define se second #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() const ll hmod1 = 999999937; const ll hmod2 = 1000000000 + 9; const int INF = 1<<30; const ll INFLL = 1LL<<62; const long double EPS = 1e-12; const ll mod = 1000000000 + 7; const int dx4[4] = {1, 0, -1, 0}; const int dy4[4] = {0, 1, 0, -1}; const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1}; const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1}; const long double pi = 3.141592653589793; #define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod #define inside(y, x, h, w) (0 <= (y) && (y) < (h) && 0 <= (x) && (x) < (w)) ? true : false //debug #define DEBUG #define DUMPOUT cout #ifdef DEBUG #define dump(...) DUMPOUT<<#__VA_ARGS__<<" :["<<__FUNCTION__<<":"<<__LINE__<<"]"<<endl; DUMPOUT<<" "; dump_func(__VA_ARGS__) #else #define dump(...) #endif void dump_func() {DUMPOUT << endl;}; template <class Head, class... Tail> void dump_func(Head&& head, Tail&&... tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) DUMPOUT << " "; else DUMPOUT << ", "; dump_func(std::move(tail)...); } //ostream template<typename T> ostream& operator << (ostream& os, vector<T>& vec) { os << "["; for (int i = 0; i<vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "]"; return os; } template<typename T, typename U> ostream& operator << (ostream& os, pair<T, U>& pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } template<typename T, typename U> ostream& operator << (ostream& os, map<T, U>& map_var) { os << "["; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << "(" << itr->first << ", " << itr->second << ")"; itr++; if(itr != map_var.end()) os << ", "; itr--; } os << "]"; return os; } template<typename T> ostream& operator << (ostream& os, set<T>& set_var) { os << "["; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if(itr != set_var.end()) os << ", "; itr--; } os << "]"; return os; } int n; double a[25]; double dp[25][200000 + 5]; vector<vector<int>> v(200000 + 5); /* a[1] ~ a[n-1] を a[0]に合わせると考えると, a[0] < a[i] (i = 1, 2, .. n - 1) なので ratio = 1 - x / a[i] を計算すればよく, また今 x = a[0] なので ratio = 1 - a[0] / a[i] < 1 よって ratio の max の min は1未満に抑えられる そしてa[i] <= 100000であることから調べるべき数字のmaxは, xが十分大きいとして ratio = x / a[i] - 1 を考えればよく x / a[i] - 1 < 1 より, x < 2 * a[i] <= 200000 ∴ x < 200000 ratioのmaxのminは1未満に抑えられるから調べるべき数字のminは, xが十分小さいとして ratio = 1 - x / a[i] を考えればよく 1 - x / a[i] < 1 より 0 < x ∴ 0 < x; 以上から調べるべき数字の範囲は 0 < x < 200000 */ signed main(){ cin.tie(0); ios::sync_with_stdio(false); cin >> n; rep(i, n) cin >> a[i]; rep2(i, 1, 200000 + 5) { for (int j = 1; j * j <= i; j++) { if (i % j == 0) { v[i].push_back(j); if (j != i / j) v[i].push_back(i / j); } } } rep(i, n)rep(j, 200000+ 1) dp[i][j] = 1; rep(i, n) { if (i == 0) { rep2(j, 1, 200000 + 1) dp[i][j] = abs(a[i] - j) / a[i]; } else { rep2(j, 1, 200000 + 1) { double ratio = abs(a[i] - j) / a[i]; double mn = 1; for (auto k : v[j]) { mn = min(mn, dp[i - 1][k]); } if (ratio < mn) dp[i][j] = mn; else dp[i][j] = ratio; } } } double ans = 1; rep2(j, 1, 200000 + 1) ans = min(ans, dp[n - 1][j]); cout << fixed << setprecision(20) << ans << endl; }
#include<bits/stdc++.h> /* */ using namespace std; int main() { int N; cin >> N; vector<int>D( N ); for( size_t i = 0; i < N; i++ ) { cin >> D[i]; } double maxans = 1, minans = 0; for( size_t idx = 0; idx < 40; idx++ ) { double midans = ( maxans + minans ) / 2; set<int>prev; prev.insert( 1 ); for( size_t i = 0; i < N; i++ ) { double minvalue = D[i] * ( 1. - midans ); double maxvalue = D[i] * ( 1. + midans ); set<int>next; for( auto one : prev ) { for( int j = minvalue / one + 1; j <= maxvalue / one; j++ ) { next.insert( j*one ); } } prev = next; } if( prev.size() ) { maxans = midans; } else { minans = midans; } } cout << fixed << setprecision( 20 ) << minans << endl; }
#include "bits/stdc++.h" using namespace std; typedef pair<int, int> P; typedef pair<int, P> E; typedef long long LL; int a[100000]; double dp[20][200001]; int main() { int N; cin >> N; for (int i = 0; i < N; i++) cin >> a[i]; for (int i = 0; i < N; i++) for (int j = 0; j <= 2 * a[i]; j++) dp[i][j] = 1e9; for (int i = 1; i <= 2 * a[0]; i++) { dp[0][i] = (double)abs(i - a[0]) / a[0]; } for (int i = 0; i + 1 < N; i++) { for (int j = 1; j <= 2 * a[i]; j++) { for (int k = j; k <= 2 * a[i + 1]; k+=j) { dp[i + 1][k] = min(dp[i + 1][k], max(dp[i][j], (double)abs(k - a[i + 1]) / a[i + 1])); } } } double ans = 1e9; for (int i = 1; i <= 2 * a[N - 1]; i++) ans = min(ans, dp[N - 1][i]); printf("%.15lf\n", ans); }
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<sstream> #include<cmath> #include<climits> #include<string> #include<map> #include<queue> #include<vector> #include<stack> #include<set> using namespace std; typedef long long ll; typedef pair<int,int> pii; #define pb(a) push_back(a) #define INF 0x1f1f1f1f #define lson idx<<1,l,mid #define rson idx<<1|1,mid+1,r #define PI 3.1415926535898 void debug() { #ifdef ONLINE_JUDGE #else freopen("d:\\in.txt","r",stdin); freopen("d:\\out1.txt","w",stdout); #endif } char getch() { char ch; while((ch=getchar())!=EOF) { if(ch!=' '&&ch!='\n')return ch; } return EOF; } double dp[25][200100]; int da[25]; double minn=INF; int n; double f(int k,int fa) { if(dp[k][fa]>=0)return dp[k][fa]; if(k==n+1)return 0; double minx=INF; for(int i=1;fa*i<=200000;i++) { int v=fa*i; if(da[k]<v&&((v-da[k])*1.0/(da[k]*1.0)>minx))break; if(da[k]>v&&((da[k]-v)*1.0/(da[k]*1.0)>minx))continue; minx=min(minx,max(f(k+1,v),abs(v-da[k])*1.0/(da[k]*1.0))); } return dp[k][fa]=minx; } int main() { // int n; while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",&da[i]); memset(dp,-1,sizeof(dp)); double x=f(1,1); printf("%.12lf\n",x); } return 0; }
#include<stdio.h> #include<algorithm> using namespace std; double dp[20][200000]; int b[20]; int ABS(int a){return max(a,-a);} int main(){ int a; scanf("%d",&a); for(int i=0;i<a;i++)scanf("%d",b+i); for(int i=0;i<a;i++) for(int j=0;j<200000;j++) dp[i][j]=999999999; for(int i=1;i<200000;i++) dp[0][i]=(double)ABS(i-b[0])/b[0]; for(int i=0;i<a-1;i++){ for(int j=1;j<200000;j++){ for(int k=j;k<200000;k+=j)dp[i+1][k]=min(dp[i+1][k],max(dp[i][j],(double)ABS(k-b[i+1])/b[i+1])); } } double ret=999999999; for(int i=0;i<200000;i++)ret=min(ret,dp[a-1][i]); printf("%.9f\n",ret); }
#include <cstdio> #include <cstdlib> #include <algorithm> #define REP(i,n) for(int i=0; i<(int)(n); i++) using namespace std; const int MAX = 140000; double memo[20][MAX]; int a[20]; double cr(int old, int next){ return (double)abs(old - next) / old; } int main(){ int n; scanf("%d",&n); REP(i,n) scanf("%d",a+i); const double bad = 1e10; REP(i,n) REP(j,MAX) memo[i][j] = bad; for(int j = 1; j < MAX; j++){ memo[0][j] = cr(a[0], j); } REP(i,n - 1){ REP(j,MAX) if(memo[i][j] != bad){ for(int k = j; k < MAX; k += j){ memo[i+1][k] = min(memo[i+1][k], max(memo[i][j], cr(a[i+1], k))); } } } double ans = 1e10; REP(i,MAX) if(memo[n-1][i] != bad){ ans = min(ans, memo[n-1][i]); } /* REP(i,n){ REP(j,25){ printf("%.2f ", memo[i][j]); } puts(""); } */ printf("%.9f\n", ans); return 0; }
#include "bits/stdc++.h" #include<unordered_map> #include<unordered_set> #pragma warning(disable:4996) using namespace std; using ld = double; template<class T> using Table = vector<vector<T>>; int main() { int N; cin >> N; vector<int>as; for (int i = 0; i < N; ++i) { int a; cin >> a; as.push_back(a); } vector<vector<ld>>dp(N, vector<ld>(200000,10000)); for (int b = 1; b < 3 * as[0]; ++b) { dp[0][b] = ld(abs(b - as[0])) / as[0]; } for (int i = 0; i < N-1; ++i) { for (int j = 1; j < as[i] * 2; ++j) { if (dp[i][j]>1000)continue; for (int k = j; k <= as[i + 1] * 2; k+=j) { dp[i + 1][k] = min(dp[i + 1][k], max(dp[i][j], ld(abs(k - as[i + 1])) / as[i+1])); } } } ld ans = 3; for (int j = 1; j < 2e5; ++j) { ans = min(ans, dp[N-1][j]); } cout << setprecision(22)<<fixed<<ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) #define abs(a) max((a),-(a)) #define rep(i,n) for(int i=0;i<(int)(n);i++) #define repe(i,n) rep(i,(n)+1) #define per(i,n) for(int i=(int)(n)-1;i>=0;i--) #define pere(i,n) rep(i,(n)+1) #define all(x) (x).begin(),(x).end() #define SP <<" "<< #define RET return 0 #define MOD 1000000007 #define INF 1000000000000000000 typedef long long LL; typedef long double LD; int main(){ int n; cin >> n; vector<LD> a(n); for(int i=0;i<n;i++){ cin >> a[i]; } vector<vector<LD>> dp(n,vector<LD>(200001,100000000)); for(int i=1;i<=200000;i++) dp[0][i]=0; for(int i=0;i<n-1;i++){ for(int j=1;j<=200000;j++){ dp[i][j]=max(dp[i][j],abs(a[i]-j)/a[i]); for(int k=j;k<=200000;k+=j){ dp[i+1][k]=min(dp[i+1][k],dp[i][j]); } } } LD ans=1000000000; for(int i=1;i<=200000;i++){ dp[n-1][i]=max(dp[n-1][i],abs(a[n-1]-i)/a[n-1]); ans=min(ans,dp[n-1][i]); } // for(int i=0;i<n;i++){ // for(int j=1;j<20;j++){ // cout << dp[i][j] << " "; // } // cout << endl; // } cout << fixed << setprecision(12) << ans << endl; return 0; }
#include <bits/stdc++.h> #define N 30 #define M 200000 using namespace std; int n,A[N]; double calc(double a,double b){return abs(a-b)/a;} double DP(){ vector<vector<double> > dp(N,vector<double>(M,1e9)); dp[0][1] = 0; for(int i=0;i<n;i++){ for(int num=1;num<M;num++) for(int j=num;j<M;j+=num) dp[i+1][j] = min(dp[i+1][j],max(calc(A[i],j),dp[i][num])); } double res=1e9; for(int i=0;i<M;i++) res=min(res,dp[n][i]); return res; } int main(){ cin>>n; for(int i=0;i<n;i++)cin>>A[i]; printf("%.10f\n",DP()); return 0; }
#include <vector> #include <algorithm> #include <utility> #include <queue> #include <cstdio> #include <iostream> #include <string> #include <string.h> #include <stdio.h> #include <complex> using namespace std; typedef pair<int, int> PII; typedef pair<int, PII> TIII; #define PB push_back #define MP make_pair #define MT(a,b,c) MP(a, MP(b, c)) #define T1 first #define T2 second.first #define T3 second.second #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) int a[20]; int n; bool DFS(int k, int m, double r){ int s = ceil(a[k]-a[k]*r); if(s==0)s=1; int e = ceil(a[k]+a[k]*r); for(int p = ((s-1)/m+1)*m; p<e; p+=m) if(k==n-1)return true; else if(DFS(k+1, p, r))return true; return false; } bool C(double r){ return DFS(0,1,r); } int main(){ cin>>n; REP(i,n)scanf("%d", &a[i]); double u=1.0, l=0.0; while(u-l>1e-9){ double mid = (u+l)*0.5; if(C(mid))u=mid; else l = mid; } printf("%.10f\n", u); return 0; }
#include <bits/stdc++.h> #define N 30 using namespace std; int n,A[N]; double calc(double a,double b){return abs(a-b)/a;} double DP(){ unordered_map<int,double> dp[2]; bool f=0; dp[0][1] = 0; for(int i=0;i<n;i++,f=!f){ dp[!f].clear(); for(auto it = dp[f].begin();it!=dp[f].end();it++){ int num = it->first; double cost = it->second; for(int j=num;;j+=num){ if(dp[!f].count(j))dp[!f][j] = min(dp[!f][j],max(calc(A[i],j),cost)); else dp[!f][j] = max(calc(A[i],j),cost); if(j>A[n-1]) break; } } } double res=1LL<<55; for(auto it = dp[f].begin();it!=dp[f].end();it++)res=min(res,it->second); return res; } int main(){ cin>>n; for(int i=0;i<n;i++)cin>>A[i]; printf("%.10f\n",DP()); return 0; }
#include <iostream> #include <vector> #include <cstdio> #include <cstdlib> using namespace std; const double INF = 1e30; int main() { int N; while (cin >> N) { vector<int> a(N); for (int i = 0; i < N; ++i) cin >> a[i]; const int MAX = 200000; vector<vector<double> > dp(N, vector<double>(MAX, INF)); for (int i = 1; i < MAX; ++i) dp[0][i] = abs(a[0] - i) / static_cast<double>(a[0]); for (int i = 1; i < N; ++i) { for (int j = 1; j < MAX; ++j) { for (int k = j; k < MAX; k += j) { dp[i][k] = min(dp[i][k], max(dp[i-1][j], abs(a[i] - k) / static_cast<double>(a[i]))); } } } double ans = INF; for (int i = 0; i < MAX; ++i) ans = min(ans, dp[N-1][i]); printf("%.12f\n", ans); } return 0; }
// Enjoy your stay. #include <bits/stdc++.h> #define ECH(it, A) for (__typeof((A).begin()) it=(A).begin(); it != (A).end(); ++it) #define EPS 1e-9 #define INF 1070000000LL #define MOD 1000000007LL #define fir first #define foreach(it,X) for(auto it=(X).begin();it!=(X).end();it++) #define ite iterator #define mp make_pair #define mt make_tuple #define rep(i,n) rep2(i,0,n) #define rep2(i,m,n) for(int i=m;i<(n);i++) #define pb push_back #define sec second #define sz(x) ((int)(x).size()) using namespace std; typedef istringstream iss; typedef long long ll; typedef pair<ll,ll> pi; typedef stringstream sst; typedef vector<ll> vi; int N,a[22],L[22],H[22]; template<class T> inline T ceil(T x, T y){return (x - 1) / y + 1;} int main(){ cin.tie(0); ios_base::sync_with_stdio(0); cin>>N; rep(i,N)cin>>a[i]; double lo = 0, hi = 1; rep(_,40){ double mi = (lo+hi)/2; rep(i,N){ L[i] = ceil(a[i] * (1 - mi)); H[i] = floor(a[i] * (1 + mi)); } set<int> cur,nx; rep2(i,L[0],H[0]+1){ cur.insert(i); } rep2(i,1,N){ if(sz(cur) == 0)break; nx.clear(); ECH(it, cur){ int t = *it; for(int k = ceil(L[i], t) * t; k <= H[i] / t * t; k += t){ //if(!memo[k]){ nx.insert(k); //memo[k] = 1; //} } } cur = nx; } if(sz(cur)) hi = mi; else lo = mi; } cout<<fixed<<setprecision(16)<<hi<<endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i,x,y) for(int i=(x);i<(y);++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define show(x) #endif typedef long long int ll; typedef pair<int,int> pii; template<typename T> using vec=std::vector<T>; const int inf=1<<30; const long long int infll=1LL<<62; const double eps=1e-9; const int dx[]={1,0,-1,0},dy[]={0,1,0,-1}; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } void solve(){ int n; cin >> n; vector<int> a(n); rep(i,0,n) cin >> a[i]; int maxi=2*1e5; //vector<vector<long double>> dp(n,vector<long double>(maxi,infll)); //rep(i,1,maxi) dp[0][i]=(long double)(abs(a[0]-i))/a[0]; vector<long double> curr(maxi,infll),next(maxi,infll); rep(i,1,maxi) curr[i]=(long double)(abs(a[0]-i))/a[0]; rep(i,0,n-1){ rep(j,1,maxi){ for(int k=j; k<maxi; k+=j){ next[k]=min(next[k],max(curr[j],(long double)(abs(a[i+1]-k))/a[i+1])); } } swap(curr,next); fill(next.begin(),next.end(),infll); } cout << *min_element(curr.begin(),curr.end()) << endl; } int main(){ std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(12); solve(); return 0; }
#include "bits/stdc++.h" using namespace std; #define int long long int mod=1e9+7; const int N_MAX=1e5*2+1; signed main(){ int n; cin>>n; vector<double> a(n); for(int i=0;i<n;i++)cin>>a[i]; vector<vector<double> > dp(n+1,vector<double>(N_MAX,1e9)); for(int i=1;i<N_MAX;i++){ dp[0][i]=abs(a[0]-i)/a[0]; } for(int i=1;i<n;i++){ for(int j=1;j<N_MAX;j++){ for(int k=j;k<N_MAX;k+=j){ dp[i][k]=min(dp[i][k],max(dp[i-1][j],(abs(a[i]-k)/a[i]))); } } } double ans=1e9; for(int i=1;i<N_MAX;i++){ ans=min(ans,dp[n-1][i]); } cout<<fixed<<setprecision(20)<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; namespace { typedef double real; int N; vector<int> A; void input() { cin >> N; A.clear(); A.resize(N); for (int i = 0; i < N; i++) { cin >> A[i]; } } const int L = int(1e5) * 2 + 5; const real INF = 1e9; real calc(int a, int b) { // ある貨幣を価値aから価値bに変更するときのコスト return abs(real(a - b)) / real(a); } void solve() { /* dp[i][j] :: i番の貨幣価値をjにして、0 ~ i番までの貨幣体系がbeautifulになるよう調整する最小コスト */ vector< vector<real> > dp(N, vector<real>(L, INF)); for (int j = 1; j <= 2 * A[0]; j++) { dp[0][j] = calc(A[0], j); } for (int i = 1; i < N; i++) { for (int j = 1; j <= 2 * A[i]; j++) { // コストが1を越えることはないので, もとの価値の高々2倍までみればよい int x = j; real ncost = calc(A[i], j); for (int k = 1; k * k <= x; k++) { if (x % k == 0) { int a = k, b = x / k; dp[i][j] = min(dp[i][j], max(dp[i - 1][a], ncost)); dp[i][j] = min(dp[i][j], max(dp[i - 1][b], ncost)); } } } } real ans = 1.0; for (int j = 1; j <= 2 * A[N - 1]; j++) { ans = min(ans, dp[N - 1][j]); } printf("%.12lf\n", ans); } } int main() { input(); solve(); return 0; }
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #define LINF 0x3f3f3f3f3f3f3f3f #include <cstdio> #include <iostream> #include <sstream> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <limits> #include <map> #include <string> #include <cstring> #include <set> #include <deque> #include <bitset> #include <list> #include <cctype> #include <utility> using namespace std; typedef long long ll; typedef pair <int,int> P; typedef pair <int,P > PP; const static int tx[] = {0,1,1,1,0,-1,-1,-1}; const static int ty[] = {-1,-1,0,1,1,1,0,-1}; static const double EPS = 1e-8; double dp[21][140001]; int main(){ int N; while(~scanf("%d",&N)){ int currency[21]; for(int i=0;i<N;i++){ scanf("%d",&currency[i]); } fill((double*)dp,(double*)dp + 21 * 140001,1e12); dp[0][1] = 0.0; for(int i = 0; i < N; i++){ for(int start = 1; start <= 140000; start++){ if(dp[i][start] >= 1e12) continue; for(int next = start; next <= 140000; next += start){ dp[i+1][next] = min(dp[i+1][next], max(dp[i][start], (double)abs(next - currency[i])/(double)currency[i])); } } } double res = 1e12; for(int next = 1; next <= 140000; next++){ res = min(dp[N][next],res); } printf("%.12lf\n",res); } }
#include <stdio.h> #include <cmath> #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) int a[20]; int n; bool DFS(int k, int m, double r){ int s=ceil(a[k]*(1-r)); int e=ceil(a[k]*(1+r)); for(int p=((s-1)/m+1)*m;p<e;p+=m) if(k==n-1)return true; else if(DFS(k+1,p,r))return true; return false; } int main(){ scanf("%d",&n); REP(i,n)scanf("%d",&a[i]); double u=1.0,l=0.0; REP(i,30){ double m=(u+l)*0.5; if(DFS(0,1,m))u=m; else l=m; } printf("%.9f\n",u); return 0; }
#include <iostream> #include <algorithm> using namespace std; using ll = long long int; int a[20]; double dp[20][200001]; int main() { int N; cin >> N; for (int i = 0; i < N; i++) { cin >> a[i]; } fill((double*)dp, (double*)(dp + 20), 1<<30); for (int i = 1; i < 2 * a[0]; i++) { dp[0][i] = (double)abs(a[0] - i) / a[0]; } double ans = 1; for (int i = 0; i + 1 < N; i++) { for (int j = 1; j < 2 * a[i]; j++) { if (dp[i][j] == 1<<30) continue; for (int k = 1; j*k < 2 * a[i+1]; k++) { dp[i + 1][j*k] = min(dp[i + 1][j*k], max(dp[i][j], (double)abs(j*k - a[i + 1]) / a[i + 1])); } } } for (int i = 1; i < 2 * a[N - 1]; i++) { if (dp[N - 1][i] == 1<<30) continue; ans = min(ans, dp[N - 1][i]); } printf("%.15lf\n", ans); return 0; }
#include<bits/stdc++.h> using namespace std; template<typename T1, typename T2> istream& operator>>(istream& is, pair<T1,T2>& a){ return is >> a.first >> a.second; } template<typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1,T2>& a){ return os << a.first << " "<<a.second; } template<typename T> istream& operator>>(istream& is, vector< T >& vc){ for(int i = 0; i < vc.size(); i++) is >> vc[i]; return is; } template<typename T> ostream& operator<<(ostream& os, vector< T >& vc){ for(int i = 0; i < vc.size(); i++) os << vc[i] << endl; return os; } #define ForEach(it,c) for(__typeof (c).begin() it = (c).begin(); it != (c).end(); it++) #define ALL(v) (v).begin(), (v).end() #define UNQ(s) { sort(ALL(s)); (s).erase( unique( ALL(s)), (s).end());} #define fr first #define sc second typedef pair< int , int > Pi; typedef pair< int , Pi > Pii; typedef long long int64; const double INF = 1e9; int N, a[20]; double dp[20][200001]; int main(){ fill_n( *dp, 20 * 200001, INF); cin >> N; for(int i = 0; i < N; ++i){ cin >> a[i]; } for(int i = 1; i < 200001; ++i){ dp[0][i] = fabs(a[0] - i) / a[0]; } for(int i = 0; i < N - 1; ++i){ for(int j = 1; j < 200001; ++j){ if(dp[i][j] != INF){ for(int k = j; k < 200001; k += j){ dp[i + 1][k] = min( dp[i + 1][k], max( dp[i][j], fabs( a[i + 1] - k) / a[i + 1])); } } } } cout << fixed << setprecision(10) << *min_element( dp[N - 1], dp[N]) << endl; }
#include <vector> #include <iostream> #include <algorithm> #pragma warning(disable : 4996) using namespace std; int n, a[20], t; double dp[20][200000]; vector<int> s[200000]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]), t = max(t, a[i]); for (int i = 1; i < 2 * t; i++) { for (int j = i; j < 2 * t; j += i) { s[j].push_back(i); } } for (int i = 1; i < 2 * t; i++) dp[0][i] = 1.0 * abs(a[0] - i) / a[0]; for (int i = 1; i < n; i++) { for (int j = 1; j < 2 * t; j++) { dp[i][j] = 1.0; for (int k = 0; k < s[j].size(); k++) { dp[i][j] = min(dp[i][j], max(dp[i - 1][s[j][k]], 1.0 * abs(a[i] - j) / a[i])); } } } double ret = 1.0; for (int i = 1; i < 2 * t; i++) { ret = min(ret, dp[n - 1][i]); } printf("%.9f\n", ret); return 0; }
#include <bits/stdc++.h> using namespace std; #define MAX_N 21 #define MAX_M 300001 #define INF (1<<29) typedef long long ll; double dp[MAX_N][MAX_M]; int main() { int N; cin >> N; vector<double> v(N); for (int i = 0; i < N; i++) { cin >> v[i]; } fill(dp[0], dp[MAX_N], INF); dp[0][1] = 0; for (int i = 0; i < N; i++) { for (int j = MAX_M-1; j >= 0; j--) { if (dp[i][j] == INF) continue; for (int k = 1; k <= MAX_M; k++) { ll nv = (ll)j * k; if (nv >= MAX_M) break; dp[i+1][nv] = min(dp[i+1][nv], max(dp[i][j], abs(v[i]-nv) / v[i])); } } } double res = INF; for (int i = 0; i < MAX_M; i++) { res = min(res, dp[N][i]); } printf("%.15f\n", res); return 0; }
#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <string> #include <vector> #include <queue> #include <stack> #include <map> #include <set> using namespace std; const int maxn = 22; const int maxm = 151111; double dp[maxn][maxm]; int a[maxn]; double rat (int x, int y) { return 1.0 * abs (x - y) / x; } int main () { int n, i, j, k; double ans, Min; while (~scanf ("%d", &n)) { for (i = 1; i <= n; ++i) scanf ("%d", &a[i]); memset (dp, 0x7f, sizeof (dp)); for (i = 1; i < maxm; ++i) { dp[1][i] = rat(a[1], i); } for (i = 2; i <= n; ++i) { //Min = dp[0][0]; for (j = 1; j < maxm; ++j) { for (k = 1; k * j < maxm; ++k) { dp[i][k * j] = min (dp[i][k * j], max(dp[i-1][j], rat (a[i], k * j))); //printf ("%lf %lf %lf\n", dp[i][k * j], dp[i-1][j], rat (a[i], k * j)); //Min = min (Min, dp[i][k * j]); } } //printf ("Min: %lf\n", Min); } ans = dp[n][1]; for (j = 1; j < maxm; ++j) { ans = min (ans, dp[n][j]); } printf ("%.12lf\n", ans); } return 0; }
#include <cstdio> #include <iostream> #define N 25 #define inf 1 #define db double #define swap(T, a, b) ({T ttt = a; a = b; b = ttt;}) using namespace std; int n, a[N], L, R; db t = 0, Ans = inf; int abs(int x) { return x > 0 ? x : -x; } db Up(int k, int x) { if (k == n + 1) return 0; int L = x, R = 2*a[k] - 1, j = x; while (j + x <= R) j += x; db ans = inf, t; for (; j >= L; j -= x) { t = (db)abs(j - a[k]) / a[k]; if (t < Ans) ans = min(ans, max(t, Up(k + 1, j))); } return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i] != 1) Ans = min(Ans, (db)(a[i] - 1) / a[i]); } int L = 1, R =2*a[1] - 1; db t; for (int j = R; j >= L; j--) { t = (db)abs(a[1] - j) / a[1]; if (t > Ans) continue; t = max(t, Up(2, j)); Ans = min(Ans, t); } if (Ans == inf) Ans = 0; printf("%.12lf", Ans); return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long typedef vector<int>vint; typedef pair<int,int>pint; typedef vector<pint>vpint; #define rep(i,n) for(int i=0;i<(n);i++) #define reps(i,f,n) for(int i=(f);i<(n);i++) #define all(v) (v).begin(),(v).end() #define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++) #define pb push_back #define mp make_pair #define fi first #define se second 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;} int N; int A[20]; double dp[2][200010]; signed main(){ cin>>N;rep(i,N)cin>>A[i]; fill_n(*dp,2*200010,1001001001); dp[0][1]=0; rep(i,N){ reps(j,1,200000+1){ for(int k=j;k<=200000;k+=j){ double tmp=(double)abs(A[i]-k)/A[i]; chmin(dp[(i+1)&1][k],max(dp[i&1][j],tmp)); } dp[i&1][j]=1001001001; } } double mi=1001001001; reps(j,1,200000+1)chmin(mi,dp[N&1][j]); printf("%.20f\n",mi); return 0; }
#include <bits/stdc++.h> #define r(i,n) for(int i=0;i<n;i++) using namespace std; double dp[22][200002],c[22],ans=1e9; int n; vector<int>v[200002]; void init(){ r(i,21)r(j,200002)dp[i+1][j]=1e9; r(j,200002)dp[0][j]=abs(c[0]-j)/c[0]; for(int i=1;i<200002;i++){ for(int j=1;j*j<=i;j++){ if(i%j==0){ if(i!=j*j)v[i].push_back(j); v[i].push_back(i/j); } } } } int main(){ cin>>n; r(i,n)cin>>c[i]; init(); for(int i=1;i<n;i++){ r(j,200002){ r(k,v[j].size()){ int x=v[j][k]; dp[i][j]=min(dp[i][j],max(dp[i-1][x],abs(c[i]-j)/c[i])); if(i==n-1)ans=min(ans,dp[i][j]); } } } printf("%.9f\n",n==1?0:ans); }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> vec(N); for (auto& i : vec) cin >> i; double l = 0., r = 1.0; for (int c = 0; c < 100; c++) { double m = (l + r) / 2.0; set<int> s; s.insert(1); for (int i = 0; i < N; i++) { int lo_n = ceil((1.0 - m) * (double)vec[i]); int up_n = (1.0 + m) * vec[i]; set<int> ns; for (auto j : s) { int v = up_n / j; int u = ceil((double)lo_n / j); for (; u <= v; u++) if (u != 0) ns.insert(j * u); } s = ns; } if (s.size() > 0) r = m; else l = m; } printf("%.10f\n", l); return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <cstdio> #include <cmath> using namespace std; #define EPS (1e-10) #define EQ(a,b) (abs((a)-(b))<EPS) double dp[21][200001]; const double INF=(1LL<<50); int a[21]; int n; double dfs(int idx,int lastNum){ if(idx==-1) return 0.0; if(!EQ(INF,dp[idx][lastNum])) return dp[idx][lastNum]; double minCost=INF; if(idx==n-1) for(int i = 1; i <= 150000; i++) minCost=min(minCost,max(dfs(idx-1,i),(double)abs(a[idx]-i)/a[idx])); else{ // lastNum‚ðŠ„‚èØ‚é”Žš‚Å‘JˆÚ for(int i = 1; i*i <= lastNum; i++){ if(lastNum%i==0){ minCost=min(minCost,max(dfs(idx-1,i),(double)abs(a[idx]-i)/a[idx])); minCost=min(minCost,max(dfs(idx-1,(lastNum/i)),(double)abs(a[idx]-(lastNum/i))/a[idx])); } } } return dp[idx][lastNum]=minCost; } int main(){ for(int i = 0; i < 21; i++) for(int j = 0; j < 200001; j++) dp[i][j]=INF; cin>>n; for(int i = 0; i < n; i++) cin>>a[i]; double res=dfs(n-1,0); printf("%.9f\n",res); return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <cstdio> #include <cmath> using namespace std; #define EPS (1e-10) #define EQ(a,b) (abs((a)-(b))<EPS) double dp[21][150001]; const double INF=(1LL<<50); int a[21]; int n; int maxSize=0; double dfs(int idx,int lastNum){ if(idx==-1) return 0.0; if(!EQ(INF,dp[idx][lastNum])) return dp[idx][lastNum]; double minCost=INF; if(idx==n-1) for(int i = 1; i <= maxSize*2+1; i++){ if(minCost<(double)abs(a[idx]-i)/a[idx]) continue; minCost=min(minCost,max(dfs(idx-1,i),(double)abs(a[idx]-i)/a[idx])); } else{ // lastNum‚ðŠ„‚èØ‚é”Žš‚Å‘JˆÚ for(int i = 1; i*i <= lastNum; i++){ if(lastNum%i==0){ minCost=min(minCost,max(dfs(idx-1,i),(double)abs(a[idx]-i)/a[idx])); minCost=min(minCost,max(dfs(idx-1,(lastNum/i)),(double)abs(a[idx]-(lastNum/i))/a[idx])); } } } return dp[idx][lastNum]=minCost; } int main(){ for(int i = 0; i < 21; i++) for(int j = 0; j < 150001; j++) dp[i][j]=INF; cin>>n; for(int i = 0; i < n; i++){ cin>>a[i]; maxSize=max(maxSize,a[i]); } double res=dfs(n-1,0); printf("%.9f\n",res); return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define INF 210000000 using namespace std; int a[300], b[300]; int n; int abs(int x){ return x < 0 ? -x : x; } int dfs(int now, double radio, int last){ int low = (int)ceil(a[now] - a[now] * radio); int high = (int)ceil(a[now] + a[now] * radio); int st; if (low % last == 0) st = low; else st = (low / last + 1) * last; for(int i = st; i < high; i+=last){ if (now == n - 1) return 1; if (dfs(now + 1, radio, i)) return 1; } return 0; } int check(double x){ return dfs(0, x, 1); } int main(){ int i, j; double ans = 1e10; cin>>n; for(i = 0; i < n; i++) cin>>a[i]; double l = 0, r = 1; while(r - l >= 1e-11){ double mid = (r + l) / 2.0; if (check(mid)) r = mid; else l = mid; } printf("%.10f\n", l); return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <cstdio> #include <cmath> using namespace std; #define EPS (1e-10) #define EQ(a,b) (abs((a)-(b))<EPS) double dp[21][200001]; const double INF=(1LL<<50); int a[21]; int n; double dfs(int idx,int lastNum){ if(idx==-1) return 0.0; if(!EQ(INF,dp[idx][lastNum])) return dp[idx][lastNum]; double minCost=INF; if(idx==n-1) for(int i = 1; i <= 200000; i++){ minCost=min(minCost,max(dfs(idx-1,i),(double)abs(a[idx]-i)/a[idx])); } else // lastNum‚ðŠ„‚èØ‚é”Žš‚Å‘JˆÚ for(int i = 1; i*i <= lastNum; i++){ if(lastNum%i==0){ minCost=min(minCost,max(dfs(idx-1,i),(double)abs(a[idx]-i)/a[idx])); minCost=min(minCost,max(dfs(idx-1,(lastNum/i)),(double)abs(a[idx]-(lastNum/i))/a[idx])); } } return dp[idx][lastNum]=minCost; } int main(){ for(int i = 0; i < 21; i++) for(int j = 0; j < 200001; j++) dp[i][j]=INF; cin>>n; for(int i = 0; i < n; i++) cin>>a[i]; //for(int i = 0; i < n; i++){ // if(i==0){ // for(int j=0; j <=200000;j++) // dp[i+1][j]=min(dp[i+1][j],(double)abs(a[i]-j)/a[i]); // continue; // } // for(int j = 0; j <= 200000; j++){ // //if(j% // } //} double res=dfs(n-1,0); printf("%.9f\n",res); return 0; }
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <cmath> #include <cstdlib> #include <algorithm> #include <iostream> using namespace std; #define INF 0x7fffffff #define Max 200000 #define eps 1e-10 int arr[21]; double dp[22][Max]; double getval(double a, double b) { double temp = (b - a) / a; if (temp < eps) return -temp; else return temp; } double MIN(double a, double b) {return a < b ? a : b;} int main() { int n, i, j, k; while (scanf("%d", &n) == 1) { for (i = 1; i <= n; ++i) scanf("%d", &arr[i]); for (i = 1;i <= Max;i ++) dp[0][i] = INF; dp[0][1] = 0.0; for (i = 1; i <= n; ++i) { for (k = 1;k <= Max;k ++) dp[i][k] = INF; for (j = 1; j <= Max; ++j) { for (k = 1; j * k <= Max; ++k) { double temp = max(getval(arr[i], j * k), dp[i - 1][j]); dp[i][j * k] = min(temp, dp[i][j * k]); } } } double ans = INF; for (i = 1; i <= Max; ++i) ans = min(ans, dp[n][i]); printf("%.12lf\n", ans); } } /* 3 6 11 12 3 6 11 24 3 6 11 30 */
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int gcd(int a,int b) {return b==0?a:gcd(b,a%b);} struct ConfusionRatio { int fm,fz; ConfusionRatio(int a=1,int b=1) { int g=gcd(a,b); fz=a/g; fm=b/g; } bool operator < (const ConfusionRatio &t) const { return (fz*1LL*t.fm-fm*1LL*t.fz)<=0; } bool operator == (const ConfusionRatio &t) { return (fz*1LL*t.fm-fm*1LL*t.fz)==0; } }t[2000005]; int n,a[25]; bool dp[21][200002]; bool check(int id) { int l[25],r[25]; for(int i=0;i<n;++i) { long long delt=t[id].fz*1LL*a[i]/t[id].fm; l[i]=(int)max(1LL,a[i]-delt); r[i]=(int)min(2*a[i]-1LL,a[i]+delt); } memset(dp,0,sizeof(dp)); for(int i=l[0];i<=r[0];++i) dp[0][i]=1; for(int i=0;i<n-1;++i) { for(int j=l[i];j<=r[i];++j) { if(!dp[i][j]) continue; int m1=l[i+1]/j,m2=r[i+1]/j; while(m1*j<l[i+1]) m1++; for(int t=m1;t<=m2;++t) dp[i+1][t*j]=1; } } for(int i=l[n-1];i<=r[n-1];++i) if(dp[n-1][i]) return true; return false; } int main() { //freopen("in.txt","r",stdin); int m=0; scanf("%d",&n); t[m++]=ConfusionRatio(0,1); for(int i=0;i<n;++i) { scanf("%d",&a[i]); for(int j=1;j<a[i];++j) t[m++]=ConfusionRatio(j,a[i]); } sort(t,t+m); int N=0; for(int i=0;i<m;) { t[N++]=t[i]; int j=i+1; while(j<m&&(t[j]==t[i])) j++; i=j; } int l=0,r=N-1; while(l+1<r) { int mid=(l+r)>>1; if(check(mid)) r=mid; else l=mid+1; } if(check(l)) printf("%.12f\n",t[l].fz*1.0/t[l].fm); else printf("%.12f\n",t[r].fz*1.0/t[r].fm); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using P = pair<int, int>; using vi = vector<int>; using vvi = vector<vector<int>>; using vll = vector<ll>; using vvll = vector<vector<ll>>; const double eps = 1e-8; const ll MOD = 1000000007; const int INF = 1000000000; const ll LINF = 1ll<<50; template<typename T> void printv(const vector<T>& s) { for(int i=0;i<(int)(s.size());++i) { cout << s[i]; if(i == (int)(s.size())-1) cout << endl; else cout << " "; } } template<typename T1, typename T2> ostream& operator<<(ostream &os, const pair<T1, T2> p) { os << p.first << ":" << p.second; return os; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); int n; cin >> n; vi a(n); for(int i=0;i<n;++i) { cin >> a[i]; } vector<vector<int>> yakusuu(200001); for(int i=1;i<=200000;++i) { for(int j=1;j*j<=i;++j) { if(i%j == 0) { yakusuu[i].push_back(j); if(j*j != i) yakusuu[i].push_back(i/j); } } } int sz = 200000; vector<vector<double>> dp(n, vector<double>(sz + 1, 1)); for(int i=1;i<=sz;++i) { dp[n-1][i] = abs(a[n-1] - i) / (double)a[n-1]; } for(int i=n-1;i>=1;--i) { for(int j=1;j<=sz;++j) { if(abs(dp[i][j] - 1) < eps) continue; for(int k=0;k<(int)(yakusuu[j].size());++k) { dp[i-1][yakusuu[j][k]] = min(dp[i-1][yakusuu[j][k]], max(dp[i][j], abs(a[i-1] - yakusuu[j][k]) / (double)a[i-1])); } } } double ans = 1; for(int i=1;i<=sz;++i) { ans = min(ans, dp[0][i]); } cout << ans << endl; }
#include <iostream> #include <algorithm> #include <cstdio> #include <vector> #include <cmath> using namespace std; const double eps = 1e-9; int N; int a[20]; vector<double> v; bool equals(double s, double t) { return fabs(s-t) < eps; } double cost(int s, int t) { return (double)abs(s-t)/s; } bool rec(int bx, int k, double lim) { if(k == N) return true; for(int b = bx; ; b += bx) { double newV = cost(a[k], b); if(newV < lim || equals(newV, lim)) { if(rec(b, k+1, lim)) return true; } else { if(a[k] < b) break; else continue; } } return false; } double solve() { v.push_back(0.0); for(int m = 1; m <= 100000; ++m) { for(int i = 0; i < N; ++i) { v.push_back((double)m/a[i]); } } sort(v.begin(), v.end()); int l = 0; int r = v.size(); int ansi = 0; while(l < r) { int m = (l+r)/2; if(rec(1, 0, v[m])) { ansi = r = m; } else { l = m+1; } } return v[ansi]; } int main() { cin >> N; for(int i = 0; i < N; ++i) cin >> a[i]; printf("%.12f\n", solve()); return 0; }
#include<bits/stdc++.h> using namespace std; struct I{I(){ ios::sync_with_stdio(false); cin.tie(0); cout<<fixed<<setprecision(10); }}init; const int M=2*114514; const double INF=M*100; typedef vector<double> V; typedef vector<V> VV; double diff(double a,double b){ return abs(a-b)/a; } int main(){ int N;cin>>N; V a(N);for(auto &it:a)cin>>it; VV dp(N,V(M,INF)); for(int i=1;i<M;i++)dp[0][i]=diff(a[0],i); for(int i=1;i<N;i++){ for(int j=1;j<M;j++){ for(int k=j;k<M;k+=j) dp[i][k]=min(dp[i][k],max(dp[i-1][j],diff(a[i],k))); } } double res=INF; for(auto &it:dp.back())res=min(res,it); cout<<res<<endl; return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <map> #include <string> #include <queue> using namespace std; #define SZ(v) ((int)(v).size()) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repf(i, a, b) for (int i = (a); i <= (b); ++i) const int maxint = -1u>>1; const int maxm = 200000 + 2; const int maxn = 20 + 1; double f[maxn][maxm]; int n; int a[maxn]; double calc(int a, int b) { return ((double)(abs(b - a)) / a); } int main() { //printf("%lf\n", calc(11, 12)); while (scanf ("%d", &n) != EOF) { for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } sort(a + 1, a + n + 1); for (int i = 0; i <= n; ++i) fill(f[i], f[i] + maxm, 1); f[0][1] = 0; for (int i = 0; i < n; ++i) { for (int j = 1; j < maxm; ++j) { for (int k = j; k < maxm; k += j) { f[i + 1][k] = min(f[i + 1][k], max(f[i][j], calc(a[i + 1], k))); } } } double ans = 1; for (int i = 1; i < maxm; ++i) { ans = min(ans, f[n][i]); } printf("%.12lf\n", ans); } return 0; }
#include <iostream> #include <sstream> #include <string> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <cassert> using namespace std; #define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i) #define REP(i,n) FOR(i,0,n) #define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; } inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; struct S{ int idx, val; double score; S(int i, int v, double s) : idx(i), val(v), score(s) {} bool operator < (const S& st) const { if(score != st.score) return score > st.score; if(idx != st.idx) return idx < st.idx; return val > st.val; } }; int main(){ int N; while(cin>>N && N){ int a[20]; REP(i, N) cin>>a[i]; priority_queue<S> que; FOR(v, 1, 100001) que.push(S(1, v, (double)abs(a[0] - v) / a[0])); while(!que.empty()){ S s = que.top(); que.pop(); if(s.idx == N){ printf("%.12f\n", s.score); break; } for(int k = 1; k * s.val <= 150000; k++){ int nv = k * s.val; que.push(S(s.idx + 1, nv, max(s.score, (double)abs(a[s.idx] - nv) / a[s.idx]))); } } } return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <climits> #include <cfloat> #include <ctime> #include <cassert> #include <map> #include <utility> #include <set> #include <iostream> #include <memory> #include <string> #include <vector> #include <algorithm> #include <functional> #include <sstream> #include <complex> #include <stack> #include <queue> #include <numeric> #include <list> #include <iomanip> #include <fstream> #include <bitset> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it) #define rforeach(it, c) for (__typeof__((c).rbegin()) it=(c).rbegin(); it != (c).rend(); ++it) #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define CL(arr, val) memset(arr, val, sizeof(arr)) #define COPY(dest, src) memcpy(dest, src, sizeof(dest)) #define ten(n) ((long long)(1e##n)) #define bin(n) (1LL << (n)) #define erep(i, n) for (int i = 0; i <= (int)(n); ++i) #define revrep(i, n) for (int i = (n); i >= 0; --i) #define pb push_back template <class T> void chmax(T& a, const T& b) { a = max(a, b); } template <class T> void chmin(T& a, const T& b) { a = min(a, b); } template <class T> void uniq(T& c) { sort(c.begin(), c.end()); c.erase(unique(c.begin(), c.end()), c.end()); } template <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); } template <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; } template <typename T> void print_container(ostream& os, const T& c) { const char* _s = " "; if (!c.empty()) { __typeof__(c.begin()) last = --c.end(); foreach (it, c) { os << *it; if (it != last) cout << _s; } } } template <typename T> ostream& operator<<(ostream& os, const vector<T>& c) { print_container(os, c); return os; } template <typename T> ostream& operator<<(ostream& os, const set<T>& c) { print_container(os, c); return os; } template <typename T> ostream& operator<<(ostream& os, const multiset<T>& c) { print_container(os, c); return os; } template <typename T> ostream& operator<<(ostream& os, const deque<T>& c) { print_container(os, c); return os; } template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& c) { print_container(os, c); return os; } template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << "( " << p.first << ", " << p.second << " )"; return os; } template <class T> void print(T a, int n, const string& deli = " ", int br = 1) { for (int i = 0; i < n; ++i) { cout << a[i]; if (i + 1 != n) cout << deli; } while (br--) cout << endl; } template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; } template <class T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; } template <class T> void input(T* a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; } void fix_pre(int n) { cout.setf(ios::fixed, ios::floatfield); cout.precision(10); } void fast_io() { cin.tie(0); ios::sync_with_stdio(false); } #define trace(x) (cout << #x << ": " << (x) << endl) bool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; } typedef long long ll; typedef pair<int, int> pint; // y(v): v>^< y(^): ^>v< const int dx[] = { 0, 1, 0, -1 }; const int dy[] = { 1, 0, -1, 0 }; const double PI = acos(-1.0); #define mp make_pair int main() { int n, a[22]; cin >> n; input(a, n); const int m = 2 * ten(5) + 100; static double dp[22][m + 100]; erep(i, n) rep(j, m) dp[i][j] = 1e60; dp[0][1] = 0; rep(i, n) for (int j = 1; j < m; ++j) for (int nj = j; nj < m; nj += j) chmin(dp[i + 1][nj], max(dp[i][j], (double)abs(nj - a[i]) / a[i])); printf("%.10f\n", *min_element(dp[n], dp[n] + m)); }
#include<cstdio> #include<algorithm> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; int n,a[21]; bool isOK(int i,double ub){ if(i==n) return true; bool f; int tmp; for(int b=(a[i]+a[i-1]-1)/a[i-1]*a[i-1];abs(a[i]-b)<ub*a[i];b+=a[i-1]){ tmp=a[i]; a[i]=b; f=isOK(i+1,ub); a[i]=tmp; if(f) return true; } for(int b=a[i]/a[i-1]*a[i-1];abs(a[i]-b)<ub*a[i];b-=a[i-1]){ if(b<a[i-1]) break; tmp=a[i]; a[i]=b; f=isOK(i+1,ub); a[i]=tmp; if(f) return true; } return false; } int main(){ scanf("%d",&n); rep(i,n) scanf("%d",a+i+1); a[0]=1; n++; double lo=0,hi=0; for(int i=1;i<n-1;i++){ int prev=a[i]/a[i-1]*a[i-1]; hi=max(hi,(double)abs(a[i+1]-a[i+1]/prev*prev)/a[i+1]); } rep(_,100){ double mi=(lo+hi)/2; if(isOK(1,mi)) hi=mi; else lo=mi; } printf("%.15f\n",lo,hi); return 0; }
#include <cstdio> #include <cstring> #include <vector> #include <cmath> #include <algorithm> #define INF 1e10 using namespace std; int n; int a[22]; double dp[200001][21]; int main(void){ scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } for(int i=0;i<=200000;i++){ dp[i][0]=0; for(int j=1;j<=n;j++){ dp[i][j]=INF; } } for(int i=1;i<=200000;i++){ for(int j=0;j<n;j++){ double cost=(double)abs(a[j]-i)/a[j]; for(int k=1;k*k<=i;k++){ if(i%k==0){ dp[i][j+1]=min(dp[i][j+1],dp[i/k][j]); dp[i][j+1]=min(dp[i][j+1],dp[k][j]); } } dp[i][j+1]=max(dp[i][j+1],cost); } } double res=INF; for(int i=1;i<=200000;i++){ res=min(res,dp[i][n]); } printf("%.10f\n",res); return 0; }
#include<iostream> #include<cstdio> using namespace std; double ans,radio; int ori[22],a[22],n; void dfs(int i,double maxr) { if (i==n){ans=maxr;return;} int add,sub,now=a[i]; if (a[i]>a[i-1]) { add=a[i-1]-now%a[i-1]; sub=a[i]%a[i-1]; while (true) { a[i]=now-sub; if (double(sub)/(double)ori[i]>ans||a[i]<a[i-1])break; dfs(i+1,max(maxr,double(sub)/(double)ori[i])); sub+=a[i-1]; } } else add=a[i-1]-a[i]; while (true) { if ((double)add/(double)ori[i]>ans)break; a[i]=now+add; dfs(i+1,max(maxr,(double)add/(double)ori[i])); add+=a[i-1]; } a[i]=now; } int main() { while (scanf("%d",&n)!=EOF) { for (int i=0;i<n;i++) { scanf("%d",&ori[i]); a[i]=ori[i]; } ans=1e18; dfs(1,0); int add=1; while (true) { radio=(double)add/(double)ori[0]; if (radio>ans)break; a[0]=ori[0]+add; dfs(1,radio); a[0]=ori[0]-add; dfs(1,radio); add++; } printf("%.10lf\n",ans); } return 0; }
#include <bits/stdc++.h> using namespace std; #define INF 0x33433433 #define M 1100000 int n; int as[114514]; int ls[114514]; int rs[114514]; bool dp[21][M+1]; bool check(double x) { fill(dp[0], dp[n], false); ls[0] = max(1, (int)ceil(as[0]*(1-x))); rs[0] = min(M, (int)floor(as[0]*(1+x))); for (int i=ls[0]; i<=rs[0]; i++) dp[0][i] = true; for (int i=1; i<n; i++) { ls[i] = max(1, (int)ceil(as[i]*(1-x))); rs[i] = min(M, (int)floor(as[i]*(1+x))); for (int j=ls[i-1]; j<=rs[i-1]; j++) { int p = (ls[i]/j + (ls[i]%j!=0)) * j; for (int k=p; k<=rs[i]; k+=j) { dp[i][k] |= dp[i-1][j]; } } } bool ret = false; for (int j=ls[n-1]; j<=rs[n-1]; j++) ret |= dp[n-1][j]; return ret; } int main() { scanf("%d", &n); for (int i=0; i<n; i++) { scanf("%d", &as[i]); } double low = 0.0; double high = 1; for (int i=0; i<28; i++) { double mid = (low+high)/2; if (check(mid)) high = mid; else low = mid; } printf("%.15f\n", high); }
#include <bits/stdc++.h> using namespace std; #define FOR(i,k,n) for(int i = (k); i < (n); i++) #define REP(i,n) FOR(i,0,n) #define ALL(a) a.begin(), a.end() #define MS(m,v) memset(m,v,sizeof(m)) #define D10 fixed<<setprecision(10) typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; const int MOD = 1000000007; const int INF = MOD + 1; const ld EPS = 1e-12; template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); } /*--------------------template--------------------*/ int n; vi v; double dp[22][222222]; ld solve(int i, int t) { if (i == n) return 0; if (dp[i][t] >= 0) return dp[i][t]; ld tmp = (abs(t - v[i]) / (ld)v[i]); ld res = INF; for (int j = 1;; j++) { ld nx = t*j; if (nx > v.back()*2) break; chmin(res, max(solve(i + 1, nx), tmp)); } return dp[i][t] = res; } int main() { MS(dp, -1); cin >> n; v.resize(n); REP(i, n) cin >> v[i]; ld ans = INF; FOR(i, 1, v.back()+1) { chmin(ans, solve(0, i)); } cout << D10 << ans << endl; return 0; }
#include<iostream> #include<cstdio> using namespace std; double ans; int ori[22],a[22],n; void dfs(int i,double maxr) { if (i==n){ans=maxr; return;} int add,sub,now=a[i]; if (a[i]>a[i-1]) { add=a[i-1]-now%a[i-1]; sub=a[i]%a[i-1]; while (true) { a[i]=now-sub; if (double(sub)/(double)ori[i] > ans || a[i]<a[i-1])break; dfs(i+1,max(maxr,double(sub)/(double)ori[i])); sub+=a[i-1]; } } else add=a[i-1]-a[i]; while (true) { a[i]=now+add; if ((double)add/(double)ori[i] > ans)break; dfs(i+1,max(maxr,(double)add/(double)ori[i])); add+=a[i-1]; } a[i]=now; } void solve() { ans=1e18; dfs(1,0); int add=1; double radio; while (true) { radio=(double)add/(double)ori[0]; if (radio>ans)break; a[0]=ori[0]+add; dfs(1,radio); a[0]=ori[0]-add; dfs(1,radio); add++; } } int main() { while (scanf("%d",&n)!=EOF) { for (int i=0;i<n;i++) { scanf("%d",&ori[i]); a[i]=ori[i]; } solve(); printf("%.10lf\n",ans); } return 0; }
// g++ -std=c++11 a.cpp #include<iostream> #include<vector> #include<string> #include<algorithm> #include<map> #include<set> #include<unordered_map> #include<utility> #include<cmath> #include<random> #include<cstring> #include<queue> #include<stack> #include<bitset> #include<cstdio> #include<sstream> #include<iomanip> #include<assert.h> #include<typeinfo> #define loop(i,a,b) for(int i=a;i<b;i++) #define rep(i,a) loop(i,0,a) #define FOR(i,a) for(auto i:a) #define pb push_back #define all(in) in.begin(),in.end() #define shosu(x) fixed<<setprecision(x) #define show1d(v) rep(_,v.size())cout<<" "<<v[_];cout<<endl; #define show2d(v) rep(_,v.size()){rep(__,v[_].size())cout<<" "<<v[_][__];cout<<endl;}cout<<endl; using namespace std; //kaewasuretyuui typedef long long ll; //#define int ll typedef int Def; typedef pair<Def,Def> pii; typedef vector<Def> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef vector<string> vs; typedef vector<double> vd; typedef vector<vd> vvd; typedef pair<Def,pii> pip; typedef vector<pip>vip; // #define mt make_tuple // typedef tuple<int,int,int> tp; // typedef vector<tp> vt; template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;} template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;} //template<class C>constexpr int size(const C &c){return (int)c.size();} //template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;} const double PI=acos(-1); const double EPS=1e-9; Def inf = sizeof(Def) == sizeof(long long) ? 1e18+10 : 1e9+10; // int dx[]={0,1,0,-1,1,1,-1,-1}; // int dy[]={1,0,-1,0,1,-1,1,-1};//RDLU int dx[]={-1,-1,0,0,1,1}; int dy[]={-1,0,-1,1,0,1};//RDLU int main(){ int n; cin>>n; vi in(n); rep(i,n)cin>>in[i]; vvd dp(n,vd(300000,inf)); rep(i,300000)dp[0][i]=(double)abs(in[0]-i)/in[0]; rep(i,n-1)loop(j,1,300000){ for(int k=j;k<300000;k+=j){ cmin(dp[i+1][k],max(dp[i][j],(double)abs(in[i+1]-k)/in[i+1])); } } double out=inf; rep(i,300000)cmin(out,dp[n-1][i]); cout<<shosu(11)<<out<<endl; } /* int main(){ int n,l,t,co=0; cin>>n>>l>>t; vvi in(n,vi(2)); rep(i,n)rep(j,2)cin>>in[i][j]; rep(i,n)if(in[i][1]==2)in[i][1]=-1; vvi g(n,vi(2)); rep(i,n){ if(in[0][1]!=in[i][1]){ ll w=t; co+=w/l; w%=l; ll dis=(l+in[i][0]-in[0][0])%l; if(in[0][1]==-1)dis=(l+in[0][0]-in[i][0])%l; if(dis<=2*w)co++; } g[i]={(in[i][0]+t*in[i][1])%l,in[i][1]}; } if(in[0][1]==-1)co=n-1-co%n; else co%=n; cout<<co<<endl; show2d(in); show2d(g); } */
#include <bits/stdc++.h> using namespace std; constexpr double INF = 1e9; constexpr int MAX = 2e5; int main() { int N; cin >> N; vector<int> a(N); for(int i=0; i<N; ++i) { cin >> a[i]; } vector<vector<double>> dp(N+1, vector<double>(MAX, INF)); for(int i=0; i<MAX; ++i) { dp[0][i] = 0; } for(int i=0; i<N; ++i) { for(int j=1; j<MAX; ++j) { for(int k=1; j*k < MAX; ++k) { dp[i+1][j*k] = min(dp[i+1][j*k], max(dp[i][j], abs((double)a[i] - j*k) / a[i])); } } } cout << fixed << setprecision(10) << *min_element(dp[N].begin(), dp[N].end()) << endl; }
#include <climits> #include <cstdlib> #include <iostream> #include <vector> using namespace std; template<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; } template<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; } inline double calc(int a, int b) { return static_cast<double>(abs(a - b)) / a; } inline vector<int> divisor(int n) { vector<int> res; for(int i = 1; i * i <= n; ++i) { if(n % i == 0) { res.emplace_back(i); if(i != n / i) res.emplace_back(n / i); } } return res; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(10); constexpr int MAX_A = 100000; constexpr int MAX = 2 * MAX_A; int n; cin >> n; vector<int> a(n); for(auto &e : a) cin >> e; vector<vector<int>> d(1); for(int i = 1; i <= MAX; ++i) { d.emplace_back(divisor(i)); } vector<vector<double>> dp(n, vector<double>(MAX + 1, INT_MAX)); for(int i = 1; i <= MAX; ++i) { dp[0][i] = calc(a.front(), i); } for(int i = 1; i < n; ++i) { for(int j = 1; j <= MAX; ++j) { for(const auto &e : d[j]) { chmin(dp[i][j], dp[i - 1][e]); } chmax(dp[i][j], calc(a[i], j)); } } double ans = INT_MAX; for(const auto &e : dp.back()) { chmin(ans, e); } cout << ans << endl; return EXIT_SUCCESS; }
#include <iostream> #include <cstdio> #include <vector> #include <map> #include <algorithm> #include <string.h> #include <cmath> #define MAXN 160100 using namespace std; int N; int a[30]; double f[30][MAXN]; void solve(){ for (int i=1;i<MAXN;++i) f[1][i]=abs(i-a[1])*1.0/a[1]; //for (int i=1;i<MAXN;++i) //printf("%d %.6lf\n",i,f[1][i]); for (int i=2;i<=N;++i) for (int j=1;j<MAXN;++j){ double m=abs(j-a[i])*1.0/a[i]; double tmp=MAXN; int p=sqrt(j); for (int k=1;k<=p;++k) if (j%k==0){ tmp=min(tmp,f[i-1][k]); tmp=min(tmp,f[i-1][j/k]); } f[i][j]=max(m,tmp); } double Ans=MAXN; for (int i=1;i<MAXN;++i) Ans=min(Ans,f[N][i]); printf("%.8lf",Ans); } int main(){ //freopen("input.txt","r",stdin); scanf("%d",&N); for (int i=1;i<=N;++i) scanf("%d",&a[i]); solve(); return 0; }
// Enjoy your stay. #include <bits/stdc++.h> #define EPS 1e-9 #define INF 1070000000LL #define MOD 1000000007LL #define fir first #define foreach(it,X) for(auto it=(X).begin();it!=(X).end();it++) #define ite iterator #define mp make_pair #define mt make_tuple #define rep(i,n) rep2(i,0,n) #define rep2(i,m,n) for(int i=m;i<(n);i++) #define pb push_back #define sec second #define sz(x) ((int)(x).size()) using namespace std; typedef istringstream iss; typedef long long ll; typedef pair<ll,ll> pi; typedef stringstream sst; typedef vector<ll> vi; int N,a[22]; double dp[22][200010]; int main(){ cin.tie(0); ios_base::sync_with_stdio(0); cin>>N; rep(i,N)cin>>a[i]; rep(i,N+1)rep2(j,1,200001)dp[i][j] = i ? INF : 0; rep(i,N)rep2(j,1,200001){ dp[i][j] = max(dp[i][j], i ? fabs(a[i-1] - j)/a[i-1] : 0); for(int k = j; k <= 200000; k += j){ dp[i+1][k] = min(dp[i+1][k], dp[i][j]); } } double ans = INF; rep2(j,1,200001)ans = min(ans, max(dp[N][j], fabs(a[N-1] - j)/a[N-1])); cout<<fixed<<setprecision(16)<<ans<<endl; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < n; i++) #define INF 1000000007 #define DINF 1000000000.0 using namespace std; typedef long long ll; typedef pair<ll,ll> P; typedef pair<ll,P> PP; int n; double a[20]; double dp[20][200001]; int main(){ cin >> n; rep(i,n) cin >> a[i]; rep(i,20) rep(j,200001) dp[i][j] = DINF; for(int i = 1; i <= 200000; i++){ dp[0][i] = abs(i-a[0])/a[0]; } for(int i = 1; i < n; i++){ for(int j = 1; j <= 200000; j++){ for(int k = 1;;k++){ if(k*k > j) break; if(j%k) continue; dp[i][j] = min(dp[i][j],max(dp[i-1][j/k],abs(j-a[i])/a[i])); dp[i][j] = min(dp[i][j],max(dp[i-1][k],abs(j-a[i])/a[i])); } } } double ans = DINF; for(int i = 1; i <= 200000; i++) ans = min(ans,dp[n-1][i]); printf("%.11f\n",ans); }
#include <bits/stdc++.h> using namespace std; double dp[21][200001]; int main() { int N; int A[21]; A[0] = 1; scanf("%d", &N); fill(dp[0], dp[0] + 200001, 200000); for (int i = 1; i <= N; i++){ scanf("%d", A + i); fill(dp[i], dp[i] + 200001, 200000); } dp[0][1] = 0; for (int i = 0; i < N; i++){ for (int j = 1; j <= 200000; j++){ for (int k = j; k <= 200000; k += j){ dp[i + 1][k] = min(dp[i + 1][k], max(dp[i][j], abs(k - A[i + 1]) * 1.0 / A[i + 1])); } } } printf("%.10lf\n", *min_element(dp[N], dp[N] + 200000)); return (0); }
#include <bits/stdc++.h> using namespace std; const int MAX = 2e5; const double INF = 1e9; int main() { int N; cin >> N; vector<int> a(N); for (int i = 0; i < N; i++) { cin >> a[i]; } vector<vector<double>> dp(N + 1, vector<double>(MAX, INF)); for (int i = 1; i < MAX; i++) { dp[0][i] = 0; } for (int i = 0; i < N; i++) { for (int j = 1; j < MAX; j++) { for (int k = j; k < MAX; k += j) { dp[i + 1][k] = min(dp[i + 1][k], max(dp[i][j], (double)abs(k - a[i]) / a[i])); } } } double res = INF; for (int i = 1; i < MAX; i++) { res = min(res, dp[N][i]); } cout << fixed << setprecision(10) << res << endl; return 0; }
#include <cstdlib> #include <cmath> #include <climits> #include <cfloat> #include <cassert> #include <map> #include <utility> #include <set> #include <iostream> #include <memory> #include <string> #include <vector> #include <algorithm> #include <functional> #include <sstream> #include <deque> #include <complex> #include <stack> #include <queue> #include <cstdio> #include <cctype> #include <cstring> #include <ctime> #include <iterator> #include <bitset> #include <numeric> #include <list> #include <iomanip> #include <cassert> #include <array> #include <tuple> #include <initializer_list> #include <unordered_set> #include <unordered_map> #include <forward_list> using namespace std; #define all(c) begin(c), end(c) #define rep(i,n) for(int i = 0; i < (int)(n); ++i) using ll = long long; using pii = pair<int, int>; #define VV(T) vector<vector< T > > int n; int a[25]; double dp[25][200000]; double inf = 1e9; double solve() { fill((double*)begin(dp), (double*)end(dp), inf); dp[0][1] = 0; for (int i = 0; i < n; ++i) { for (int j = 1; j < 200000; ++j) { for (int k = j; k < 200000; k += j) { double nxt = dp[i][j]; nxt = max(nxt, abs((double)(a[i] - k) / a[i])); dp[i + 1][k] = min(dp[i + 1][k], nxt); } } } return *min_element(begin(dp[n]), end(dp[n])); } int main() { while (cin >> n) { for (int i = 0; i < n; ++i) { cin >> a[i]; } printf("%.10lf\n", solve()); } }
#include<bits/stdc++.h> using namespace std; using Int = long long; template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;} struct Precision{ Precision(){ cout<<fixed<<setprecision(12); } }precision_beet; //INSERT ABOVE HERE signed main(){ using D = double; int n; cin>>n; vector<D> as(n); for(int i=0;i<n;i++) cin>>as[i]; const int MAX = 1e6; const D INF = 1e9; vector<D> dp(MAX,INF); for(int j=1;j<MAX;j++) dp[j]=abs(as[0]-j)/as[0]; for(int i=0;i+1<n;i++){ vector<D> nx(MAX,INF); for(int j=1;j<MAX;j++) for(int k=j;k<MAX;k+=j) chmin(nx[k],max(dp[j],abs(as[i+1]-k)/as[i+1])); swap(dp,nx); } cout<<*min_element(dp.begin(),dp.end())<<endl; return 0; }
#include<bits/stdc++.h> #define INF (1e9) #define M 200005 using namespace std; vector<int> S[M]; int n, a[21]; double dp[21][M]; int main(){ for(int i=1;i<=M;i++){ for(int j=1;j*j<=i;j++) if(i%j==0){ S[i].push_back(j); if(i!=j) S[i].push_back(i/j); } sort(S[i].begin(),S[i].end()); } cin>>n; for(int i=0;i<n;i++) cin>>a[i]; reverse(a,a+n); for(int i=0;i<=n;i++) for(int j=0;j<M;j++) dp[i][j]=INF; for(int i=1;i<M;i++) dp[0][i]=0; for(int i=0;i<n;i++){ for(int j=1;j<M;j++){ if(dp[i][j]==INF) continue; for(int k=0;k<S[j].size();k++){ int nj = S[j][k]; double ncost = max(dp[i][j], 1.0 * abs(a[i] - S[j][k]) / a[i]); dp[i+1][nj] = min(dp[i+1][nj], ncost); } } } double ans = INF; for(int i=1;i<M;i++) ans = min(ans, dp[n][i]); printf("%.10f\n", ans); return 0; }
#include <bits/stdc++.h> #define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++) using namespace std; typedef long long ll; const double EPS = 1e-10; ll N, A[20]; bool dp[21][200001]; bool ok(double x) { REP(i, 0, N + 1) REP(j, 0, 200001) dp[i][j] = false; REP(i, 0, 200001) dp[0][i] = true; REP(i, 0, N) { REP(j, 1, 200001) if(dp[i][j]) { double e = (double) abs(A[i] - j) / A[i]; if(e < x + EPS) { for(ll k = j; k < 200001; k += j) { dp[i + 1][k] = true; } } } } bool ret = false; REP(i, 0, 200001) ret = ret || dp[N][i]; return ret; } int main(void) { cin >> N; REP(i, 0, N) cin >> A[i]; double l = -1, h = 1e20; REP(_, 0, 100) { double m = (l + h) / 2; if(ok(m)) h = m; else l = m; } printf("%.15lf\n", h); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr) #define all(x) (x).begin(),(x).end() #define pb push_back #define fi first #define se second const int N=200000; double dp[20][N+1]; inline double cost(int before, int after) { return fabs(before-after)/before; } int main() { int n; cin >>n; vector<int> a(n); rep(i,n) cin >>a[i]; fill(dp[0],dp[20],1e10); for(int i=1; i<=N; ++i) dp[0][i]=cost(a[0],i); for(int i=1; i<n; ++i)for(int j=1; j<=N; ++j)for(int k=j; k<=N; k+=j) dp[i][k]=min(dp[i][k],max(dp[i-1][j],cost(a[i],k))); double ans=1e10; for(int i=1; i<=N; ++i) ans=min(ans,dp[n-1][i]); printf("%.10f\n", ans); return 0; }