Arithmetic Progressions


Problem Statement :


Let F(a,d) denote an arithmetic progression (AP) with first term  and common difference , i.e.  denotes an infinite . You are given  APs => . Let  denote the sequence obtained by multiplying these APs.

Multiplication of two sequences is defined as follows. Let the terms of the first sequence be , and terms of the second sequence be . The sequence obtained by multiplying these two sequences is
If  are the terms of a sequence, then the terms of the first difference of this sequence are given by  calculated as  respectively. Similarly, the second difference is given by , and so on.

We say that the  difference of a sequence is a constant if all the terms of the  difference are equal.

Let  be a sequence defined as => 
Similarly,  is defined as => product of .

Task:
Can you find the smallest  for which the  difference of the sequence  is a constant? You are also required to find this constant value.

You will be given many operations. Each operation is of one of the two forms:

1) 0 i j => 0 indicates a query . You are required to find the smallest  for which the  difference of  is a constant. You should also output this constant value.

2) 1 i j v => 1 indicates an update . For all , we update .

Input Format
The first line of input contains a single integer , denoting the number of APs.
Each of the next  lines consists of three integers  .
The next line consists of a single integer , denoting the number of operations. Each of the next  lines consist of one of the two operations mentioned above.  

Output Format
For each query, output a single line containing two space-separated integers  and .  is the smallest value for which the  difference of the required sequence is a constant.  is the value of this constant. Since  might be large, output the value of  modulo 1000003.

Note:  will always be such that it fits into a signed 64-bit integer. All indices for query and update are 1-based. Do not take modulo 1000003 for .



Solution :



title-img


                            Solution in C :

In    C++  :









#include <cstdio>
#include <cstring>
#include <string>
using namespace std;

const int N=100005;
const int M=1000003;
typedef long long i64;

struct node {
int ans;
int mul;
i64 sump;
i64 c;
};

node tree[N<<2];
int f[M];

int mul(i64 x,i64 y) {
return x*y%M;
}

int gao(int x,int y) {
if (x==0) {
return 0;
}
if (y==0) {
return 1;
}
if (y&1) {
y=gao(x,y-1);
return mul(y,x);
}
y=gao(x,y>>1);
return mul(y,y);
}

void build(int ind,int left,int right) {
int mid;
tree[ind].ans=1;  
tree[ind].mul=1; 
tree[ind].sump=0; 
tree[ind].c=0;
if (left==right) {
return;
}
mid=(left+right)>>1;
build(ind<<1,left,mid);
build((ind<<1)|1,mid+1,right);
}

void update(int ind,int left,int right,
int ll,int rr,int x) {  
int mid,lson,rson;
if ((left==ll) && (right==rr)) {
tree[ind].sump+=(rr-ll+1)*x;
tree[ind].ans=mul(tree[ind].ans,gao(tree[ind].mul,x));
tree[ind].c+=x; 
return;
}
mid=(left+right)>>1;
lson=ind<<1;
rson=lson|1;
if (tree[ind].c) {   
tree[lson].sump+=(mid-left+1)*tree[ind].c;
tree[rson].sump+=(right-mid)*tree[ind].c;
tree[lson].ans=mul(tree[lson].ans,gao(
    tree[lson].mul,tree[ind].c));
tree[rson].ans=mul(tree[rson].ans,gao(
    tree[rson].mul,tree[ind].c));
tree[lson].c+=tree[ind].c;
tree[rson].c+=tree[ind].c;
tree[ind].c=0;
}  
if (rr<=mid) {
update(lson,left,mid,ll,rr,x);
}
else if (ll>mid) {
update(rson,mid+1,right,ll,rr,x);
}
else {
update(lson,left,mid,ll,mid,x);
update(rson,mid+1,right,mid+1,rr,x);
}
tree[ind].ans=mul(tree[lson].ans,tree[rson].ans);
tree[ind].sump=tree[lson].sump+tree[rson].sump;
}

void insert(int ind,int left,int right,
int x,int p,int d,int dp) { 
int mid;
tree[ind].sump+=p;
tree[ind].mul=mul(tree[ind].mul,d);
tree[ind].ans=mul(tree[ind].ans,dp);
if (left==right) {
return;
}
mid=(left+right)>>1;
if (x<=mid) {
insert(ind<<1,left,mid,x,p,d,dp);
}
else {
insert((ind<<1)|1,mid+1,right,x,p,d,dp);
}
}

