📄 toj_2800.cpp
字号:
/*2800. Cube Time Limit: 1.0 Seconds Memory Limit: 65536KTotal Runs: 555 Accepted Runs: 298As a student of the applied mathematics school of UESTC, WCM likes mathematics. Some day he found an interesting theorem that every positive integer's cube can be expressed as the sum of some continuous odd positive integers.For example,11*11*11 = 1331 = 111+113+115+117+119+121+123+125+127+129+131Facing such a perfect theorem, WCM felt very agitated. But he didn't know how to prove it. He asked his good friend Tom Riddle for help. Tom Riddle is a student of the computer science school of UESTC and is skillful at programming. He used the computer to prove the theorem's validity easily. Can you also do it?Given a positive integer N, you should determine how to express this number as the sum of N continuous odd positive integers. You only need to output the smallest and the largest number among the N integers.InputThe input contains an integer on the first line, which indicates the number of test cases. Each test case contains one positive integer N on a single line(0 < N ≤ 1000).OutputFor each test case, output two integers on a line, the smallest and the largest number among the N continuous odd positive integers whose sum is N * N * N.Sample Input2113Sample Output111 1317 11Source: The 5th UESTC Programming Contest*/#include<cstdio>int getMid( int a , int b ){ int mid; mid = ( a + b ) / 2; if ( mid % 2 == 0 ) return mid + 1; else return mid;}void find( int a , int& start , int mid , int& end , int cubeN ) { int result; result = ( a + end ) * ( end - a + 2 ) / 4; if ( start > mid && mid > end ) { start = -1; } else if ( result > cubeN ) { start = mid; mid = getMid( start , end ); find( a , start , mid , end , cubeN ); printf( "start = %d , mid = %d , end = %d\n" , start , mid , end ); } else if ( result < cubeN ) { end = mid; mid = ( start + end ) / 2; mid = ( mid % 2 == 0 ) ? mid - 1 : mid; find( a , start , mid , end , cubeN ); printf( "start = %d , mid = %d , end = %d\n" , start , mid , end ); }}int main(){ int i , n , a , b , cubeN , start , mid , end , times; bool flag; scanf( "%d" , × ); for ( i = 0; i < times; i++ ) { scanf( "%d" , &n ); cubeN = n * n * n; a = 1; for ( a = 1; a < cubeN; a+= 2 ) { start = a; end = ( cubeN % 2 == 0 ) ? cubeN - 1: cubeN; mid = getMid( start , end ); find( a , start , mid , end , cubeN ); if ( start != -1 ) { b = end; break; } } printf( "%d %d\n" , a , b ); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -