Light OJ 1278 Sum of consecutive integers (number of odd factors)

Source: Internet
Author: User

Topic Link: Portal

Test instructions: Divides the given n into successive numbers and has at least two numbers to see how many options are available.

Analysis:

A + (A + 1) + (A + 2) + ... +(A + k-1) = N;

===> (2*a + k-1) * k = 2*n;

===> (2*a-1) *k = 2*n-k*k;

===> 2*a-1 = 2*n/k-K;

The left side of the equation is odd, then the right side must be odd, then K must be an odd number of n

So the n factor decomposition is possible.

The code is as follows:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <set> #include <map> #include <queue> #define PB push_back#define MP make_pair# Define REP (I,n) for (int i=0;i< (n), ++i) #define for (I,L,H) for (int i= (L), i<= (h), ++i) #define DWN (i,h,l) for (int i= (h ); i>= (l);-i) #define IFOR (I,H,L,V) for (int i= (h); i<= (L); i+= (v)) #define CLR (Vis) memset (vis,0,sizeof (VIS)) # Define MST (Vis,pos) memset (vis,pos,sizeof (VIS)) #define MAX3 (A,B,C) max (A,max (b,c)) #define MAX4 (A,B,C,D) max (max (b) , Max (c,d)) #define MIN3 (a,b,c) min (a,min (b,c)) #define MIN4 (a,b,c,d) min (min (A, b), Min (c,d)) #define PI ACOs (-1.0) # Define INF 1000000000#define linf 1000000000000000000ll#define eps 1e-8#define LL long longusing namespace Std;const int m    AXN = 1e7+10;int p[maxn/10],num;bool vis[maxn];void init () {num=0;    CLR (VIS);            for (i,2,maxn-1) {if (!vis[i]) {p[num++]=i;        IFOR (J,i+i,maxn-1,i) vis[j]=1; }   }}int Main () {init ();    int T,cas=1;    scanf ("%d", &t);        while (t--) {LL n;        scanf ("%lld", &n);        LL ans = 1;                for (int i=0;i<num&&p[i]<=n;i++) {if (n%p[i]==0) {int cnt = 0;                while (n%p[i]==0) n=n/p[i],cnt++;            if (p[i]&1) ans=ans* (LL) (cnt+1);        }} if (n>1&&n&1) ans=ans*2ll;        ans--;    printf ("Case%d:%lld\n", Cas++,ans); } return 0;}


Light OJ 1278 Sum of consecutive integers (number of odd factors)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.