Codeforces #269 (Div. 2)C. MUH and House of Cards(数学:通项公式)

来源:互联网 发布:torch软件去马赛克 编辑:程序博客网 时间:2024/05/17 21:44

看起来很复杂,直接找通项公式即可

令x表示最底层的卡片数,令y表示有多少层

对于y层最少需要x = y*(y+1)/2张卡片数

则代码入下:

#include <bits/stdc++.h>#define MAXN 1000010#define LL long longusing namespace std;bool vis[MAXN];int main(void) {    //3*x == n+y;    LL n, x, y, tmp, cnt;    cin >> n;    x = n/3+1;    memset(vis, 0, sizeof(vis));    cnt = 0;    while(true) {        y = 3*x-n;        tmp = y*(y+1)>>1;        if(y > x || tmp>x) {            break;        }        else if(!vis[y]) {            ++cnt;            vis[y] = true;        }        ++x;    }    cout << cnt << endl;    return 0;}


0 0
原创粉丝点击