#include<bits/stdc++.h>
#define int long long
#define f first
#define s second
#define repeat(i,a,b) for(int i=a;i<=b;i++)
#define pb push_back
#define p push
#define pii pair<int,int>
#define piii pair<int,pair<int,int>>
#define vii vector<vector<int>>
#define vi vector<int>
//#define DEBUG
//#define multipletest
using namespace std;
const int LIM=5e4;
const int mod=1e9+7;
const int maxn=20;
const int inf=1e9;
int n,m,x,y,k,t,e,q,o;
int dx[8]={1,-1,0,0,-1,-1,1,1};
int dy[8]={0,0,-1,1,1,-1,1,-1};
char c;
string s;
int a[LIM+5],factorial[(1<<maxn)+5],inv_fact[(1<<maxn)+5];
bool notprime[LIM+5];
map<int,int> mp;
char grid[100+5][100+5];
int power(int a,int b){
if(b==0) return 1;
int t=power(a,b/2);
t = t * t %mod;
if(b%2==1) t = t * a %mod;
return t;
}
struct matrix{
int a[5][5];
matrix(int x=inf){
for(int i=0;i<5;++i){
for(int j=0;j<5;++j){
a[i][j]=x;
}
}
}
matrix operator *(const matrix & u){
matrix ans;
for(int i=0;i<5;++i){
for(int j=0;j<5;++j){
int res=inf;
for(int k=0;k<5;++k){
res = min(res,a[i][k] + u.a[k][j]);
}
ans.a[i][j]=res;
}
}
return ans;
}
};
bool check(int x,int y){
if(x<=0 || x>n || y<=0 || y>m || grid[x][y]=='T'){
return false;
}
return true;
}
class DSU{
int *parent;
int *rank;
int *tot;
public:
DSU(int n){
rank=new int[n+5];
parent=new int[n+5];
tot=new int [n+5];
for(int i=0;i<n+5;i++){
parent[i]=i;
rank[i]=0;
tot[i]=1;
}
}
int find(int i){
if(parent[i]==i){
return i;
}
return parent[i]=find(parent[i]);
}
void unite(int u,int v){
int i=find(u);
int j=find(v);
if(i!=j){
if(rank[i]>rank[j]){
parent[j]=i;
tot[i]+=tot[j];
}
else if(rank[i]<rank[j]){
parent[i]=j;
tot[j]+=tot[i];
}
else{
parent[j]=i;
rank[i]++;
tot[i]+=tot[j];
}
}
}
int total(int u){
return tot[u];
}
};
void precal(int n){
factorial[0]=1;
inv_fact[0]=1;
for (int i = 1; i <n; ++i)
{
factorial[i] = factorial[i - 1] * i % mod;
inv_fact[i] = inv_fact[i - 1] * power(i, mod - 2) % mod;
}
}
int choose(int n,int k){
if(k<0 || k>n) return 0;
return factorial[n] * inv_fact[n-k] % mod * inv_fact[k] %mod;
}
void Sieve(){
notprime[0]=notprime[1]=true;
for(int i=2;i<LIM+5;++i){
if(notprime[i]==false){
for(int j=i*i;j<LIM+5;j+=i){
notprime[j]=true;
}
}
}
}
matrix up[20][LIM+5];
void solve(){
//Sieve();
// precal();
cin>>k>>n>>m>>o;
for(int i=1;i<=m;++i){
int u,v,w;
cin>>u>>v>>w;
up[0][(u/k)].a[u%k][v%k]=w;
}
for(int i=1;((1<<i)<=n);++i){
for(int j=0;j+(1<<i)-1<=n;++j){
up[i][j]=up[i-1][j] * up[i-1][j+(1<<(i-1))];
}
}
for(int i=0;i<o;++i){
int a,b;
cin>>a>>b;
matrix res;
// cout<<a<<b<<endl;
for(int i=0;i<5;++i){
res.a[i][i]=0;
}
if(a==b){
cout<<0<<endl;
}
int div1=(a/k),div2=(b/k);
int dist=div2-div1;
int temp=0;
while(dist>0){
if(dist%2==1) {
res = res * up[temp][div1];
div1 = div1 + (1<<temp);
}
temp++;
dist>>=1;
}
if(res.a[a%k][b%k]==inf) cout<<-1<<endl;
else cout<<res.a[a%k][b%k]<<endl;
}
}
signed main(){
// freopen(".inp","r",stdin);
// freopen(".out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int test;
test=1;
#ifdef multipletest
cin>>test;
#endif
while(test--){
solve();
#ifdef DEBUG
cerr << "Runtime is: " << clock() * 1.0 / CLOCKS_PER_SEC << endl;
#endif
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
196956 KB |
Output is correct |
2 |
Correct |
79 ms |
195996 KB |
Output is correct |
3 |
Correct |
79 ms |
195952 KB |
Output is correct |
4 |
Correct |
81 ms |
196120 KB |
Output is correct |
5 |
Correct |
80 ms |
196028 KB |
Output is correct |
6 |
Correct |
79 ms |
196044 KB |
Output is correct |
7 |
Correct |
79 ms |
195952 KB |
Output is correct |
8 |
Correct |
206 ms |
196940 KB |
Output is correct |
9 |
Correct |
214 ms |
197064 KB |
Output is correct |
10 |
Correct |
194 ms |
196168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
212 ms |
197580 KB |
Output is correct |
2 |
Correct |
77 ms |
196076 KB |
Output is correct |
3 |
Correct |
75 ms |
196008 KB |
Output is correct |
4 |
Correct |
77 ms |
195912 KB |
Output is correct |
5 |
Correct |
77 ms |
195916 KB |
Output is correct |
6 |
Correct |
76 ms |
195988 KB |
Output is correct |
7 |
Correct |
97 ms |
196044 KB |
Output is correct |
8 |
Correct |
94 ms |
196172 KB |
Output is correct |
9 |
Correct |
202 ms |
196880 KB |
Output is correct |
10 |
Correct |
214 ms |
198292 KB |
Output is correct |
11 |
Correct |
222 ms |
197744 KB |
Output is correct |
12 |
Correct |
201 ms |
197316 KB |
Output is correct |
13 |
Correct |
171 ms |
198412 KB |
Output is correct |
14 |
Correct |
151 ms |
197452 KB |
Output is correct |
15 |
Correct |
152 ms |
197240 KB |
Output is correct |
16 |
Correct |
161 ms |
197160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
195916 KB |
Output is correct |
2 |
Correct |
78 ms |
195984 KB |
Output is correct |
3 |
Correct |
81 ms |
195952 KB |
Output is correct |
4 |
Correct |
78 ms |
195916 KB |
Output is correct |
5 |
Correct |
83 ms |
196036 KB |
Output is correct |
6 |
Correct |
78 ms |
195928 KB |
Output is correct |
7 |
Correct |
82 ms |
196044 KB |
Output is correct |
8 |
Correct |
92 ms |
195988 KB |
Output is correct |
9 |
Correct |
94 ms |
196004 KB |
Output is correct |
10 |
Correct |
192 ms |
196720 KB |
Output is correct |
11 |
Correct |
191 ms |
197452 KB |
Output is correct |
12 |
Correct |
212 ms |
198232 KB |
Output is correct |
13 |
Correct |
207 ms |
198444 KB |
Output is correct |
14 |
Correct |
213 ms |
197832 KB |
Output is correct |
15 |
Correct |
199 ms |
197216 KB |
Output is correct |
16 |
Correct |
161 ms |
197208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
195916 KB |
Output is correct |
2 |
Correct |
78 ms |
195984 KB |
Output is correct |
3 |
Correct |
81 ms |
195952 KB |
Output is correct |
4 |
Correct |
78 ms |
195916 KB |
Output is correct |
5 |
Correct |
83 ms |
196036 KB |
Output is correct |
6 |
Correct |
78 ms |
195928 KB |
Output is correct |
7 |
Correct |
82 ms |
196044 KB |
Output is correct |
8 |
Correct |
92 ms |
195988 KB |
Output is correct |
9 |
Correct |
94 ms |
196004 KB |
Output is correct |
10 |
Correct |
192 ms |
196720 KB |
Output is correct |
11 |
Correct |
191 ms |
197452 KB |
Output is correct |
12 |
Correct |
212 ms |
198232 KB |
Output is correct |
13 |
Correct |
207 ms |
198444 KB |
Output is correct |
14 |
Correct |
213 ms |
197832 KB |
Output is correct |
15 |
Correct |
199 ms |
197216 KB |
Output is correct |
16 |
Correct |
161 ms |
197208 KB |
Output is correct |
17 |
Correct |
222 ms |
197584 KB |
Output is correct |
18 |
Correct |
84 ms |
195956 KB |
Output is correct |
19 |
Correct |
93 ms |
195988 KB |
Output is correct |
20 |
Correct |
87 ms |
196020 KB |
Output is correct |
21 |
Correct |
89 ms |
196032 KB |
Output is correct |
22 |
Correct |
86 ms |
195904 KB |
Output is correct |
23 |
Correct |
102 ms |
195968 KB |
Output is correct |
24 |
Correct |
89 ms |
196080 KB |
Output is correct |
25 |
Correct |
95 ms |
196036 KB |
Output is correct |
26 |
Correct |
95 ms |
196060 KB |
Output is correct |
27 |
Correct |
191 ms |
196876 KB |
Output is correct |
28 |
Correct |
211 ms |
198156 KB |
Output is correct |
29 |
Correct |
214 ms |
198492 KB |
Output is correct |
30 |
Correct |
202 ms |
197872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
196956 KB |
Output is correct |
2 |
Correct |
79 ms |
195996 KB |
Output is correct |
3 |
Correct |
79 ms |
195952 KB |
Output is correct |
4 |
Correct |
81 ms |
196120 KB |
Output is correct |
5 |
Correct |
80 ms |
196028 KB |
Output is correct |
6 |
Correct |
79 ms |
196044 KB |
Output is correct |
7 |
Correct |
79 ms |
195952 KB |
Output is correct |
8 |
Correct |
206 ms |
196940 KB |
Output is correct |
9 |
Correct |
214 ms |
197064 KB |
Output is correct |
10 |
Correct |
194 ms |
196168 KB |
Output is correct |
11 |
Correct |
212 ms |
197580 KB |
Output is correct |
12 |
Correct |
77 ms |
196076 KB |
Output is correct |
13 |
Correct |
75 ms |
196008 KB |
Output is correct |
14 |
Correct |
77 ms |
195912 KB |
Output is correct |
15 |
Correct |
77 ms |
195916 KB |
Output is correct |
16 |
Correct |
76 ms |
195988 KB |
Output is correct |
17 |
Correct |
97 ms |
196044 KB |
Output is correct |
18 |
Correct |
94 ms |
196172 KB |
Output is correct |
19 |
Correct |
202 ms |
196880 KB |
Output is correct |
20 |
Correct |
214 ms |
198292 KB |
Output is correct |
21 |
Correct |
222 ms |
197744 KB |
Output is correct |
22 |
Correct |
201 ms |
197316 KB |
Output is correct |
23 |
Correct |
171 ms |
198412 KB |
Output is correct |
24 |
Correct |
151 ms |
197452 KB |
Output is correct |
25 |
Correct |
152 ms |
197240 KB |
Output is correct |
26 |
Correct |
161 ms |
197160 KB |
Output is correct |
27 |
Correct |
82 ms |
195916 KB |
Output is correct |
28 |
Correct |
78 ms |
195984 KB |
Output is correct |
29 |
Correct |
81 ms |
195952 KB |
Output is correct |
30 |
Correct |
78 ms |
195916 KB |
Output is correct |
31 |
Correct |
83 ms |
196036 KB |
Output is correct |
32 |
Correct |
78 ms |
195928 KB |
Output is correct |
33 |
Correct |
82 ms |
196044 KB |
Output is correct |
34 |
Correct |
92 ms |
195988 KB |
Output is correct |
35 |
Correct |
94 ms |
196004 KB |
Output is correct |
36 |
Correct |
192 ms |
196720 KB |
Output is correct |
37 |
Correct |
191 ms |
197452 KB |
Output is correct |
38 |
Correct |
212 ms |
198232 KB |
Output is correct |
39 |
Correct |
207 ms |
198444 KB |
Output is correct |
40 |
Correct |
213 ms |
197832 KB |
Output is correct |
41 |
Correct |
199 ms |
197216 KB |
Output is correct |
42 |
Correct |
161 ms |
197208 KB |
Output is correct |
43 |
Correct |
222 ms |
197584 KB |
Output is correct |
44 |
Correct |
84 ms |
195956 KB |
Output is correct |
45 |
Correct |
93 ms |
195988 KB |
Output is correct |
46 |
Correct |
87 ms |
196020 KB |
Output is correct |
47 |
Correct |
89 ms |
196032 KB |
Output is correct |
48 |
Correct |
86 ms |
195904 KB |
Output is correct |
49 |
Correct |
102 ms |
195968 KB |
Output is correct |
50 |
Correct |
89 ms |
196080 KB |
Output is correct |
51 |
Correct |
95 ms |
196036 KB |
Output is correct |
52 |
Correct |
95 ms |
196060 KB |
Output is correct |
53 |
Correct |
191 ms |
196876 KB |
Output is correct |
54 |
Correct |
211 ms |
198156 KB |
Output is correct |
55 |
Correct |
214 ms |
198492 KB |
Output is correct |
56 |
Correct |
202 ms |
197872 KB |
Output is correct |
57 |
Correct |
243 ms |
199304 KB |
Output is correct |
58 |
Correct |
222 ms |
197068 KB |
Output is correct |
59 |
Correct |
219 ms |
197828 KB |
Output is correct |