首先说一下。N*(N-1)/2为三角形数,随意一个自然数都最多可由三个三角形数表示。

对于,对于给定的要求值 V, 那么其一组解可表示为 V = 6*(K个三角形数的和)+K; 即随意由k个数组成的解 都有 (V-K)%6==0;

那么仅仅须要找到最小的K(1,2须要特判,结论最小值为3);

在对2进行特判时候,能够从两端到中间的线性扫描来做。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <map>
  6. using namespace std;
  7. typedef long long LL;
  8. const int N = 100005;
  9.  
  10. int n,f[N];
  11. void init(){
  12. for(int i=1;;i++){
  13. int num= 3*i*(i-1)+1;
  14. f[i]=num;
  15. if(num>1e9){
  16. n=i;
  17. break;
  18. }
  19. }
  20. }
  21. int get(int v){
  22. int p = lower_bound(f+1,f+1+n,v)-f;
  23. return f[p]==v;
  24. }
  25. int find_(int v){
  26. int l=1,r=n;
  27. while(l<=r){
  28. if(f[l] + f[r] < v) return 0;
  29. while(l<=r){
  30. if(f[l]+f[r] < v) {r++; break;}
  31. else if(f[l]+f[r]==v) return 1;
  32. else r--;
  33. }
  34. l++;
  35. }
  36. return 0;
  37. }
  38. int main()
  39. {
  40. init();
  41. int T;
  42. scanf("%d",&T);
  43. while(T--){
  44. int k; scanf("%d",&k);
  45. if(get(k)) printf("1\n");
  46. else{
  47. int ok=0;
  48. if((k-2)%6 == 0){
  49. if(find_(k)) {ok=1;}
  50. }
  51. if(ok) printf("2\n");
  52. else {
  53. for(int i=3;i<=8;i++){
  54. if((k-i)%6 == 0){
  55. printf("%d\n",i);
  56. break;
  57. }
  58. }
  59. }
  60. }
  61. }
  62. return 0;
  63. }

HDU 5312(数学推导+技巧)的更多相关文章

  1. [ An Ac a Day ^_^ ] hdu 4565 数学推导+矩阵快速幂

    从今天开始就有各站网络赛了 今天是ccpc全国赛的网络赛 希望一切顺利 可以去一次吉大 希望还能去一次大连 题意: 很明确是让你求Sn=[a+sqrt(b)^n]%m 思路: 一开始以为是水题 暴力了 ...

  2. hdu 5312 数学

  3. HDU 5073 Galaxy(Anshan 2014)(数学推导,贪婪)

    Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total S ...

  4. HDU-1719 Friend 数学推导

    Friend HDU - 1719 Friend number are defined recursively as follows. (1) numbers 1 and 2 are friend n ...

  5. 借One-Class-SVM回顾SMO在SVM中的数学推导--记录毕业论文5

    上篇记录了一些决策树算法,这篇是借OC-SVM填回SMO在SVM中的数学推导这个坑. 参考文献: http://research.microsoft.com/pubs/69644/tr-98-14.p ...

  6. 关于不同进制数之间转换的数学推导【Written By KillerLegend】

    关于不同进制数之间转换的数学推导 涉及范围:正整数范围内二进制(Binary),八进制(Octonary),十进制(Decimal),十六进制(hexadecimal)之间的转换 数的进制有多种,比如 ...

  7. UVA - 10014 - Simple calculations (经典的数学推导题!!)

    UVA - 10014 Simple calculations Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...

  8. 『sumdiv 数学推导 分治』

    sumdiv(POJ 1845) Description 给定两个自然数A和B,S为A^B的所有正整数约数和,编程输出S mod 9901的结果. Input Format 只有一行,两个用空格隔开的 ...

  9. HDU 5984 数学期望

    对长为L的棒子随机取一点分割两部分,抛弃左边一部分,重复过程,直到长度小于d,问操作次数的期望. 区域赛的题,比较基础的概率论,我记得教材上有道很像的题,对1/len积分,$ln(L)-ln(d)+1 ...

随机推荐

  1. 【推导】【线段树】hdu5929 Basic Data Structure

    题意: 维护一个栈,支持以下操作: 从当前栈顶加入一个0或者1: 从当前栈顶弹掉一个数: 将栈顶指针和栈底指针交换: 询问a[top] nand a[top-1] nand ... nand a[bo ...

  2. Nginx出现504 Gateway Time-out的解决方案

    使用Nginx作为WEB服务器时,经常会遇到504 Gateway Time-out的错误提示.经过研究,基本可以确定多数情况下这个错误与Nginx本身无关,问题的根源在于Nginx将PHP的解析提交 ...

  3. Codeforces Round #348 (VK Cup 2016 Round 2, Div. 1 Edition) C. Little Artem and Random Variable 数学

    C. Little Artem and Random Variable 题目连接: http://www.codeforces.com/contest/668/problem/C Descriptio ...

  4. codeforces 148E Aragorn's Story 背包DP

    Aragorn's Story Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/probl ...

  5. MySQL的max_user_connections拒绝连接的一次踩雷经验

    近期线上的数据遇到一个问题,最终原因为max_user_connections和max_connections的一个bug导致,具体过程如下 现象 前端页面不断的出现错误页面. 排查处理过程 按照数据 ...

  6. Circuit forms adjustable bipolar clamp

    The easy way to clamp a signal to a given value is to use two zener diodes, connected back-to-back. ...

  7. [转]如何在Windows Server 2012中安装.Net Framework 3.5?

    http://www.cnblogs.com/westsource/archive/2012/12/26/2834876.html If you have Windows Server 2012 is ...

  8. JavaScript面向对象编程指南(第2版)》读书笔记

    一.对象 1.1 获取属性值的方式 water = { down: false } console.log(water.down) // false console.log(water['down'] ...

  9. HTTP Error 404.2 - Not Found The page you are requesting cannot be served because of the ISAPI and CGI Restriction list settings on the Web server(转)

    今天公司的同事問我,為什麼同一支程式在自己的電腦OK,部署到Server上會出現下面的錯誤 我想,沒有錯啊~ 我在這台Server所部署的程式一向都是OK的 看了錯誤的Error page, 發現是I ...

  10. setTimeOut传參数

    function blink(e_Id, second) {var soccer = document.getElementById(e_Id); soccer.style.visibility = ...