pair<i64,int> query(int ind,int left,
int right,int ll,int rr) {
int mid,lson,rson;
pair<i64,int> templ,tempr;
if ((left==ll) && (right==rr)) {
return make_pair(tree[ind].sump,tree[ind].ans);
}
mid=(left+right)>>1;
lson=ind<<1;
rson=lson|1;
if (tree[ind].c) {   
tree[lson].sump+=(mid-left+1)*tree[ind].c;
tree[rson].sump+=(right-mid)*tree[ind].c;
tree[lson].ans=mul(
tree[lson].ans,gao(
tree[lson].mul,tree[ind].c));
tree[rson].ans=mul(
tree[rson].ans,gao(tree[rson].mul,tree[ind].c));
tree[lson].c+=tree[ind].c;
tree[rson].c+=tree[ind].c;
tree[ind].c=0;
}  
if (rr<=mid) {
return query(lson,left,mid,ll,rr);
}
else if (ll>mid) {
return query(rson,mid+1,right,ll,rr);
}
else {
templ=query(lson,left,mid,ll,mid);
tempr=query(rson,mid+1,right,mid+1,rr);
return make_pair(templ.first+tempr.first,
mul(templ.second,tempr.second));
}


}

int main() {
pair<i64,int> ans;
int i,j,x,y,n;
for (i=f[0]=1;i<M;++i) {
f[i]=mul(f[i-1],i);
}
scanf("%d",&n);
build(1,1,n);
for (i=1;i<=n;++i) {
scanf("%d",&x);
scanf("%d%d",&x,&y);
insert(1,1,n,i,y,x,gao(x,y));
}
for (scanf("%d",&i);i;--i) {
scanf("%d%d%d",&j,&x,&y);
if (j==0) {
ans=query(1,1,n,x,y);
printf("%Ld %d\n",
ans.first,(ans.first>=M)?0:mul(f[ans.first],ans.second));
}
else {
scanf("%d",&j);
update(1,1,n,x,y,j);
}
}
return 0;
}









In      Java  :







