Polynomial Division


Problem Statement :


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 coefficients such that each coefficient of  is divisible by Q( x ) =  a*x + b. If a valid  exists, print Yes on a new line; otherwise, print No.
Given the values of , , , and  queries, perform each query in order.

Input Format

The first line contains four space-separated integers describing the respective values of n (the length of the sequence),  a (a coefficient in Q( x ) ),  b (a coefficient in Q( x )  ), and  q(the number of queries).
The second line contains n space-separated integers describing c0, c1, . . . , cn-1.
Each of the q subsequent lines contains three space-separated integers describing a query of either type 1 or type 2.


Output Format

For each query of type 2, print Yes on a new line if Q(x) is a divisor of P(x); otherwise, print No instead.

Sample Input 0

3 2 2 3
1 2 3
2 0 2
1 2 1
2 0 2
Sample Output 0

No
Yes



Solution :



title-img


                            Solution in C :

In C ++ :




#include <bits/stdc++.h>
using namespace std;
typedef pair<long long int,long long int> pll;
const int T = (1<<17);
const long long int MOD = 1000000007;
long long int X;
pll seg[2*T];
long long int power(long long int a, int b)
{
	if(!b)
		return 1;
	long long int ans = power(a,b/2);
	ans = (ans*ans)%MOD;
	if(b%2)
		ans = (ans*a)%MOD;
	return ans;
}
pll seg_merge(pll &v1, pll &v2)
{
	pll ret;
	ret.first = (v1.first + v2.first*power(X,v1.second))%MOD;
	ret.second = v1.second + v2.second;
	return ret;
}
pll que(int root, int lm, int rm, int u, int v)
{
	if(u <= lm && rm <= v)
		return seg[root];
	int mid = (lm + rm)/2;
	if(u <= mid)
	{
		pll lval = que(2*root, lm, mid, u, v);
		if(mid < v)
		{
			pll rval = que(2*root+1, mid+1, rm, u, v);
			return seg_merge(lval,rval);
		}
		return lval;
	}
	pll rval = que(2*root+1, mid+1, rm, u, v);
	return rval;
}
int main()
{
	int n,a,b,q;
	scanf("%d %d %d %d", &n, &a, &b, &q);
	X = ((MOD - b)*power(a,MOD-2))%MOD;
	for (int i = 0; i < n; ++i)
	{
		scanf("%lld", &seg[T+i].first);
		seg[T+i].second = 1;
	}
	for (int i = T-1; i >= 1; --i)
		seg[i] = seg_merge(seg[2*i],seg[2*i+1]);
	while(q--)
	{
		int ch;
		scanf("%d", &ch);
		if(ch == 1)
		{
			int pos, val;
			scanf("%d %d", &pos, &val);
			pos+=T;
			seg[pos].first = val;
			pos/=2;
			while(pos)
			{
				seg[pos] = seg_merge(seg[2*pos],seg[2*pos+1]);
				pos/=2;
			}
		}
		else
		{
			int l,r;
			scanf("%d %d", &l, &r);
			l+=T;
			r+=T;
			pll ans = que(1, T, 2*T-1, l, r);
			if(ans.first)
				printf("No\n");
			else
				printf("Yes\n");
		}
	}
	return 0;
}







In Java :





import java.io.*;
import java.util.*;

public class D {

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	static final int P = 1_000_000_007;

	int pow(int a, int b) {
		int ret = 1;
		for (; b > 0; b >>= 1) {
			if ((b & 1) == 1) {
				ret = (int) ((long) ret * a % P);
			}
			a = (int) ((long) a * a % P);
		}
		return ret;
	}

	void add(long[] f, int pos, long delta) {
		for (int i = pos; i < f.length; i |= i + 1) {
			f[i] += delta;
		}
	}

	long get(long[] f, int pos) {
		long ret = 0;
		for (int i = pos; i >= 0; i = (i & (i + 1)) - 1) {
			ret += f[i];
		}
		return ret;
	}

	void solve() throws IOException {
		int n = nextInt();
		int a = nextInt();
		int b = nextInt();
		int q = nextInt();

		int x;
		if (a == 0) {
			x = 1;
		} else {
			x = (int) ((long) b * pow(a, P - 2) % P);
			if (x != 0) {
				x = P - x;
			}
		}

		int[] pow = new int[n];
		pow[0] = 1;
		for (int i = 1; i < n; i++) {
			pow[i] = (int) ((long) pow[i - 1] * x % P);
		}

		int[] arr = new int[n];
		int[] arrX = new int[n];
		long[] fen = new long[n];

		for (int i = 0; i < n; i++) {
			arr[i] = nextInt();
			arrX[i] = (int) ((long) arr[i] * pow[i] % P);
			add(fen, i, arrX[i]);
		}

		for (int i = 0; i < q; i++) {
			int type = nextInt();
			if (type == 1) {
				int pos = nextInt();
				int val = nextInt();
				add(fen, pos, -arrX[pos]);

				arr[pos] = val;
				arrX[pos] = (int) ((long) val * pow[pos] % P);
				add(fen, pos, arrX[pos]);
			} else {
				int l = nextInt();
				int r = nextInt();
				
				// [l; r]
				
				long sumR = get(fen, r);
				long sumL = get(fen, l - 1);
				
				if (a == 0 && b == 0) {
					out.println(sumR - sumL == 0 ? "Yes" : "No");
				} else if (a == 0 && b != 0) {
					out.println("Yes");
				} else if (a != 0 && b == 0) {
					out.println(arr[l] == 0 ? "Yes" : "No");
				} else {
					out.println((sumR - sumL) % P == 0 ? "Yes" : "No");
				}
			}
		}

	}

