HDU 5698 C
先找规律 0 0 0 0 0 0
0 1 1 1 1 1
0 1 2 3 4 5
0 1 3 6 10 15
0 1 4 10 20 35
我操 这不是杨辉三角吗 gg 然后找规律 要求的 不就是 C(n+m-4,m-2);
然后呢 要 a/b mod(1e9+7)
= a * b的逆元 ? 费马小定理一下 (b ^1e9+5 ) %1e9+7
#include<stdio.h> #include<algorithm> #include<stdlib.h> #include<cstring> #include<string> #include<cmath> #include<vector> #include<queue> #include<map> using namespace std; #define ll __int64 #define MAXN 500 #define inf 1000000000 #define mod 1000000007 ll quick(ll a,ll b,ll c) { ll ans =1; a=a%c; while(b>0) { if(b%2==1) ans = (ans *a)%c; b=b/2; a= (a*a)%c; } return ans; } ll C(int a,int b) { ll up,down; up=down=1; for(int i=0;i<b;i++) { up = (up*(a-i))%mod; down = (down*(b-i))%mod; } ll c =quick(down,mod-2,mod); return (up*c)%mod; } int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { printf("%I64d ",C(n+m-4,m-2)); } return 0; }
HDU 5699
这B 看起来就像是二分
然而并不会
需要判断一个桥的长度mid是否可行,假设他的两个端点是x,y,那么必然满足:
|l-x|+|r-y|<=mid,解得:
l+r-mid<=y+x<=l+r+mid;r-l-mid <= y-x <= r+mid-l;
l-mid<=x<=l+mid; r-mid <= y <= r+mid;
然后就是线性规划求各种参数的范围,满足每个参数的上界都大于等于下界就好了。
#include<stdio.h> #include<algorithm> #include<stdlib.h> #include<cstring> #include<string> #include<cmath> #include<vector> #include<queue> #include<map> using namespace std; #define ll __int64 #define MAXN 1000010 #define inf 1000000000 #define mod 1000000007 int l1[MAXN],r1[MAXN]; int m; bool chick(int mid) { int xl,xr,yl,yr,xaddyl,ysubxl,xaddyr,ysubxr; xl=yl=xaddyl=ysubxl=-inf; xr=yr=xaddyr=ysubxr=inf; for(int i=1;i<=m;i++) { if(r1[i]-l1[i]<=mid) continue; xl=max(xl,l1[i]-mid); yl=max(yl,r1[i]-mid); xaddyl=max(xaddyl,l1[i]+r1[i]-mid); ysubxl=max(ysubxl,r1[i]-l1[i]-mid); xr=min(xr,l1[i]+mid); yr=min(yr,r1[i]+mid); xaddyr=min(xaddyr,l1[i]+r1[i]+mid); ysubxr=min(ysubxr,r1[i]-l1[i]+mid); } if(xr>=xl&&yr>=yl&&xaddyr>=xaddyl&&ysubxr>=ysubxl) return 1; return 0; } int main() { int n; while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=m;i++) { scanf("%d%d",&l1[i],&r1[i]); if(l1[i]>r1[i]) swap(l1[i],r1[i]); } int l=0,r=n; int ans=inf; while(l<=r) { int mid=(l+r)>>1; if(chick(mid)) { ans = min(ans,mid); r=mid-1; } else l=mid+1; } printf("%d ",ans); } return 0; }