import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Solution {
private static class SumTree {
private final int n;
private final long[] elements;
private final long[] operations;
private final long[] source;

public SumTree(long[] elements) {
n = elements.length;
this.elements = new long[4 * n + 1];
this.operations = new long[4 * n + 1];
this.source = new long[n];

initFromArray(elements, 0, n, 0);
}

protected long monoidSum(long arg1, long arg2) {
return arg1 + arg2;
}

protected long operationSum(long arg1, 
long arg2) {
return arg1 + arg2;
}

protected long applyOperation(long monoidSum, 
int l, int r, long operation) {
return monoidSum + (r - l) * operation;
}

// all [l,r)
private void initFromArray(long[] elements,
 int l, int r, int num) {
if (r == l + 1) {
this.elements[num] = elements[l];
this.source[l] = elements[l];
} else {
int m = (l + r) / 2;
initFromArray(elements, l, m, num * 2 + 1);
initFromArray(elements, m, r, num * 2 + 2);
this.elements[num] = monoidSum(this.elements[num * 2 + 1],
 this.elements[num * 2 + 2]);
}
}

private long getTransformedSum(int l, int r, 
int ll, int rr, int num) {
if ((l == ll) && (r == rr)) {
return elements[num];
} else {
int m = (ll + rr) / 2;
long interimResult;
if (r <= m) {
interimResult = getTransformedSum(l, r, ll,
 m, num * 2 + 1);
} else if (l >= m) {
interimResult = getTransformedSum(l, r, m, 
rr, num * 2 + 2);
} else {
interimResult = monoidSum(
getTransformedSum(l, m, ll, m, num * 2 + 1),
getTransformedSum(m, r, m, rr, num * 2 + 2)
);
}
return applyOperation(interimResult, l, r, 
operations[num]);
}
}

public long getTransformedSum(int l, int r) {
return getTransformedSum(l, r, 0, n, 0);
}

private void applyOperation(int l, int r, int ll,
 int rr, int num, long operation) {
if ((l == ll) && (r == rr)) {
operations[num] = operationSum(operations[num], operation);
} else {
int m = (ll + rr) / 2;
if (l < m) {
applyOperation(l, Math.min(m, r), ll, m, 
num * 2 + 1, operation);
}
if (m < r) {
applyOperation(Math.max(m, l), r, m, rr, 
num * 2 + 2, operation);
}
}
if (rr == ll + 1) {
elements[num] = applyOperation(source[l], ll,
 rr, operations[num]);
} else {
elements[num] = applyOperation(monoidSum(
    elements[num * 2 + 1], elements[num * 2 + 2]), 
    ll, rr, operations[num]);
}
}

public void applyOperation(int l, int r, 
long operation) {
applyOperation(l, r, 0, n, 0, operation);
}
}

private static class MultTree {
private static final long[] INVERSE = 
new long[(int) MODULO];

static {
int v = 1;
int modulo = (int) MODULO;
int[] powers = new int[modulo];
for (int i = 0; i < modulo; i++) {
powers[i] = v;
v = (v * 2) % modulo;
}
for (int i = 0; i < modulo; i++) {
INVERSE[powers[i]] = powers[modulo - 1 - i];
}
}

private final int n;
private final int[] elements;

public MultTree(long[] elements) {
n = elements.length;
this.elements = new int[n];

long current = 1;
for (int i = 0; i < elements.length; i++) {
current = (current * elements[i]) % MODULO;
this.elements[i] = (int) current;
}
}

// all [l,r)
public long getTransformedSum(int l, int r) {
if (l == 0) {
return elements[r - 1];
} else {
int rV = elements[r - 1];
int lV = elements[l - 1];
// should return rV-lV
return (1L * rV * INVERSE[lV]) % MODULO;
}
}
}

private static class MainTree {
private final int n;
private final long[] elements;
private final long[] operations;
private final long[] source;

private final MultTree dTree;

// all [l,r)
private void initFromArray(long[] elements,
 int l, int r, int num) {
if (r == l + 1) {
this.elements[num] = elements[l];
this.source[l] = elements[l];
} else {
int m = (l + r) / 2;
initFromArray(elements, l, m, num * 2 + 1);
initFromArray(elements, m, r, num * 2 + 2);
this.elements[num] = monoidSum(this.elements[num * 2 + 1], 
this.elements[num * 2 + 2]);
}
}

private long getTransformedSum(int l, int r, 
int ll, int rr, int num) {
if ((l == ll) && (r == rr)) {
return elements[num];
} else {
int m = (ll + rr) / 2;
long interimResult;
if (r <= m) {
interimResult = getTransformedSum(l, r, ll, 
m, num * 2 + 1);
} else if (l >= m) {
interimResult = getTransformedSum(l, r, m,
 rr, num * 2 + 2);
} else {
interimResult = monoidSum(
getTransformedSum(l, m, ll, m, num * 2 + 1),
getTransformedSum(m, r, m, rr, num * 2 + 2)
);
}
return applyOperation(interimResult, l, r, 
operations[num]);
}
}

public long getTransformedSum(int l, int r) {
return getTransformedSum(l, r, 0, n, 0);
}

private void applyOperation(int l, int r,
 int ll, int rr, int num, long operation) {
if ((l == ll) && (r == rr)) {
operations[num] = operationSum(operations[num], 
operation);
} else {
int m = (ll + rr) / 2;
if (l < m) {
applyOperation(l, Math.min(m, r), ll, m,
 num * 2 + 1, operation);
}
if (m < r) {
applyOperation(Math.max(m, l), r, m, rr,
 num * 2 + 2, operation);
}
}
if (rr == ll + 1) {
elements[num] = applyOperation(source[l], ll, 
rr, operations[num]);
} else {
elements[num] = applyOperation(monoidSum(
    elements[num * 2 + 1], elements[num * 2 + 2]),
    ll, rr, operations[num]);
}
}

public void applyOperation(int l, int r, long operation) {
applyOperation(l, r, 0, n, 0, operation);
}

public MainTree(long[] elements, MultTree dTree) {
n = elements.length;
this.elements = new long[4 * n + 1];
this.operations = new long[4 * n + 1];
this.source = new long[n];

initFromArray(elements, 0, n, 0);
this.dTree = dTree;
}

protected long monoidSum(long arg1, long arg2) {
return (arg1 * arg2) % MODULO;
}

protected long operationSum(long arg1, long arg2) {
return arg1 + arg2;
}

protected long applyOperation(long monoidSum, 
int l, int r, long operation) {
return (monoidSum * pow(
dTree.getTransformedSum(l, r), operation)) % MODULO;
}
}

private static final long MODULO = 1000003;


private static final long[] FACTORIALS = 
new long[(int) MODULO + 1];

static {
FACTORIALS[0] = 1;
for (int n = 1; n <= MODULO; n++) {
FACTORIALS[n] = (FACTORIALS[n - 1] * n) % MODULO;
}
}

public static long factorial(long n) {
if (n > MODULO) {
return 0;
} else {
return FACTORIALS[(int) n];
}
}

public static long pow(long v, long p) {
if (p == 0) {
return 1;
} else {
long tmp = pow(v, p / 2);
tmp = (tmp * tmp) % MODULO;
return p % 2 == 0 ? tmp : (tmp * v) % MODULO;
}
}

private void run(Scanner input, PrintWriter output) {
int n = input.nextInt();
int[] a = new int[n];
long[] p = new long[n], d = new long[n], 
dp = new long[n];
for (int i = 0; i < n; i++) {
a[i] = input.nextInt();
d[i] = input.nextInt();
p[i] = input.nextInt();

dp[i] = pow(d[i], p[i]);
}

SumTree pTree = new SumTree(p);
MultTree dTree = new MultTree(d);
MainTree cTree = new MainTree(dp, dTree);

int q = input.nextInt();
for (int qNum = 0; qNum < q; qNum++) {
int type = input.nextInt();
int i = input.nextInt() - 1;
int j = input.nextInt() - 1;

if (type == 0) {
long pRes = pTree.getTransformedSum(i, j + 1);
long cRes;

if (pRes > MODULO) {
cRes = 0;
} else {
cRes = cTree.getTransformedSum(i, j + 1);
}

output.println(pRes + " " + (
    (cRes * factorial(pRes)) % MODULO));
} else {
int v = input.nextInt();

pTree.applyOperation(i, j + 1, (long) v);
cTree.applyOperation(i, j + 1, (long) v); 
}
}
}

public static void main(String[] args) throws IOException {
Scanner input = new Scanner(new BufferedInputStream(System.in));
PrintWriter output = new PrintWriter(System.out);

(new Solution()).run(input, output);

output.flush();
}
}











