zoukankan      html  css  js  c++  java
  • Sum(构造,枚举)

    原题连接http://acm.hnu.cn/online/?action=problem&type=show&id=13396

    这道题并不难,他已经给出公式了

    k+k+1+k+2+k+3+....+b=N;

    我们可得(2*k+d-1)*d=2*N;然后我们从2~根号2*n枚举d,看它是否满足整除关系= =

     1 #include<stdio.h>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<string.h>
     5 #include<math.h> 
     6 int main()
     7 {
     8     int t,n;
     9     scanf("%d",&t);
    10     while(t--)
    11     {
    12         int flag=0;
    13         int l,r;
    14         scanf("%d",&n);
    15         n=2*n;
    16         for(int i=2;i<=sqrt(n);i++)
    17         {
    18             if(n%i==0)
    19             {
    20                    int ans=n/i;
    21                     if((ans-i+1)%2==0)
    22                     {
    23                         l=(ans-i+1)/2;
    24                         r=l+(i-1);
    25                         flag=1;
    26                          break;
    27                   }
    28             }
    29             }
    30         if(flag==0)
    31         printf("IMPOSSIBLE
    ");
    32         else
    33         {
    34             printf("%d = ",n/2);
    35             for(int i=l;i<r;i++)
    36             printf("%d + ",i);
    37             printf("%d
    ",r);
    38         }
    39     } 
    40     return 0;
    41 }
  • 相关阅读:
    2020/10/10周总结
    2020/10/02周总结
    2020/9/28周总结
    第十二周总结
    第十一周总结
    人月神话阅读笔记03
    人月神话阅读笔记02
    人月神话阅读笔记01
    冲刺一8
    冲刺一7
  • 原文地址:https://www.cnblogs.com/NaCl/p/4735585.html
Copyright © 2011-2022 走看看