BestCoder 1st Anniversary($) 1003 Sequence

题目传送门

 1 /*
 2    官方题解:
 3    这个题看上去是一个贪心, 但是这个贪心显然是错的.
 4    事实上这道题目很简单, 先判断1个是否可以, 然后判断2个是否可以. 之后找到最小的k(k>2), 使得(m-k)mod6=0即可.
 5    证明如下:
 6    3n(n-1)+1=6(n*(n-1)/2)+1, 注意到n*(n-1)/2是三角形数, 任意一个自然数最多只需要3个三角形数即可表示.
 7    枚举需要k个, 那么显然m=6(k个三角形数的和)+k, 由于k≥3, 只要m?k是6的倍数就一定是有解的.
 8    事实上, 打个表应该也能发现规律.
 9  */
10 #include <cstdio>
11 #include <algorithm>
12 #include <cmath>
13 #include <cstring>
14 using namespace std;
15
16 typedef long long ll;
17 const int MAXN = 2e4 + 10;
18 const int INF = 0x3f3f3f3f;
19 ll a[MAXN];
20 int tot;
21
22 void solve(void)    {
23     for (ll i=1; i<=20000; ++i)    {
24         a[i] = (ll) 3 * i * (i - 1) + 1;
25         if (a[i] > (ll) 1000000000) return ;
26         tot = i;
27     }
28 }
29
30 bool ok(ll m)   {
31     int j = tot;
32     for (int i=1; i<=tot; ++i)  {
33         while (a[i] + a[j] > m) j--;
34         if (j >=1 && a[i] + a[j] == m)  return true;
35     }
36     return false;
37 }
38
39 int main(void)  {       //BestCoder 1st Anniversary($) 1003 Sequence
40     //freopen ("C.in", "r", stdin);
41     tot = 0;    solve ();
42     int T;  scanf ("%d", &T);
43     while (T--) {
44         ll m;   scanf ("%I64d", &m);
45         if (m % 6 == 0) puts ("6");
46         else if (m % 6 == 1)    {
47             if (*lower_bound (a+1, a+1+tot, m) == m)    puts ("1");
48             else    puts ("7");
49         }
50         else if (m % 6 == 2)    {
51             if (ok (m)) puts ("2");
52             else    puts ("8");
53         }
54         else    printf ("%d\n", m % 6);
55     }
56
57     return 0;
58 }
时间: 2024-10-25 13:49:41

BestCoder 1st Anniversary($) 1003 Sequence的相关文章

HDU 5312 Sequence(三角形数应用)——BestCoder 1st Anniversary ($)

传送门 Sequence Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1424    Accepted Submission(s): 442 Problem Description Today, Soda has learned a sequence whose n-th (n≥1) item is 3n(n?1)+1. Now

BestCoder 1st Anniversary

problem 1001 Souvenir Accepts: 901 Submissions: 2743 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) 问题描述 今天是BestCoder一周年纪念日. 比赛管理员Soda想要给每个参赛者准备一个纪念品. 商店里纪念品的单价是p元, 同时也可以花q元购买纪念品套装, 一个套装里有mm个纪念品. 今天总共有nn个参赛者, Soda想

从lca到树链剖分 bestcoder round#45 1003

bestcoder round#45 1003 题,给定两个点,要我们求这两个点的树上路径所经过的点的权值是否出现过奇数次.如果是一般人,那么就是用lca求树上路径,然后判断是否出现过奇数次(用异或),高手就不这么做了,直接树链剖分.为什么不能用lca,因为如果有树退化成链,那么每次询问的复杂度是O(n), 那么q次询问的时间复杂度是O(qn) 什么是树链剖分呢? 就是把树的边分成轻链和重链 http://blogsina.com.cn/s/blog_6974c8b20100zc61.htmlh

BestCoder Round #29 1003 (hdu 5172) GTY&#39;s gay friends [线段树 判不同 预处理 好题]

传送门 GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 264    Accepted Submission(s): 57 Problem Description GTY has n gay friends. To manage them conveniently, every morning he o

hdu 5311 Hidden String (BestCoder 1st Anniversary ($))(深搜)

http://acm.hdu.edu.cn/showproblem.php?pid=5311 Hidden String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1499    Accepted Submission(s): 534 Problem Description Today is the 1st anniversar

BestCoder 1st Anniversary 1002-1005

1002 Hidden String 这个题怎么暴力怎么搞就好了. 可以枚举最长匹配前缀, 和最长匹配后缀, 中间暴力for. 1 /*Author :usedrose */ 2 /*Created Time :2015/7/25 19:05:28*/ 3 /*File Name :2.cpp*/ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <sstream

HDU 5806 NanoApe Loves Sequence Ⅱ(尺取+思维)——BestCoder Round #86 1003

传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 514    Accepted Submission(s): 248 Problem Description NanoApe, the Retired Dog, has returned back to prepare for f

HDU 5682/BestCoder Round #83 1003 zxa and leaf 二分+树

zxa and leaf Problem Description zxa have an unrooted tree with n nodes, including (n−1) undirected edges, whose nodes are numbered from 1 to n. The degree of each node is defined as the number of the edges connected to it, and each node whose degree

BestCoder 1st Anniversary ($) 题解

Souvenir 问题描述 今天是BestCoder一周年纪念日. 比赛管理员Soda想要给每个参赛者准备一个纪念品. 商店里纪念品的单价是p元, 同时也可以花q元购买纪念品套装, 一个套装里有m个纪念品. 今天总共有n个参赛者, Soda想要知道最少需要花多少钱才可以给每个人都准备一个纪念品. 输入描述 输入有多组数据. 第一行有一个整数T (1≤T≤105), 表示测试数据组数. 然后对于每组数据: 一行包含4个整数 n,m,p,q (1≤n,m,p,q≤104). 输出描述 对于每组数据输