Submission #32185372
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007
#define rep(i,a,n) for (ll i=a;i<(ll)n;i++)
#define per(i,a,n) for (ll i=n;i-->(ll)a;)
#define pb push_back
ll read(){ll r;scanf("%lld",&r);return r;} // read
int gcd(int a,int b){
while(b!=0){
a=a%b;
swap(a,b);
}
return a;
}
int fa[200010];
int a[4];
int inner[200010]; // 组内部最小间隔, 一定是n的因子
int tofa[200010]; // 跨组偏移距离
ll n;
int getfa(int i){
if(i == fa[i]) return i;
int newfa = getfa(fa[i]);
tofa[i] = ((tofa[i] + tofa[fa[i]])%n)%inner[newfa];
return fa[i] = newfa;
}
// new root u
// u and v is old root
void link(int u,int v,int off){
fa[v] = u;
inner[u] = gcd(inner[u],inner[v]);
tofa[v] = off % inner[u];
}
int main(){
n = read();
ll q = read();
ll ans = n*n;
iota(fa,fa+n,0);
fill(inner,inner+n,n);
rep(i,0,q){
rep(j,0,4) a[j] = read();
per(j,0,4) (a[j] += n-a[0])%=n;
int g1 = a[1] - a[0];
int g2 = (a[3]-a[2]+n)%n;
int off = a[2] - a[0];
int f1 = getfa(g1);
int f2 = getfa(g2);
// 同组更新内部间隔
if(f1 == f2){
// ans -= inner[f1] - inner[f1];
// printf("SAME %d[%d] => ",f1,inner[f1]);
ans -= inner[f1];
inner[f1] = gcd(inner[f1], 2*n + tofa[g1] - tofa[g2] + off); // 不是off
ans -= -inner[f1];
// printf("%d \n",inner[f1]);
}else{ // 不同组 合并组
// printf("DIFF %d[%d] + %d[%d] => ",f1,inner[f1],f2,inner[f2]);
// g1->f1, g2->f2
// f2->f1?
// f1 - f2 = (f1 - g1) - (f2 - g2) + (g2 - g1)
// ans -= inner[f1] + inner[f2] - inner[f1];
ans -= inner[f1] + inner[f2];
link(f1,f2, (2*n + tofa[g1] - tofa[g2] + off)%n);
ans -= - inner[f1];
// printf("%d \n",inner[f1]);
}
printf("%lld\n",ans);
}
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | E - Sliding Edge on Torus |
| User | cromarmot |
| Language | C++ (GCC 9.2.1) |
| Score | 700 |
| Code Size | 1850 Byte |
| Status | AC |
| Exec Time | 140 ms |
| Memory | 6144 KiB |
Compile Error
./Main.cpp: In function ‘ll read()’:
./Main.cpp:10:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
10 | ll read(){ll r;scanf("%lld",&r);return r;} // read
| ~~~~~^~~~~~~~~~~
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 700 / 700 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt |
| All | 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 01-random-001.txt, 01-random-002.txt, 01-random-003.txt, 01-random-004.txt, 01-random-005.txt, 02-max-001.txt, 02-max-002.txt, 02-max-003.txt, 02-max-004.txt, 03-highly_composite-001.txt, 03-highly_composite-002.txt, 03-highly_composite-003.txt, 03-highly_composite-004.txt, 03-highly_composite-005.txt, 03-highly_composite-006.txt, 03-highly_composite-007.txt, 03-highly_composite-008.txt, 03-highly_composite-009.txt, 03-highly_composite-010.txt, 03-highly_composite-011.txt, 03-highly_composite-012.txt, 03-highly_composite-013.txt, 03-highly_composite-014.txt, 03-highly_composite-015.txt, 03-highly_composite-016.txt, 03-highly_composite-017.txt, 03-highly_composite-018.txt, 03-highly_composite-019.txt, 03-highly_composite-020.txt, 03-highly_composite-021.txt, 03-highly_composite-022.txt, 03-highly_composite-023.txt, 03-highly_composite-024.txt, 03-highly_composite-025.txt, 03-highly_composite-026.txt, 03-highly_composite-027.txt, 03-highly_composite-028.txt, 03-highly_composite-029.txt, 03-highly_composite-030.txt, 03-highly_composite-031.txt, 03-highly_composite-032.txt, 03-highly_composite-033.txt, 03-highly_composite-034.txt, 03-highly_composite-035.txt, 03-highly_composite-036.txt, 04-line-001.txt, 04-line-002.txt, 04-line-003.txt, 04-line-004.txt, 04-line-005.txt, 04-line-006.txt, 04-line-007.txt, 04-line-008.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00-sample-001.txt | AC | 2 ms | 3584 KiB |
| 00-sample-002.txt | AC | 2 ms | 3584 KiB |
| 00-sample-003.txt | AC | 2 ms | 3744 KiB |
| 01-random-001.txt | AC | 121 ms | 4496 KiB |
| 01-random-002.txt | AC | 132 ms | 5260 KiB |
| 01-random-003.txt | AC | 123 ms | 4556 KiB |
| 01-random-004.txt | AC | 132 ms | 5112 KiB |
| 01-random-005.txt | AC | 115 ms | 4312 KiB |
| 02-max-001.txt | AC | 131 ms | 6132 KiB |
| 02-max-002.txt | AC | 128 ms | 5988 KiB |
| 02-max-003.txt | AC | 135 ms | 6036 KiB |
| 02-max-004.txt | AC | 127 ms | 6048 KiB |
| 03-highly_composite-001.txt | AC | 117 ms | 4676 KiB |
| 03-highly_composite-002.txt | AC | 120 ms | 4580 KiB |
| 03-highly_composite-003.txt | AC | 119 ms | 4556 KiB |
| 03-highly_composite-004.txt | AC | 118 ms | 4772 KiB |
| 03-highly_composite-005.txt | AC | 118 ms | 4580 KiB |
| 03-highly_composite-006.txt | AC | 116 ms | 4580 KiB |
| 03-highly_composite-007.txt | AC | 117 ms | 4688 KiB |
| 03-highly_composite-008.txt | AC | 115 ms | 4532 KiB |
| 03-highly_composite-009.txt | AC | 114 ms | 4576 KiB |
| 03-highly_composite-010.txt | AC | 115 ms | 4728 KiB |
| 03-highly_composite-011.txt | AC | 128 ms | 4692 KiB |
| 03-highly_composite-012.txt | AC | 114 ms | 4676 KiB |
| 03-highly_composite-013.txt | AC | 127 ms | 4864 KiB |
| 03-highly_composite-014.txt | AC | 124 ms | 5008 KiB |
| 03-highly_composite-015.txt | AC | 127 ms | 5008 KiB |
| 03-highly_composite-016.txt | AC | 120 ms | 5108 KiB |
| 03-highly_composite-017.txt | AC | 122 ms | 4908 KiB |
| 03-highly_composite-018.txt | AC | 122 ms | 5044 KiB |
| 03-highly_composite-019.txt | AC | 119 ms | 5096 KiB |
| 03-highly_composite-020.txt | AC | 120 ms | 4856 KiB |
| 03-highly_composite-021.txt | AC | 119 ms | 4996 KiB |
| 03-highly_composite-022.txt | AC | 118 ms | 4996 KiB |
| 03-highly_composite-023.txt | AC | 140 ms | 5044 KiB |
| 03-highly_composite-024.txt | AC | 126 ms | 4868 KiB |
| 03-highly_composite-025.txt | AC | 133 ms | 5520 KiB |
| 03-highly_composite-026.txt | AC | 135 ms | 5700 KiB |
| 03-highly_composite-027.txt | AC | 131 ms | 5528 KiB |
| 03-highly_composite-028.txt | AC | 126 ms | 5648 KiB |
| 03-highly_composite-029.txt | AC | 130 ms | 5648 KiB |
| 03-highly_composite-030.txt | AC | 129 ms | 5556 KiB |
| 03-highly_composite-031.txt | AC | 126 ms | 5752 KiB |
| 03-highly_composite-032.txt | AC | 126 ms | 5648 KiB |
| 03-highly_composite-033.txt | AC | 122 ms | 5556 KiB |
| 03-highly_composite-034.txt | AC | 122 ms | 5560 KiB |
| 03-highly_composite-035.txt | AC | 138 ms | 5560 KiB |
| 03-highly_composite-036.txt | AC | 134 ms | 5652 KiB |
| 04-line-001.txt | AC | 116 ms | 5528 KiB |
| 04-line-002.txt | AC | 115 ms | 5664 KiB |
| 04-line-003.txt | AC | 122 ms | 5512 KiB |
| 04-line-004.txt | AC | 118 ms | 5652 KiB |
| 04-line-005.txt | AC | 117 ms | 6036 KiB |
| 04-line-006.txt | AC | 117 ms | 5900 KiB |
| 04-line-007.txt | AC | 119 ms | 6132 KiB |
| 04-line-008.txt | AC | 119 ms | 6144 KiB |