计蒜客 Half-consecutive Numbers 暴力打表找规律

2024-09-09 06:48

本文主要是介绍计蒜客 Half-consecutive Numbers 暴力打表找规律,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

The numbers 113366101015152121282836364545 and t_i=\frac{1}{2}i(i+1)ti=21i(i+1), are called half-consecutive.

For given NN, find the smallest rr which is no smaller than NN such that t_rtr is square.

Input Format

The input contains multiple test cases.

The first line of a multiple input is an integer TT followed by TT input lines.

Each line contains an integer N~(1\le N\le 10^{16})N (1N1016).

Output Format

For each test case, output the case number first.

Then for given NN, output the smallest rr.

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

样例输入
4
1
2
9
50
样例输出
Case #1: 1
Case #2: 8
Case #3: 49
Case #4: 288
题目来源

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛

打表找规律

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;public class Main {public static void main(String[] args) {new Task().solve();}
}class Task {InputReader in = new InputReader(System.in) ;PrintWriter out = new PrintWriter(System.out) ;void solve(){/*for(long i = 1 ; i <= 100000000L ; i++){long x =  i*(i+1)/2 ;long y = (long)Math.sqrt(x + 0.5) ;if(y * y == x){System.out.println(i + " " + x + " " + y) ;}}*/long[] a = new long[26] ;long[] b = new long[26] ;long[] r = new long[26] ;a[0] = 1 ;b[0] = 1 ;r[0] = 1 ;for(int i = 1 ; i < 26 ; i++){a[i] = a[i-1] + b[i-1] ;b[i] = a[i-1] + a[i] ;r[i] = b[i] * b[i] + ((i%2==0) ? 0 : -1) ;}int cas = 1 ;int t = in.nextInt() ;while(t-- > 0){long n = in.nextLong() ; for(int i = 0 ; i < 26 ; i++){if(r[i] >= n){out.println("Case #" + (cas++) + ": " + r[i]) ;break ;}}}out.flush() ; }}class InputReader {    public BufferedReader reader;    public StringTokenizer tokenizer;    public InputReader(InputStream stream) {    reader = new BufferedReader(new InputStreamReader(stream), 32768);    tokenizer = new StringTokenizer("");    }    private void eat(String s) {    tokenizer = new StringTokenizer(s);    }    public String nextLine() {     try {    return reader.readLine();    } catch (Exception e) {    return null;    }    }    public boolean hasNext() {    while (!tokenizer.hasMoreTokens()) {    String s = nextLine();    if (s == null)    return false;    eat(s);    }    return true;    }    public String next() {    hasNext();    return tokenizer.nextToken();    }    public int nextInt() {    return Integer.parseInt(next());    }    public int[] nextInts(int n) {    int[] nums = new int[n];    for (int i = 0; i < n; i++) {    nums[i] = nextInt();    }    return nums;    }    public long nextLong() {    return Long.parseLong(next());    }    public double nextDouble() {    return Double.parseDouble(next());    }    public BigInteger nextBigInteger() {    return new BigInteger(next());    }    }    



这篇关于计蒜客 Half-consecutive Numbers 暴力打表找规律的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1150458

相关文章

uva 568 Just the Facts(n!打表递推)

题意是求n!的末尾第一个不为0的数字。 不用大数,特别的处理。 代码: #include <stdio.h>const int maxn = 10000 + 1;int f[maxn];int main(){#ifdef LOCALfreopen("in.txt", "r", stdin);#endif // LOCALf[0] = 1;for (int i = 1; i <=

uva 10916 Factstone Benchmark(打表)

题意是求 k ! <= 2 ^ n ,的最小k。 由于n比较大,大到 2 ^ 20 次方,所以 2 ^ 2 ^ 20比较难算,所以做一些基础的数学变换。 对不等式两边同时取log2,得: log2(k ! ) <=  log2(2 ^ n)= n,即:log2(1) + log2(2) + log2 (3) + log2(4) + ... + log2(k) <= n ,其中 n 为 2 ^

计蒜客 Skiing 最长路

In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort has MM different ski paths and NN different flags situated at those turning points. The ii-th path from the

hdu 6198 dfs枚举找规律+矩阵乘法

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description We define a sequence  F : ⋅   F0=0,F1=1 ; ⋅   Fn=Fn

高精度打表-Factoring Large Numbers

求斐波那契数,不打表的话会超时,打表的话普通的高精度开不出来那么大的数组,不如一个int存8位,特殊处理一下,具体看代码 #include<stdio.h>#include<string.h>#define MAX_SIZE 5005#define LEN 150#define to 100000000/*一个int存8位*/int num[MAX_SIZE][LEN];void

10125-Sumsets【暴力】

利用n^2的时间枚举所有a[i] + a[j] 利用n^2的时间枚举所有a[i] - a[j] 之后利用n^2时间一个一个找a[i] - a[j]的值是否存在于a[i] + a[j]中 找的时候需要二分查找 另外一点就是注意long long的范围以及四个数是集合内不同的四个元素 15222638 10125 Sumsets Accepted C++ 0.449 2015-03-

10730-Antiarithmetic?【暴力枚举】

水题 求一个序列是否存在3个数按顺序构成等差数列 直接枚举等差数列的差值 时间复杂度降到 n * n / 3 开pos数组记录每个值得为之 楷vis数组记录目前i是否出现过 强行AC 15221397 10730 Antiarithmetic? Accepted C++ 0.035 2015-03-26 12:09:56 #include<cstdio>#include

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

HLJUOJ1125(暴力三点一线)

两点确定一条直线 Time Limit: 1 Sec   Memory Limit: 128 MB Submit: 19   Solved: 11 [ Submit][ Status][ Web Board] Description  给一个15000 * 15000 的区域, 坐标都是整数. 其中有N个点,N <= 770.问总共有多少个3点共线的组合.并按升序(点的ID)输出