In   C  :







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

long long power(int d,int n)
{
	long long i,temp;
	i=1;
	temp=d;
	while(n>0)
	{
		if(n%2==1) i*=temp;
		temp*=temp;
		if(temp>1000003) temp%=1000003;
		if(i>1000003) i%=1000003;
		n=n/2;
	}
	return i;
}

int main()
{
	int i,j,k,v;
	int s,e;
	int n,q,qr;
	long long temp,temp1;
	int *a,*d,*p;
        long long *nfac;
	nfac=(long long *)malloc(1000003*sizeof(long long));
	nfac[0]=1;
	for(i=1;i<1000003;i++)
	{
		nfac[i]=nfac[i-1]*i;
		if(nfac[i]>1000003) nfac[i]%=1000003;
	}
	scanf("%d",&n);
	a=(int *)malloc(n*sizeof(int));
	d=(int *)malloc(n*sizeof(int));
	p=(int *)malloc(n*sizeof(int));
	for(i=0;i<n;i++) scanf("%d %d %d",&a[i],&d[i],&p[i]);
	scanf("%d",&q);
	for(k=0;k<q;k++)
	{
		scanf("%d",&qr);
		if(qr==0)
		{
			temp=0;
			scanf("%d %d",&s,&e);
			for(i=s;i<=e;i++) temp+=p[i-1];
			printf("%d ",temp);
			if(temp>=1000003) 
			{
				printf("%d\n",0);
			}
			else
			{
				temp1=nfac[temp];
				for(i=s;i<=e;i++)
				{
					temp1*=power(d[i-1],p[i-1]);
					if(temp1>1000003) temp1%=1000003;
				}
				printf("%d\n",temp1);
			}
		}
		else
		{
			scanf("%d %d %d",&s,&e,&v);
			for(i=s;i<=e;i++) p[i-1]+=v;
		}
	}
}









In   Python3  :









from array import array

f = array('i', [1])

def mod_factorial(n, r):
    if n < len(f):
        return f[n]
    res = f[-1]
    for i in range(len(f), n+1):
        res = (res*i) % r
        f.append(res)
    return f[-1]

r = 1000003

n = int(input())

d = array('i')
p = array('i')
dp = array('i', [-1])*n
for _ in range(n):
    a, b, c = map(int, input().split())
    d.append(b)
    p.append(c)

q = int(input())

for _ in range(q):
    a = input().split()
    if a[0] == '0':
        i, j = map(int, a[1:])
        k = sum(p[i-1:j])
        v = mod_factorial(k, r)
        for m in range(i-1, j):
            if dp[m] < 0:
                dp[m] = pow(d[m], p[m], r)
            v = (v * dp[m]) % r
                
        print(k, v)
    else:
        i, j, v = map(int, a[1:])
        for k in range(i-1, j):
            p[k] += v
            dp[k] = -1
                        








View More Similar Problems

Array and simple queries

Given two numbers N and M. N indicates the number of elements in the array A[](1-indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] . You are given queries. Queries can be of two types, type 1 and type 2. Type 1 queries are represented as 1 i j : Modify the given array by removing elements from i to j and adding them to the front. Ty

View Solution →

Median Updates

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

View Solution →

Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

View Solution →

Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

View Solution →

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 →