本文主要是介绍ICPC 6929 Sums,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
思路 :如果一个数n是2的阶乘,那么是无解的。
然后,如果N是奇数,那么肯定能由中间的两个数相加得到。如果N是偶数,就要分成两种情况,一种是 取 n-1 n n+1等等,n的左右两边可以取I个值,可以发现n-2和n+2相加和n-1加n+1相加相等。。另一种情况就是中间情况是 n-1 n n+1,n-1和n的值等于n-2和n+1的值。。
贴上JYJJ阿姨的代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
//746137348
#define N 150005
#define M 305
int f[31];
int main(){//freopen("D:\\input.txt","r",stdin);//freopen("D:\\output.txt","w",stdout);int T;scanf("%d", &T);for(int i = 0;i <= 31;i++){f[i] = 1<<i;}while(T --){int n;scanf("%d", &n);if(n < 3){printf("IMPOSSIBLE\n");continue;}int fff = 0;for(int i = 0;i <= 31;i++){if(f[i] == n){printf("IMPOSSIBLE\n");fff = 1;break;}}if(fff) continue;int flag = 0;if(n%2 == 1){flag = 1;printf("%d = %d + %d", n, n/2, n/2+1);}else{for(int s = 2;s <= n;s++){if(s%2 == 0){int i = s/2;if(n%i==0 && (n/i)%2==1){int t = i*2;int tmp = (n/i)/2;int x = tmp-i+1;int ff = 0;if(x > 0){printf("%d = ", n);for(int j = x;j < x+t;j++){if(ff) printf(" + ");ff = 1;printf("%d", j);}flag = 1;break;}}}if(s%2 == 0) continue;int i = (s+1)/2;if(n%s != 0) continue;int t = n/s; int x = t-i+1;if(x <= 0) continue;printf("%d = ", n);int h = x+s;int ff = 0;for(int j = x;j < h;j++){if(ff) printf(" + ");ff = 1;printf("%d", j);}flag = 1;break;}}if(!flag) printf("IMPOSSIBLE");printf("\n");} return 0;
}
这篇关于ICPC 6929 Sums的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!