Codeforces 525E Anya and Cubes 中途相遇法,codeforces525e

來源:互聯網
上載者:User

Codeforces 525E Anya and Cubes 中途相遇法,codeforces525e

題目連結:點擊開啟連結

題意:

給定n個數,k個驚嘆號,常數S

下面給出這n個數。

目標:

任意給其中一些數變成階乘,至多變k個。

再任意取一些數,使得這些數和恰好為S

問有多少方法。

思路:

三進位狀壓,中途尋找。

#include <stdio.h>#include <vector>#include <algorithm>#include <iostream>#include <cmath>#include <map>#include <string.h>#include <string>template <class T>inline bool rd(T &ret) {    char c; int sgn;    if (c = getchar(), c == EOF) return 0;    while (c != '-' && (c<'0' || c>'9')) c = getchar();    sgn = (c == '-') ? -1 : 1;    ret = (c == '-') ? 0 : (c - '0');    while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');    ret *= sgn;    return 1;}template <class T>inline void pt(T x) {    if (x <0) {        putchar('-');        x = -x;    }    if (x>9) pt(x / 10);    putchar(x % 10 + '0');}using namespace std;typedef long long ll;const double pi = acos(-1.);const double e = 2.718281828459;const ll ma = 1e8;const int N = 2005;int n, k;ll a[30], m;ll jie[1000], hehe;ll cal(ll x){    if (x >= hehe)return -1;    return jie[x];}ll re[30];map<ll, int>mp[2][30];ll b[30], d[30], top;ll y[30], t;ll san[30];void work(int x){    for (int i = 0; i < san[top]; i++)    {        int cnt = 0;        ll sum = 0;        int tmp = i, id = 0;        while (tmp){            if ((tmp % 3) == 1){                cnt++; sum += d[id];                if (d[id] < 0){ sum = m + 1; break; }            }            else if ((tmp % 3) == 2){                sum += b[id];            }            if (cnt >k || sum > m)break;            tmp /= 3; id++;        }        if (cnt <= k && sum <= m)mp[x][cnt][sum]++;    }}int main(){    while (cin >> n){        rd(k); rd(m);        san[0] = 1; for (int i = 1; i < 30; i++)san[i] = san[i - 1] * 3;        jie[1] = 1;        for (int i = 2;; i++){            jie[i] = jie[i - 1] * i;            if (m / jie[i] <= i){                hehe = i + 1; break;            }        }        for (int i = 0; i < n; i++){            rd(a[i]);            re[i] = cal(a[i]);        }        for (int i = 0; i < n / 2; i++){ b[i] = a[i]; d[i] = re[i]; }        top = n / 2;        work(0);        for (int i = n / 2; i < n; i++){ b[i - n / 2] = a[i]; d[i - n / 2] = re[i]; }        top = n - n / 2;        work(1);        ll ans = 0;        for (int i = 0; i <= k; i++)        for (auto it : mp[0][i])        for (int j = 0; j + i <= k; j++)        if (mp[1][j].count(m - it.first))            ans += (ll)it.second * mp[1][j][m - it.first];        pt(ans);    }    return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.