	D() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new D();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}







In C :






#include <stdio.h>
#include <stdlib.h>
#define MOD 1000000007
void build(int v,int tl,int tr);
void update(int v,int tl,int tr,int pos,int new_val);
long long sum(int v,int tl,int tr,int l,int r);
long long modInverse(long long a,long long mod);
int min(int x,int y);
int max(int x,int y);
int c[100000];
long long dp[100000],t[400000];

int main(){
  int n,a,b,q,x,y,a_inv,i;
  scanf("%d%d%d%d",&n,&a,&b,&q);
  a_inv=modInverse(a,MOD);
  for(i=dp[0]=1;i<100000;i++)
    dp[i]=(MOD-dp[i-1]*b%MOD*a_inv%MOD)%MOD;
  for(i=0;i<n;i++)
    scanf("%d",c+i);
  build(1,0,n-1);
  while(q--){
    scanf("%d",&x);
    if(x==1){
      scanf("%d%d",&x,&y);
      update(1,0,n-1,x,y);
    }
    else{
      scanf("%d%d",&x,&y);
      if(sum(1,0,n-1,x,y))
        printf("No\n");
      else
        printf("Yes\n");
    }
  }
  return 0;
}
void build(int v,int tl,int tr){
  int tm;
  if(tl==tr)
    t[v]=c[tl];
  else{
    tm=(tl+tr)/2;
    build(v*2,tl,tm);
    build(v*2+1,tm+1,tr);
    t[v]=(t[v*2]+t[v*2+1]*dp[tm-tl+1])%MOD;
  }
  return;
}
void update(int v,int tl,int tr,int pos,int new_val){
  int tm;
  if(tl==tr)
    t[v]=new_val;
  else{
    tm=(tl+tr)/2;
    if(pos<=tm)
      update(v*2,tl,tm,pos,new_val);
    else
      update(v*2+1,tm+1,tr,pos,new_val);
    t[v]=(t[v*2]+t[v*2+1]*dp[tm-tl+1])%MOD;
  }
  return;
}
long long sum(int v,int tl,int tr,int l,int r){
  int tm,temp;
  if(l>r)
    return 0;
  if(l==tl && r==tr)
    return t[v];
  tm=(tl+tr)/2;
  if(min(r,tm)>=l)
    temp=min(r,tm)-l+1;
  else
    temp=0;
  return (sum(v*2,tl,tm,l,min(r,tm))+sum(v*2+1,tm+1,tr,max(l,tm+1),r)*dp[temp])%MOD;
}
long long modInverse(long long a,long long mod){
    long long b0 = mod, t, q;
    long long x0 = 0, x1 = 1;
    while (a > 1) {
        q = a / mod;
        t = mod; mod = a % mod; a = t;
        t = x0; x0 = x1 - q * x0; x1 = t;
    }
    if (x1 < 0) x1 += b0;
    return x1;
}
int min(int x,int y){
  return (x<y)?x:y;
}
int max(int x,int y){
  return (x>y)?x:y;
}













In Python3 :




def init(R, x, p):
   T = [R]
   while len(R) > 1:
      if len(R) & 1: R.append(0)
      R = [(R[i] + x * R[i+1]) % p for i in range(0,len(R),2)]
      x = (x * x) % p
      T.append(R)
   return T
def update(T, i, x, p):
   S = T[0]
   for j in range(1, len(T)):
      R = T[j]
      i >>= 1
      R[i] = (S[2*i] + x*S[2*i+1]) % p
      S = R
      x = (x * x) % p
def query(T, i, x, p):
   if i == 0: return T[-1][0]
   s = 0
   y = 1
   for j in range(len(T)-1):
      if i & 1:
         s = (s + y * T[j][i]) % p
         y = (y * x) % p
      i = (i + 1) >> 1
      x = (x * x) % p
   return s
p = 10**9 + 7
n, a, b, q = map(int, input().split())
c = [int(x) for x in input().split()]
x = (-b * pow(a,p-2,p)) % p
T = init(c, x, p)
for Q in range(q):
   k, a, b = map(int, input().split())
   if k == 1:
      c[a] = b
      update(T, a, x, p)
   elif k == 2:
      y = (query(T,a,x,p) - query(T,b+1,x,p) * pow(x,b-a+1,p)) % p
      print('No' if y else 'Yes')
                        








View More Similar Problems

Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

View Solution →

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →