"Title description"
We all love Kwekwe, recently kwekwe to everyone out a problem.
He tells us a n*m matrix point in which the points in this matrix can be connected to a number of lines, asking
The length equals the number of lines of a certain length.
"Input")
The first line is three positive integers n,m,t, and the 2-t+1 behavior is asked for WI.
Output
Output 1 lines, a total of T number, number I is to meet the length of the number of Wi-th.
"Input Sample 1"
3 3 1
1
For example, this is a matrix point for 3*3
"Output Example 1"
12
"Input Sample 2"
7 11 1
5
"Output Example 2"
168
"Input Sample 3"
3 5 1
2
"Output Example 3"
14
"Data description"
Test point data range
1 2 n<=2000; m<=2000; t<=100
3 4 n<=1000000; m<=1000000;t<=100
5 6 n<=10000000; m<=10000000;t<=100
7 8 n<=100000000; m<=100000000;t<=100
9 n<=1000000000; m<=1000000000;t<=100
W<=2max (N,M);
The problem is to find the number of shares, but it is too difficult to think.
Consider the horizontal and vertical conditions of the line first:
if (n>x)
ans+= (n-x) *m;
if (m>x)
ans+= (m-x) *n;
In the case of an oblique consideration:
We set the length to find R, then set x²+y²=r²
Get X=sqrt ((r+y) * (R-Y))
Set D=GCD (r+y,r-y), a= (r-y)/d,b= (r+y)/d
Get X=d*sqrt (a*b), roll out A*b is the total square number, also because A, B is two number divided by the maximum common factor
It is concluded that A and B are the complete squares of coprime
A+B=2*R/D (a+b) *d=2*r
enumeration D from 1~sqrt (2*r), 2*r%d=0 (reason: If D is greater than the square root 2r, the equivalent value of a+b and the value of D is reversed, repeat it)
Re-enumeration A from 1~SQRT (R/D)-to ensure that the a<b, and then determine whether B is a complete square number
If satisfied, the United A= (r-y)/d,b= (r+y)/d is:
Y= (A-B) *D/2, x derived from R and y
The complexity of time is counted. Enumeration d with radical 2*r, CA. 5*1e4
Then enumerate the 1~ root r/d, which should be greater than 100, multiply by 100 sets of data, and time out.
But why not time out?
Because the value of D cannot be 2*r to the root of the root, the number of loops in the back is also greatly reduced.
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <cmath>5 using namespacestd;6 intn,m,t;7 intgcdintXinty)8 {9 if(y==0)returnx;Ten returnGCD (y,x%y); One } A intMain () - { -Freopen ("amount.in","R", stdin); theFreopen ("Amount.out","W", stdout); -scanf"%d%d%d",&n,&m,&t); - if(n>m) swap (n,m);//N Small - for(intj=1; j<=t;j++) + { - Long LongX;SCANF ("%i64d",&x); + Long Longans=0; A if(n>x) atans+= (n-x) *m; - if(m>x) -ans+= (m-x) *N; - for(Long LongD=1;d *d<=2*x;d++)//Enumeration D - if(2*x%d==0) - { in for(Long LongA=1; a*a<x/d;a++) - { to + Long Longa=a*A; - Long Longb=2*x/d-A; the Long LongC=int(sqrt (b +0.5)); * if(c*c!=b)Continue;//To arrive at a, B $ if(GCD (a,c)! =1)Continue;Panax Notoginseng Long Longk1= (b-a) *d/2; - Long LongK2=SQRT (x*x-k1*k1+0.5); the if(k1*k1+k2*k2!=x*x)Continue; + if(K1>K2)Continue; A if(k1<=n&&k2<=m) theans+= (N-K1) * (M-K2) *2; + if(k1<=m&&k2<=N) -ans+= (N-K2) * (M-K1) *2; $ } $ Long Longk=2*x/D; - if(2*x%k==0) - for(Long LongA=1; a*a<x/k;a++) the { - Long Longa=a*A;Wuyi Long Longb=2*x/k-A; the Long LongC=sqrt (b +0.5); - if(GCD (a,c)! =1)Continue; Wu if(c*c!=b)Continue;//To arrive at a, B - Long Longk1= (b-a) *k/2; About Long LongK2=SQRT (x*x-k1*k1+0.5); $ if(k1*k1+k2*k2!=x*x)Continue; - if(K1>K2)Continue; - if(k1<=n&&k2<=m) -ans+= (N-K1) * (M-K2) *2; A if(k1<=m&&k2<=N) +ans+= (N-K2) * (M-K1) *2; the } - } $printf"%i64d", ans); the } the return 0; the}
I don't love Kwekwe.
Noip simulation Problem--counting