# Travel around the world

### Problem Statement :

```There are N cities and N directed roads in Steven's world. The cities are numbered from 0 to N - 1. Steven can travel from city i to city (i + 1) % N, ( 0-> 1 -> 2 -> .... -> N - 1 -> 0).

Steven wants to travel around the world by car. The capacity of his car's fuel tank is C gallons. There are a[i] gallons he can use at the beginning of city i and the car takes b[i] gallons to travel from city i to (i + 1) % N.

How many cities can Steven start his car from so that he can travel around the world and reach the same city he started?

Note

The fuel tank is initially empty.

Input Format

The first line contains two integers (separated by a space): city number N and capacity C.
The second line contains N space-separated integers: a[0], a[1], … , a[N - 1].
The third line contains N space-separated integers: b[0], b[1], … , b[N - 1].

Constraints

2 ≤ N ≤ 105
1 ≤ C ≤ 1018
0 ≤ a[i], b[i] ≤ 109

Output Format

The number of cities which can be chosen as the start city.```

### Solution :

```                            ```Solution in C :

In C++ :

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define output(x) cout << #x << ": " << (x) << endl;

typedef long long LL;
typedef vector<int> VI;
typedef vector<long long> VL;
typedef vector<double> VD;
typedef vector<string> VS;

const int max_n = 100000 + 10;

int n, a[max_n], b[max_n];
LL c, fuel[max_n];

int get_start() {
int valid_start = -1;
int start_city = 0, current_city = 0;
LL current_fuel = 0;
for (int i = 0; i < n * 2 + 10; ++i) {
fuel[current_city] = current_fuel;
int next_city = (current_city + 1) % n;
LL next_fuel = min(current_fuel + a[current_city], c) - b[current_city];
if (next_fuel < 0) {
start_city = current_city = next_city;
current_fuel = 0;
} else if (next_city == start_city) {
valid_start = start_city;
break;
} else {
current_city = next_city;
current_fuel = next_fuel;
}
}
return valid_start;
}

int count_start() {
int start = get_start();
if (start == -1)
return 0;
int current = start;
while (true) {
int pre = (current + n - 1) % n;
LL tmp = min(max(fuel[current] + b[pre] - a[pre], 0LL), c);
if (tmp == fuel[pre])
break;
fuel[pre] = tmp;
current = pre;
}
return count(fuel, fuel + n, 0);
}

void solve() {
cin >> n >> c;
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]);
for (int i = 0; i < n; ++i)
scanf("%d", &b[i]);
printf("%d\n", count_start());
}

int main() {
solve();
return 0;
}

In Java :

import java.util.Scanner;

public class Solution {

public static void main(String args[]) {

Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();
long c = scanner.nextLong();
int a[] = new int[n];
int b[] = new int[n];

for (int i = 0; i < n; i++)
a[i] = scanner.nextInt();

for (int i = 0; i < n; i++)
b[i] = scanner.nextInt();

int currInd = 0;
long fuel = 0;

for (int i = 0; i < n; i++) {
//            System.out.println("i is " + i);
int j = 0;
while (j < n) {
//                System.out.println(i + " " + j);
fuel += a[i % n];
fuel = Math.min(fuel, c);

if (fuel >= b[i % n])
fuel -= b[i % n];
else {
fuel = 0;
break;
}
i++;
j++;
}

if (j == n)
currInd = i % n;
else currInd = -1;
}

//        System.out.println("index is " + currInd);

int res = 0;
if (currInd >= 0) {
res = 1;
long ans[] = new long[n];
ans[currInd] = 0;
for (int j = 0; j < n - 1; j++) {
int i = (currInd - j - 1 + n) % n;
if (Math.min(a[i], c) - b[i] >= ans[(i + 1) % n]) {
ans[i] = 0;
res++;
} else {
ans[i] = ans[(i + 1) % n] - (Math.min(a[i], c) - b[i]);
}
//                System.out.println(i + " " + ans[i]);
}
}

System.out.println(res);
}
}

