July 6, 2015
The problem is a math problem, there are two places to pay attention to.
First: The topic asks us to divide a number into successive numbers and requires the number of digits to be the least. By the meaning of the topic can be deduced a formula, the number of n= (A + (a+k-1))/2. Where a represents a continuous number and the smallest of the number, K represents the number of consecutive numbers, the origin of the whole formula is arithmetic progression summation formula derivation.
Second: In the solution, although the formula has been deduced, but in the solution, according to the formula, we take the value of traversing all k to solve a, if the traversal from 2 to N will time out. So it's important to note that you need to open the square root from 2 to 2*n, so you can avoid timeouts.
Code:
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
Main ()
{
int T;
scanf ("%d", &t);
while (t--)
{
int n,k,flag=0;
scanf ("%d", &n);
for (int i=2;i<=int (sqrt(n*2)); i++)
{
int temp;
Temp=2*n+i-i*i;
if (temp%i==0&& (temp/i)%2==0&&temp>0)
{
K=i;
flag=1;
break;
}
}
if (flag)
{
printf ("%d =", n);
int a= (2*N+K-K*K)/(2*K);
for (int i=0;i<k;i++)
{
int ans=a+i;
if (i==k-1)
printf ("%d\n", ans);
Else
printf ("%d +", ans);
}
}
Else
printf ("impossible\n");
}
return 0;
}
Summer Practice Diary (i)