forked from Anubhav-1020/Hacktoberfest-Beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.NumberSpiral.cpp
51 lines (50 loc) · 842 Bytes
/
6.NumberSpiral.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll n;
cin>>n;
for(int i =0;i<n;i++){
ll r, c, m,s,z;
cin>>r>>c;
m=max(r,c);
s= m*m;
z= (m-1)*(m-1);
if (m % 2 == 0){
if(m==r) cout<<s - (c-1)<<endl;
else cout<<z+r<<endl;
}
else{
if (m == c) cout<< s - (r-1)<<endl;
else cout<<z+c<<endl;
}
}
return 0;
}
//from net
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll t;
cin>>t;
int r, c , a , b;
for(int i = 0;i < t;i++){
ll res =0;
cin>>r>>c;
a= max(r,c);
b=min(r,c);
res =1ll*(a-1)*(a-1);
if(a==b)res+=a;
else if (a%2 == 1){
if(c== a) res+=a*2-r;
else res+=c;
}
else{
if(r==a) res+=a*2-c;
else res+=r;
}
cout<<res<<endl;
}
return 0;
}