HDU 5312(数学推导+技巧)
首先说一下。N*(N-1)/2为三角形数,随意一个自然数都最多可由三个三角形数表示。
对于,对于给定的要求值 V, 那么其一组解可表示为 V = 6*(K个三角形数的和)+K; 即随意由k个数组成的解 都有 (V-K)%6==0;
那么仅仅须要找到最小的K(1,2须要特判,结论最小值为3);
在对2进行特判时候,能够从两端到中间的线性扫描来做。
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <iostream>
- #include <map>
- using namespace std;
- typedef long long LL;
- const int N = 100005;
- int n,f[N];
- void init(){
- for(int i=1;;i++){
- int num= 3*i*(i-1)+1;
- f[i]=num;
- if(num>1e9){
- n=i;
- break;
- }
- }
- }
- int get(int v){
- int p = lower_bound(f+1,f+1+n,v)-f;
- return f[p]==v;
- }
- int find_(int v){
- int l=1,r=n;
- while(l<=r){
- if(f[l] + f[r] < v) return 0;
- while(l<=r){
- if(f[l]+f[r] < v) {r++; break;}
- else if(f[l]+f[r]==v) return 1;
- else r--;
- }
- l++;
- }
- return 0;
- }
- int main()
- {
- init();
- int T;
- scanf("%d",&T);
- while(T--){
- int k; scanf("%d",&k);
- if(get(k)) printf("1\n");
- else{
- int ok=0;
- if((k-2)%6 == 0){
- if(find_(k)) {ok=1;}
- }
- if(ok) printf("2\n");
- else {
- for(int i=3;i<=8;i++){
- if((k-i)%6 == 0){
- printf("%d\n",i);
- break;
- }
- }
- }
- }
- }
- return 0;
- }
HDU 5312(数学推导+技巧)的更多相关文章
- [ An Ac a Day ^_^ ] hdu 4565 数学推导+矩阵快速幂
从今天开始就有各站网络赛了 今天是ccpc全国赛的网络赛 希望一切顺利 可以去一次吉大 希望还能去一次大连 题意: 很明确是让你求Sn=[a+sqrt(b)^n]%m 思路: 一开始以为是水题 暴力了 ...
- hdu 5312 数学
- HDU 5073 Galaxy(Anshan 2014)(数学推导,贪婪)
Galaxy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total S ...
- HDU-1719 Friend 数学推导
Friend HDU - 1719 Friend number are defined recursively as follows. (1) numbers 1 and 2 are friend n ...
- 借One-Class-SVM回顾SMO在SVM中的数学推导--记录毕业论文5
上篇记录了一些决策树算法,这篇是借OC-SVM填回SMO在SVM中的数学推导这个坑. 参考文献: http://research.microsoft.com/pubs/69644/tr-98-14.p ...
- 关于不同进制数之间转换的数学推导【Written By KillerLegend】
关于不同进制数之间转换的数学推导 涉及范围:正整数范围内二进制(Binary),八进制(Octonary),十进制(Decimal),十六进制(hexadecimal)之间的转换 数的进制有多种,比如 ...
- UVA - 10014 - Simple calculations (经典的数学推导题!!)
UVA - 10014 Simple calculations Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- 『sumdiv 数学推导 分治』
sumdiv(POJ 1845) Description 给定两个自然数A和B,S为A^B的所有正整数约数和,编程输出S mod 9901的结果. Input Format 只有一行,两个用空格隔开的 ...
- HDU 5984 数学期望
对长为L的棒子随机取一点分割两部分,抛弃左边一部分,重复过程,直到长度小于d,问操作次数的期望. 区域赛的题,比较基础的概率论,我记得教材上有道很像的题,对1/len积分,$ln(L)-ln(d)+1 ...
随机推荐
- 【推导】【线段树】hdu5929 Basic Data Structure
题意: 维护一个栈,支持以下操作: 从当前栈顶加入一个0或者1: 从当前栈顶弹掉一个数: 将栈顶指针和栈底指针交换: 询问a[top] nand a[top-1] nand ... nand a[bo ...
- Nginx出现504 Gateway Time-out的解决方案
使用Nginx作为WEB服务器时,经常会遇到504 Gateway Time-out的错误提示.经过研究,基本可以确定多数情况下这个错误与Nginx本身无关,问题的根源在于Nginx将PHP的解析提交 ...
- 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 ...
- codeforces 148E Aragorn's Story 背包DP
Aragorn's Story Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/probl ...
- MySQL的max_user_connections拒绝连接的一次踩雷经验
近期线上的数据遇到一个问题,最终原因为max_user_connections和max_connections的一个bug导致,具体过程如下 现象 前端页面不断的出现错误页面. 排查处理过程 按照数据 ...
- 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. ...
- [转]如何在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 ...
- JavaScript面向对象编程指南(第2版)》读书笔记
一.对象 1.1 获取属性值的方式 water = { down: false } console.log(water.down) // false console.log(water['down'] ...
- 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 ...
- setTimeOut传參数
function blink(e_Id, second) {var soccer = document.getElementById(e_Id); soccer.style.visibility = ...