蓝桥杯--杨辉三角形

蓝桥杯--杨辉三角形

核心:求组合数的方法,找规律

https://www.acwing.com/problem/content/3421/

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
LL n;
LL C(int a,int b){
    LL res=1;
    for(int i=a,j=1;j<=b;j++,i--){
        res=res* i/j;
        if(res>n) return res;
    }
    return res;
}

bool check(int k){
    LL l=2*k;
    LL r=max(n,l);
    while(l<r){
        LL mid=l+r>>1;
        //mid行 k列 ,求>=n的第一个值
        if(C(mid,k) >=n)    r=mid;
        else l=mid+1;
    }
    if(C(r,k)!=n)   return false;
    
    cout <<  r *(r+1) /2 +k+1; //坐标(r,k)前面有r行,有k列,是第 r *(r+1) /2 +k+1个
    return true;
    
}



int main()
{
    cin>>n;
    for(int i=16;;i--){
        if(check(i))    break;
    }
    return 0;
}
posted @ 2022-03-16 15:05  秋月桐  阅读(78)  评论(0编辑  收藏  举报