hdu5312 Sequence 三角形数的应用

给一个数,问最少由多少 个 3*n(n-1)+1的数字构成   n*(n-1)/2是三角形数,任意一个数可以由不超过三个三角形数构成,假设有k个数   6*(A1+。。。Ak)+k=n

(n-k)%6==0 最小的k就是答案,需要特判下k=1 或2 的情况

 
 
  
  #include <iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
using namespace std;
const int N=1e6+10;
const int MAXN=(1<<31)-1;
int INF=0x7f7f7f7f;
int T,n,m,k,tot;
const int MOD=1e9+7;
int cas=1;
bool check1(int n){
    n-=1;
    if(n%3) return false;
    n/=3;
    int tmp=(int)sqrt(n)+1;
    if(tmp*(tmp-1)!=n) return false;
    return true;
}

bool check2(int n){
    for(int i=1;3*i*(i-1)+1<=n;i++){
        int u=3*i*(i-1)+1;
        if(check1(n-u)) return true;
    }
    return false;
}
int main()
{
#ifndef  ONLINE_JUDGE
   freopen("aaa","r",stdin);
#endif
    int T;
   scanf("%d",&T);
   while(T--){
          scanf("%d",&m);
          if(check1(m)) puts("1");
          else if(check2(m)) puts("2");
          else{
            for(int i=3;i<=9;i++){
                if(m-i>=0 && (m-i)%6==0){
                    printf("%d\n",i);
                    break;
                }
            }

          }
   }
    return 0;
}

 
 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值