清北考前刷题day4下午好
/* 辗转相除,每次计算多出现了几个数。 */ #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #define ll long long using namespace std; ll a1,a2,a3,a4,ans; inline ll read() { ll x=0,f=1;char c=getchar(); while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } ll gcd(ll a,ll b) { if(!b) return a; ans+=ceil(a/b); return gcd(b,a%b); } int main() { freopen("seq.in","r",stdin); freopen("seq.out","w",stdout); ll tmp; a1=read();a2=read(); if(a1<a2) a1^=a2,a2^=a1,a1^=a2; ll g=gcd(a1,a2); printf("%I64d\n",ans+1); return 0; }
#include<bits/stdc++.h> #define N 1010 #define M 40010 using namespace std; int front[N],cap[M],to[M],nextt[M],dis[N],mx,mn,ans,tot,u,v,w,n,m,now,t; int siz[1001]; int sum[1001]; bool inque[N]; inline void in(int &x) { x=0; char ch=getchar(); while(!isdigit(ch)) ch=getchar(); while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar(); } } inline void add(int u,int v,int w) { to[++tot]=v; nextt[tot]=front[u]; front[u]=tot; cap[tot]=w; to[++tot]=u; nextt[tot]=front[v]; front[v]=tot; cap[tot]=w; } queue<int>q; void spfa(int src) { mx=0; mn=0x3f3f3f3f; memset(dis,-1,sizeof(dis)); memset(inque,0,sizeof(inque)); memset(siz,0,sizeof(siz)); dis[src]=0x3f3f3f3f; q.push(src); while(!q.empty()) { now=q.front(); q.pop(); inque[now]=0; for(int i=front[now]; i; i=nextt[i]) { t=to[i]; int val=min(dis[now],cap[i]); if(dis[t]<val) { siz[val]++; if(dis[t]!=-1) siz[dis[t]]--; dis[t]=val; if(!inque[t]) { inque[t]=1; q.push(t); } } } } for(int i=1000; i>=1; i--) sum[i]=sum[i+1]+siz[i]; for(int i=1; i<=999; i++) { ans+=(sum[i]-sum[i+1])*(sum[i]-sum[i+1]); } ans+=sum[1000]*sum[1000]; } int main() { freopen("car.in","r",stdin); freopen("car.out","w",stdout); in(n); in(m); for(int i=1; i<=m; i++) { in(u);in(v);in(w); add(u,v,w); } for(int i=1; i<=n; i++) { ans=0; spfa(i); printf("%d ",ans); } }
取数
/* 30暴力dp */ #include<iostream> #include<cstdio> #include<cstring> #define N 3001 #define ll long long using namespace std; int n,m,k,cnt; ll ans; ll f[N][N],a[N]; inline int read() { int x=0,f=1;char c=getchar(); while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } int main() { n=read();m=read();k=read(); memset(f,127,sizeof f);ans=0x3f3f3f3f3f; for(int i=1;i<=n;i++) a[i]=read(); for(int i=1;i<=n;i++) f[i][1]=min(f[i-1][1],a[i]); for(int i=m;i<=n;i++) for(int j=2;j<=k;j++) f[i][j]=min(f[i][j],min(f[i-1][j],f[i-m][j-1]+a[i])); for(int i=k;i<=n;i++) ans=min(ans,f[i][k]); printf("%I64d\n",ans); }
折花枝,恨花枝,准拟花开人共卮,开时人去时。
怕相思,已相思,轮到相思没处辞,眉间露一丝。