51nod 1138 连续整数的和(数学)
2017-04-05 21:57
344 查看
1138 连续整数的和 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 给出一个正整数N,将N写为若干个连续数字和的形式(长度 >= 2)。例如N = 15,可以写为1 + 2 + 3 + 4 + 5,也可以写为4 + 5 + 6,或7 + 8。如果不能写为若干个连续整数的和,则输出No Solution。 Input 输入1个数N(3 <= N <= 10^9)。 Output 输出连续整数中的第1个数,如果有多个按照递增序排列,如果不能分解为若干个连续整数的和,则输出No Solution。 Input示例 15 Output示例 1 4 7
根据等差数列S=(a1+(n-1)*d)*n/2和an=a1+(n-1)*d 可以算出S所能取的连续长度最大为sqrt(2*n+1/4) 即该题的范围可以缩小到[2,sqrt(2*n+1/4)]大概。 根据这区间,再配合S=(2*a1+(i-1))*i/2(i为连续整数长度)即可算出符合的连续整数和符合条件的个数了。
#include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cctype> #include<cmath> #include<ctime> #include<string> #include<stack> #include<deque> #include<queue> #include<list> #include<set> #include<map> #include<cstdio> #include<limits.h> #define MOD 1000000007 #define fir first #define sec second #define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin) #define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout) #define mes(x, m) memset(x, m, sizeof(x)) #define Pii pair<int, int> #define Pll pair<ll, ll> #define INF 1e9+7 #define inf 0x3f3f3f3f #define Pi 4.0*atan(1.0) #define lowbit(x) (x&(-x)) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long ll; typedef unsigned long long ull; const double eps = 1e-9; const int maxn = 1e5; using namespace std; inline int read(){ int x(0),f(1); char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); return x*f; } int a[maxn+5]; int main(){ int n; cin>>n; bool flag=false; int end=sqrt(2*n+1/2); // sqrt(2*n+1/4)-1/2 for(int i=end;i>=2;--i){ if((n-i*(i-1)/2)%i==0){ flag=true; printf("%d\n",(n-i*(i-1)/2)/i); } } if(!flag){ printf("No Solution\n"); } return 0; }
相关文章推荐
- 51nod 1138 连续整数的和 (数学公式_转换思想,好题吧)
- 51nod 1138 连续整数的和(数学)
- 51nod 1138 连续整数的和(数学)
- 51nod 1138 连续整数的和(数学公式)
- 51nod 1138 连续整数的和(简单数学、等差数列)
- 51Nod 1138 连续整数的和(数学、规律)
- 51nod--1138 . 连续整数的和
- 51nod 1138 连续整数的和(牛逼)
- 51Nod 1138 连续整数的和
- 51nod 1138 连续整数的和
- 51Nod 1138 连续整数的和
- 51nod 1138 连续整数的和
- 【51nod】--1138 连续整数的和 (思维)
- 【51Nod】1138 连续整数的和
- 51Nod 1138 连续整数的和
- 51nod 1138 连续整数的和(等差数列)
- 51nod 1138 连续整数的和
- 51nod 1138 连续整数的和
- 51Nod-1138-连续整数的和
- 51 NOD 1138 连续整数的和(简单数学公式)