我是靠谱客的博主 顺利皮带,这篇文章主要介绍2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛E Half-consecutive Numbers,现在分享给大家,希望可以做个参考。

time limit
2000ms
memory limit

131072KB


The numbers  1 ,  3 ,  6 ,  10 ,  15 ,  21 ,  28 ,  36 ,  45 and  t(i) = [i(i + 1) ] / 2, are called half-consecutive.
For given  N , find the smallest  r which is no smaller than  N such that  t(r) is square.

Input Format
The input contains multiple test cases.
The first line of a multiple input is an integer  T followed by  T input lines.

Each line contains an integer  N (1 ≤ N ≤ 10 ) .


Output Format
For each test case, output the case number first.
Then for given  N , output the smallest  r .

If this half-consecutive number does not exist, output  −1 .


Sample Input
4
1
2
9

50


Sample Output
Case #1: 1
Case #2: 8
Case #3: 49

Case #4: 288


题意:定义自然数的前 i 项和为 half-consecutive;给定一个数N,t(r) 是个平方数,找到不小于N且离N最近的 r 是多少。

思路:数论先打表;

打表完就找到规律了:

i          x^2                               x

1:         1                                     1
8:         36-                                  6
49:        1225-                             35
288:       41616-                          204
1681:      1413721-                     1189
9800:      48024900-                   6930
57121:     1631432881-              40391
332928:    55420693056-           235416
1940449:   1882672131025-      1372105
11309768:  63955431761796-    7997214

开放数有规律,每个平方数的开方与对应的i有规律;


代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const ll stop=10000000000000000; const int N=1000006; ll chazhi[N],square[N],bianhao[N]; int main() { square[1]=1,square[2]=6; int i; for( i=3;; i++) { square[i]=6*square[i-1]-square[i-2]; if(square[i]>=stop) break; } chazhi[1]=0,chazhi[2]=2,chazhi[3]=14; for(int j=4; j<=i; j++) { chazhi[j]=chazhi[j-1]*6-(chazhi[j-2]-2); } for(int j=1; j<=i; j++) { bianhao[j]=chazhi[j]+square[j]; } // for(int j=1; j<=i; j++) // printf("%I64d ",bianhao[j]); // printf("n"); int t,cas=0; scanf("%d",&t); while(t--) { ll x; scanf("%lld",&x); ll ans=lower_bound(bianhao+1,bianhao+1+i,x)-bianhao; // if(bianhao[ans]>) if(bianhao[ans]>stop) printf("Case #%d: -1n",++cas); else printf("Case #%d: %lldn",++cas,bianhao[ans]); } return 0; }


最后

以上就是顺利皮带最近收集整理的关于2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛E Half-consecutive Numbers的全部内容,更多相关2017内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部