This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int vmax = 5e4;
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
int m,n,f;
vector<pair<int,int>> l;
vector<pair<pair<int,int>,int>> t[15];
int nrfact[vmax + 5], coef[vmax + 5];
bool bad[vmax + 5];
vector<int> d[vmax + 5];
void solve(int x, int y, int s, int dir)
{
for(int i=1; i<=s; i++)
{
int xx = x + i * dx[dir];
int yy = y + i * dy[dir];
l.push_back({xx,yy});
}
x = x + s * dx[dir];
y = y + s * dy[dir];
if(s==1)
{
return;
}
for(int i=1; i<=s/2; i++)
{
int xs = x + i * dx[dir] + i * dx[(dir + 3) % 4];
int ys = y + i * dy[dir] + i * dy[(dir + 3) % 4];
l.push_back({xs,ys});
}
solve(x + (s / 2) * dx[dir] + (s / 2) * dx[(dir + 3) % 4], y + (s / 2) * dy[dir] + (s / 2) * dy[(dir + 3) % 4], s / 2, dir);
solve(x + (s / 2) * dx[dir] + (s / 2) * dx[(dir + 3) % 4], y + (s / 2) * dy[dir] + (s / 2) * dy[(dir + 3) % 4], s / 2, (dir + 3) % 4);
for(int i=1; i<=s/2; i++)
{
int xd = x + i * dx[dir] + i * dx[(dir + 1) % 4];
int yd = y + i * dy[dir] + i * dy[(dir + 1) % 4];
l.push_back({xd,yd});
}
solve(x + (s / 2) * dx[dir] + (s / 2) * dx[(dir + 1) % 4], y + (s / 2) * dy[dir] + (s / 2) * dy[(dir + 1) % 4], s / 2, dir);
solve(x + (s / 2) * dx[dir] + (s / 2) * dx[(dir + 1) % 4], y + (s / 2) * dy[dir] + (s / 2) * dy[(dir + 1) % 4], s / 2, (dir + 1) % 4);
}
void precalc_paths()
{
for(int p=0; p<=10; p++)
{
int s = (1<<p);
l.push_back({0,0});
solve(0,0,s,0);
sort(l.begin(),l.end());
int cnt = 1;
for(int i=1; i<l.size(); i++)
{
if(l[i]!=l[i-1])
{
t[p].push_back({l[i - 1], cnt});
cnt = 1;
}
else
{
++cnt;
}
}
t[p].push_back({l.back(),cnt});
l.clear();
}
}
void precalc_factors()
{
for(int i=2; i<=vmax; i++)
{
if(!nrfact[i])
{
for(int j=i; j<=vmax; j+=i)
{
++nrfact[j];
if((j / i) % i == 0)
{
bad[j] = true;
}
}
}
}
for(int i=1; i<=vmax; i++)
{
if(!bad[i])
{
if(nrfact[i] % 2 == 1)
{
coef[i] = -1;
}
else
{
coef[i] = 1;
}
}
}
for(int i=1; i<=vmax; i++)
{
if(coef[i])
{
for(int j=i; j<=vmax; j+=i)
{
d[j].push_back(i);
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef home
freopen("nr.in","r",stdin);
freopen("nr.out","w",stdout);
#endif // home
cin>>m>>n>>f;
precalc_paths();
precalc_factors();
int rez = 0;
for(int i=1; i<=f; i++)
{
int x,y,s;
cin>>x>>y>>s;
int p = 0;
while(s!=1)
{
++p;
s /= 2;
}
for(auto it : t[p])
{
if(it.first.first + x - 1 == 0)
{
rez += (it.first.second + y - 1 == 1) * it.second;
}
else if(it.first.second + y - 1 == 0)
{
rez += (it.first.first + x - 1 == 1) * it.second;
}
else
{
for(auto div : d[it.first.second + y - 1])
{
if((it.first.first + x - 1) % div != 0)
{
continue;
}
rez += coef[div] * it.second;
}
}
}
}
cout<<rez<<'\n';
return 0;
}
Compilation message (stderr)
squirrel.cpp: In function 'void precalc_paths()':
squirrel.cpp:61:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i=1; i<l.size(); i++)
| ~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |