zoukankan      html  css  js  c++  java
  • SGU 221.Big Bishops(DP)

    题意:

           给一个n*n(n<=50)的棋盘,放上k个主教(斜走),求能放置的种类总数。


    Solution :

                  同SGU 220,加个高精度就好了。

    code

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    string  f[2][250][250], ans;
    int tem[250];
    int n, k, tol;
    
    string add (string a, string b) {
        string c;
        int s[1000] = {0};
        for (int i = 0; i < a.size(); i++) s[i] += a[i] - '0';
        for (int i = 0; i < b.size(); i++) s[i] += b[i] - '0';
        int len = max (a.size(), b.size() );
        for (int i = 0; i < len; ++i) {
            if (s[i] >= 10) {
                s[i + 1] += s[i] / 10, s[i] = s[i] % 10;
                if (i + 1 == len) len++;
            }
            c += '0' + s[i];
        }
        return c;
    }
    string operator * (string a, int k) {
        string c;
        int len = a.size(), x = 0;
        for (int i = 0, tem; i < len; ++i) {
            tem = (a[i] - '0') * k + x;
            c += '0' + tem % 10;
            x = tem / 10;
        }
        for (; x; x /= 10) c += '0' + x % 10;
        return c;
    }
    string operator * (string a, string b) {
        string c;
        int s[1000] = {0};
        for (int i = 0; i < a.size(); ++i)
            for (int j = 0; j < b.size(); ++j)
                s[i + j] += (a[i] - '0') * (b[j] - '0');
        int len = a.size() + b.size() - 1;
        for (int i = 0; i < len; ++i) {
            if (s[i] >= 10) {
                s[i + 1] += s[i] / 10, s[i] %= 10;
                if (i + 1 == len) len++;
            }
            c += '0' + s[i];
        }
        return c;
    }
    void make (int x) {
        tol = 0;
        for (int t = x; t <= n; t += 2) {
            tem[++tol] = t;
            if (t != n) tem[++tol] = t;
        }
        f[x - 1][0][0] = "1";
        string t;
        for (int i = 1; i <= tol; i++)
            for (int j = 0; j <= k; j++)
                if (tem[i] >= j) {
                    if (j > 0) t = f[x - 1][i - 1][j - 1] * (tem[i] - j + 1);
                    else       t = "0";
                    f[x - 1][i][j] = add (f[x - 1][i - 1][j] , t );
                }
    }
    int main() {
        ios::sync_with_stdio (0);
        cin >> n >> k;
        make (1);
        make (2);
        ans = "0";
        for (int i = 0; i <= k; i++)
            ans = add (ans , f[1][tol][i] * f[0][2 * n - 1 - tol][k - i]);
        while (* (ans.end() - 1) == '0' && ans.size() > 1) ans.erase (ans.end() - 1);
        reverse (ans.begin(), ans.end() );
        cout << ans ;
    }
    View Code
  • 相关阅读:
    七月在线爬虫班学习笔记(五)——scrapy spider的几种爬取方式
    七月在线爬虫班学习笔记(四)——相关库使用与登录问题
    七月在线爬虫班学习笔记(三)——爬虫基础知识与简易爬虫实现
    文件处理工具类
    执行脚本工具类
    LRU
    springboot 整合 memcached
    转载连接
    SpringBoot2.x中redis使用(lettuce)
    Redis 数据类型
  • 原文地址:https://www.cnblogs.com/keam37/p/4032209.html
Copyright © 2011-2022 走看看