D. Spongebob and squares--cf599d (mathematics)

Source: Internet
Author: User

Http://codeforces.com/problemset/problem/599/D

To give you a number k let you ask for a n*m rectangle contains k square output how many such rectangles are

Can introduce a mathematical formula

We enumerate the squares of the i*i square inside the square containing the square that has (i*i) + (i-1) * (i-1) + (i-2) * (i-2) +...+ (1*1) is equal to b=i* (i-1) * (2*i-1)/6;

If K>b shows that the square inside this square is not enough, we need to add n (1*i) columns

If adding a column can increase the small square is (from 0 plus to i) i* (i+1)/2

So if (k-b)% (i* (i+1)/2) ==0 description happens to have (k-b)/(i* (i+1)/2) so many columns and then save it.

I can be directly violent with a maximum of 2000000.

#include <iostream>#include<cstring>#include<algorithm>#include<queue>#include<cstdio>#include<cstdlib>#include<cctype>#include<math.h>#include<ctype.h>using namespacestd;#defineMemset (A, B) memset (A,b,sizeof (a))#defineN 5001000typedefLong Longll;Const DoubleESP = 1e-8;#defineINF 0XFFFFFFFstructnode{ll x, y;} A[n];intMain () {ll k;  while(SCANF ("%lld", &k)! =EOF) {ll sum=0;        ll I; intflag=0; LL P=(ll) sqrt (k);  for(i=1;; i++)        {            if(i>2000000|| I>p) Break; LL b= (i* (i+1)*(2*i+1)/6); ll c=i* (i+1)/2; if(k>=b&& (K-b)%c==0) {a[sum].x=i; A[sum++].y= (k-b)/c+i; }        }        if(a[sum-1].y==a[sum-2].x && a[sum-1].x==a[sum-2].y) {printf ("%lld\n", (sum-1)*2);  for(i=0; i<sum;i++) printf ("%lld%lld\n", A[I].X,A[I].Y);  for(i=sum-3; i>=0; i--) printf ("%lld%lld\n", a[i].y,a[i].x); }        Else        {            if(a[sum-1].x==a[sum-1].y) {printf ("%d\n", sum*2-1);  for(i=0; i<sum-1; i++) printf ("%lld%lld\n", A[I].X,A[I].Y);  for(i=sum-1; i>=0; i--) printf ("%lld%lld\n", a[i].y,a[i].x); }            Else{printf ("%lld\n", sum*2);  for(i=0; i<sum;i++) printf ("%lld%lld\n", A[I].X,A[I].Y);  for(i=sum-1; i>=0; i--) printf ("%lld%lld\n", a[i].y,a[i].x); }        }    }    return 0;}

D. Spongebob and squares--cf599d (mathematics)

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.