hihocoder 1543
阿新 • • 發佈:2017-08-05
coder public 多少 out can 容易 for set problem
http://hihocoder.com/problemset/problem/1543
題目很簡單,最開始想了一下前綴和然後二分查找一下,發現二分很容易找不到答案
然後想起來了$s = \frac{(m+n)*(m-n+1))}{2}$公式,但是並沒有想到怎麽用,
然後看了一下別人的代碼,我們可以枚舉系數$(m-n+1)$,從$\sqrt{2*x}$開始枚舉
然後如果這個系數是2*a的因子的話,那麽看它的平均數是多少
計數個因子的平均數一定是偶數,偶數個因子的平均數一定是奇數
然後根據這個可以判斷這個等式是否成立就可以了
1 import java.math.BigDecimal;2 import java.util.Scanner; 3 4 public class Main{ 5 public static void main(String[] args) { 6 Scanner cin = new Scanner(System.in); 7 int t; 8 long a; 9 t = cin.nextInt(); 10 while((t--)>0){ 11 a = cin.nextLong(); 12 for(int i = (int) Math.sqrt(2*a);i>0;i--){ 13 if((a*2)%i==0){ 14 if((((a*2)/i)-i)%2!=0){ 15 System.out.println(i); 16 break; 17 } 18 } 19 } 20 } 21 } 22}
hihocoder 1543