Codeforces Educational Codeforces Round 16 部分题解

来源:互联网 发布:ps淘宝图标教程视频 编辑:程序博客网 时间:2024/05/19 13:30

ECR场的题一般都比较水,而我这种菜鸡就只能写写这样的题解来安慰一下自己了。
传送门:http://codeforces.com/contest/710
A、B、C都是语言入门题,就不说了。贴上C找规律的代码:

#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm>#include<math.h>using namespace std;int a[50][50];int main (){    int n;    scanf("%d",&n);    int p=2,q=1,x=(n+1)/2;    for(int i=1;i<=n;i++){        for(int j=1;j<=n;j++){            if(abs(j-x)>=x-abs(x-i)) {                printf("%d ",p);                p+=2;            }else{                printf("%d ",q);                q+=2;            }        }        printf("\n");    }    return 0;} 

代码丑,勿喷。
D. Two Arithmetic Progressions
此题废了不少时间,起初暴力TLE,然后写了个类似扩展欧几里得算法A了,既然说类似,当然就不是,思路是解出方程:ka1+b1=la2+b2 中 k (或 l)的一个非负整数解,得出一个 x 值 ,然后…然后就出结果了
AC代码:

#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#define LL long longusing namespace std;LL l,r,a1,b1,a2,b2,g,ans;LL gcd(LL x,LL y) {    return x ? gcd(y%x,x) : y;}LL exgcd(LL a,LL b,LL c) {    return a ? (exgcd(b%a,a,-c)*b+c)/a : c/b;}int main() {    cin>>a1>>b1>>a2>>b2>>l>>r;    l=max(l,b1);    l=max(l,b2);    g=gcd(a1,a2);    if(abs(b1-b2)%g!=0) {        printf("0");        return 0;    }    if(b1<b2)        swap(b1,b2),swap(a1,a2);    LL k=exgcd(a1,a2,b2-b1);    k=k%(a2/g);    LL b=b1+k*a1,a=1LL*a1*a2/gcd(a1,a2);    if(b>r) ans=0;    else if(b<l) ans=(r-b)/a-(l-1-b)/a;    else ans=(r-b)/a+1;    cout<<ans;    return 0;}

E. Generate a String
E题,DP看代码:

#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm>using namespace std;long long dp[10000001];int main () {    long long n,x,y;    scanf("%I64d%I64d%I64d",&n,&x,&y);    dp[0]=0;    for(long long i=1; i<=n; i++) {        dp[i]=dp[i-1]+x;        if(i%2==0) {            dp[i]=min(dp[i/2]+y,dp[i]);            dp[i]=min(dp[i/2+1]+x+y,dp[i]);            dp[i]=min(dp[i/2-1]+x+y,dp[i]);        }else{            dp[i]=min(dp[i],dp[(i+1)/2]+y+x);        }    }    printf("%I64d\n",dp[n]);    return 0;}

好像状态转移有重复,但不影响AC,懒得改了。

1 0
原创粉丝点击