http://acm.hust.edu.cn/vjudge/contest/view.action?cid=96545#overview   (acmacm)

 

A:   动态规划

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>

#define N 50
#define INF 0x3f3f3f3f

using namespace std;

long long dp[N][N];

int main()
{
    int i, j, t, iCase=1;
    scanf("%d", &t);

    memset(dp, 0, sizeof(dp));

    for(i=0; i<=30; i++)
        dp[i][0] = 1;


    for(i=1; i<=30; i++)
    for(j=1; j<=i; j++)
    {
        long long a=0;   ///因为把a刚开始定义为int类型的让我WA了好多次,可伤心,不过最后还好我找出来了
        if(j-2>=0)
            a = (i-j+1)*(i-j+1)*dp[i-1][j-2];

        dp[i][j] += dp[i-1][j] + (2*(i-j)+1)*dp[i-1][j-1] + a;
    }



    while(t--)
    {
        int n, k;

        scanf("%d%d", &n, &k);

        printf("Case %d: %llu\n", iCase++, dp[n][k]);

    }
    return 0;
}
View Code

 

B:扩展欧几里得 (其实我是不会这个东西的,自己下去要好好学下)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>

#define N 2100000
#define INF 0x3f3f3f3f

using namespace std;

long long  exgcd(long long  a,long long  b,long long  &x,long long  &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    long long  r=exgcd(b,a%b,x,y);
    long long  t=x;
    x=y;
    y=t-a/b*y;
    return r;
}

bool linear_equation(long long a, long long  b, long long  c, long long  &x,long long  &y)
{
    long long  d=exgcd(a,b,x,y);
    if(c%d)
        return false;
    long long  k=c/d;
    x*=k;
    y*=k;    //求得的只是其中一组解
    return true;
}

int main()
{

    long long  a, b, c, x, y;

    while(scanf("%lld%lld%lld", &a, &b, &c)!=EOF)
    {
        long long  ans = linear_equation(a, b, c, x, y);

        if(ans)
        printf("%lld %lld\n", -x, -y);
        else
        printf("-1\n");
    }

    return 0;
}
View Code

 

C: 基础数论

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>

using namespace std;
#define N 1100000

int a[N];

int main()
{
    int t, iCase=1;
    scanf("%d", &t);

    while(t--)
    {
        int i, n, b, Max=0;

        memset(a, 0, sizeof(a));

        scanf("%d", &n);

        for(i=1; i<=n; i++)
        {
            scanf("%d", &b);
            Max = max(Max, b);
            a[b]++;
        }

        long long sum = 0;
        for(i=0; i<=Max; i++)
        {
            sum += (a[i]+i)/(i+1)*(i+1);
        }

        printf("Case %d: ", iCase++);
        printf("%lld\n", sum);
    }
    return 0;
}
View Code

 

D:回文串 manacher

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>

#define N 2100000
#define INF 0x3f3f3f3f

using namespace std;

char s[N], str[N];
int p[N];

int manacher()
{
    int index=0, MaxLen = 0, ans=0;
    int len = strlen(s);

    memset(p, 0, sizeof(p));

    for(int i=2; s[i]; i++)
    {

        if(MaxLen > i) p[i] = min(MaxLen-i, p[2*index-i]);
        else p[i] = 1;
        while( s[i-p[i]] == s[i+p[i]] )
            p[i]++;

        if(i+p[i]>MaxLen)
        {
            MaxLen = p[i]+i;
            index = i;
        }

        if(i+p[i]==len) ///重点
           ans = max(ans, p[i]);
    }

    return ans-1;
}

int main()
{
   int t, iCase=1, i;
   scanf("%d", &t);

   while(t--)
   {
      scanf("%s", str);
      printf("Case %d: ", iCase++);
      int len = strlen(str);

      s[0] = '#';
      for( i=0; str[i]; i++)
      {
          s[i*2+1] = '*';
          s[i*2+2] = str[i];
      }

      s[i*2+1] = '*';
      s[i*2+2] = '\0';


      int L = manacher();

      printf("%d\n", 2*len-L);
   }
   return 0;
}
View Code

 

posted on 2015-10-25 00:52  栀蓝  阅读(155)  评论(0编辑  收藏  举报

levels of contents