zoukankan      html  css  js  c++  java
  • Poj 2853,2140 Sequence Sum Possibilities(因式分解)

    一、Description

    Most positive integers may be written as a sum of a sequence of at least two consecutive positive integers. For instance,

    6 = 1 + 2 + 3
    9 = 5 + 4 = 2 + 3 + 4
    but 8 cannot be so written.

    Write a program which will compute how many different ways an input number may be written as a sum of a sequence of at least two consecutive positive integers.

    Input

    The first line of input will contain the number of problem instances N on a line by itself, (1 N 1000) . This will be follo
    wed by N lines, one for each problem instance. Each problem line will have the problem number, a single space and the number to be written as a sequence of consecutive positive integers. The second number will be less than 231 (so will fit in a 32-bit integer).

    Output

    The output for each problem instance will be a single line containing the problem number, a single space and the number of ways the input number can be written as a sequence of consecutive positive integers.

    二、题解

    一种解法:

    1. 这串连续的整数可以表示为:(x+1)+(x+2)+(x+3)+(x+4)+……+(x+i) , 其中x=0,1,2 ......

    2. 假设已经找到:(x+1)+(x+2)+(x+3)+(x+4)+……+(x+i) =number;

    3. A=number-(1+2+3+......+i)=number-(1+i)*i/2;

    4. A=x*i;即A一定能被i整除,从而达到判定目的.

    三、java代码

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc=new Scanner(System.in);
    		int i,j,n,num,sum,temp;
    		n=sc.nextInt();
    		for(i=0;i<n;i++){
    			num=sc.nextInt();
    			sum=0;
    			temp=sc.nextInt();
    			for(j=2;(j+1)*j/2<=temp;j++){
    				if((temp-(j+1)*j/2)%j==0)
    					sum++;
    			}
    			System.out.println(num+" "+(sum));
    		}
    	}
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    电视剧的拍摄和发行
    常用键盘快捷键
    河北省各城市名称由来
    爱情16谈
    人生赏心十六件乐事
    Android——apk反编译
    Android.mk
    Android——init可执行程序
    Android启动脚本init.rc(2)
    MountService初探
  • 原文地址:https://www.cnblogs.com/AndyDai/p/4734150.html
Copyright © 2011-2022 走看看