In C :

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int N,i,flag=0;
long long C,*dp;
int *a,*b,ans=0;
scanf("%d%lld",&N,&C);
a=(int*)malloc(N*sizeof(int));
b=(int*)malloc(N*sizeof(int));
dp=(long long*)malloc(2*N*sizeof(long long));
for(i=0;i<N;i++)
scanf("%d",a+i);
for(i=0;i<N;i++)
scanf("%d",b+i);
for(i=0;i<2*N;i++)
dp[i]=1;
for(i=0;i<N;i++){
if(!i)
dp[i]=0;
else if(a[N-1-i]<=C)
dp[i]=dp[i-1]-a[N-1-i]+b[N-1-i];
else
dp[i]=dp[i-1]-C+b[N-1-i];
if(dp[i]<0)dp[i]=0;
if(dp[i]+((a[N-1-i]>C)?C:a[N-1-i])>C){
flag=1;
//printf("break at %d\n",i);
break;
}
}
if(!flag){
for(i=0;i<N;i++){
if(a[N-1-i]<=C)
dp[i+N]=dp[i-1+N]-a[N-1-i]+b[N-1-i];
else dp[i+N]=dp[i-1+N]-C+b[N-1-i];
if(dp[i+N]<0)dp[i+N]=0;
if(dp[i+N]+((a[N-1-i]>C)?C:a[N-1-i])>C){
dp[i+N]=1;//printf("break at %d\n",i);
break;
}
}

}

//for(i=0;i<2*N;i++)
//printf("%lld ",dp[i]);
//printf("\n");

for(i=0;i<N;i++)
if(dp[i+N]<=0)
ans++;
printf("%d",ans);
return 0;
}

In Python3 :

#!/usr/bin/env python3

def travel(cap, fuel, costs):
fuel = list(fuel)
costs = list(costs)

n = len(fuel)
req = [0] * n

# first iter
for _ in range(2):
for i in reversed(range(n)):
nexti = (i + 1) % n
req[i] = max(0, req[nexti] + costs[i] - fuel[i])
if min(req[i] + fuel[i], cap) - costs[i] < req[nexti]:
return 0

return sum(1 for r in req if r == 0)

def main():
cities, capacity = map(int, input().split())
fuel = map(int, input().split())
costs = map(int, input().split())
print(travel(capacity, fuel, costs))

if __name__ == '__main__':
main()```
```

## Counting On a Tree

Taylor loves trees, and this new challenge has him stumped! Consider a tree, t, consisting of n nodes. Each node is numbered from 1 to n, and each node i has an integer, ci, attached to it. A query on tree t takes the form w x y z. To process a query, you must print the count of ordered pairs of integers ( i , j ) such that the following four conditions are all satisfied: the path from n

## Polynomial Division

Consider a sequence, c0, c1, . . . , cn-1 , and a polynomial of degree 1 defined as Q(x ) = a * x + b. You must perform q queries on the sequence, where each query is one of the following two types: 1 i x: Replace ci with x. 2 l r: Consider the polynomial and determine whether is divisible by over the field , where . In other words, check if there exists a polynomial with integer coefficie

## Costly Intervals

Given an array, your goal is to find, for each element, the largest subarray containing it whose cost is at least k. Specifically, let A = [A1, A2, . . . , An ] be an array of length n, and let be the subarray from index l to index r. Also, Let MAX( l, r ) be the largest number in Al. . . r. Let MIN( l, r ) be the smallest number in Al . . .r . Let OR( l , r ) be the bitwise OR of the

## The Strange Function

One of the most important skills a programmer needs to learn early on is the ability to pose a problem in an abstract way. This skill is important not just for researchers but also in applied fields like software engineering and web development. You are able to solve most of a problem, except for one last subproblem, which you have posed in an abstract way as follows: Given an array consisting

## Self-Driving Bus

Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever

## Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti