HDU 3074 Multiply game

52 篇文章 0 订阅
35 篇文章 0 订阅

Problem Description

Tired of playing computer games, alpc23 is planning to play a game on numbers. Because plus and subtraction is too easy for this gay, he wants to do some multiplication in a number sequence. After playing it a few times, he has found it is also too boring. So he plan to do a more challenge job: he wants to change several numbers in this sequence and also work out the multiplication of all the number in a subsequence of the whole sequence.
  To be a friend of this gay, you have been invented by him to play this interesting game with him. Of course, you need to work out the answers faster than him to get a free lunch, He he…

Input

The first line is the number of case T (T<=10).
  For each test case, the first line is the length of sequence n (n<=50000), the second line has n numbers, they are the initial n numbers of the sequence a1,a2, …,an, 
Then the third line is the number of operation q (q<=50000), from the fourth line to the q+3 line are the description of the q operations. They are the one of the two forms:
0 k1 k2; you need to work out the multiplication of the subsequence from k1 to k2, inclusive. (1<=k1<=k2<=n) 
1 k p; the kth number of the sequence has been change to p. (1<=k<=n)
You can assume that all the numbers before and after the replacement are no larger than 1 million.

Output

For each of the first operation, you need to output the answer of multiplication in each line, because the answer can be very large, so can only output the answer after mod 1000000007.

Sample Input

1
6
1 2 4 5 6 3
3
0 2 5
1 3 7
0 2 5

Sample Output

240

420

zkw线段树

#include<iostream>  
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int maxn = 50005;
const int base = 1000000007;
int n, t, m, l, r, M;
long long a[4 * maxn];

void change(int x,int y)
{
	x = x + M;	a[x] = y;
	for (x >>= 1; x; x >>= 1) a[x] = a[x + x] * a[x + x + 1] % base;
}

long long find(int l, int r)
{
	long long tot = 1;
	for (l += M - 1, r += M + 1; l ^ r ^ 1; l >>= 1, r >>= 1)
	{
		if (~l & 1) tot = tot * a[l ^ 1] % base;
		if ( r & 1) tot = tot * a[r ^ 1] % base;
	}
	return tot;
}

int main(){
	while (~scanf("%d", &t))
	{	
		while (t--){
			memset(a, 0, sizeof(a));
			scanf("%d", &n);	for (M = 1; M < n; M += M);
			for (int i = 1; i <= n; i++) { scanf("%d", &m); change(i, m); }
			scanf("%d", &n);
			for (int i = 1; i <= n; i++)
			{
				scanf("%d%d%d", &m, &l, &r);
				if (m) change(l, r); else cout << find(l, r) << endl;
			}
		}
	}
	return 0;
}
膜拜一下zkw神犇,普通的线段树以后再补。
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
using namespace std;
const long long base = 1000000007;
const long long maxn = 50005;
const long long low(long long x){ return x&-x; }
long long T, x, y, n, m, f[maxn], X, Y, Z, a[maxn];

long long exgcd(long long a, long long b, long long &x, long long &y)
{
	if (b == 0)
	{
		x = 1; y = 0;
		return a;
	}
	long long gcd = exgcd(b, a%b, x, y);
	long long t = x;
	x = y;
	y = t - (a / b)*y;
	return gcd;
}

long long mod(const long long a)
{
	return (a % base + base) % base;
}

long long sum(long long x)
{
	long long tot = 1;
	for (long long i = x; i; i -= low(i)) tot = mod(tot*f[i]);
	return tot;
}

void change(long long x, long long y)
{
	for (long long i = x; i <= n; i += low(i)) f[i] = mod(f[i] * y);
}

int main()
{
	scanf("%lld", &T);
	while (T--)
	{
		scanf("%lld", &n);
		for (long long i = 0; i <= n; i++) f[i] = 1;
		for (long long i = 1; i <= n; i++)
		{
			scanf("%lld", &a[i]);
			change(i, a[i]);
		}
		scanf("%lld", &m);
		while (m--)
		{
			scanf("%lld%lld%lld", &X, &Y, &Z);
			if (X) {
				exgcd(a[Y], base, x, y);
				change(Y, mod(Z*x));
				a[Y] = Z;
			}
			else
			{
				exgcd(sum(Y - 1), base, x, y);
				cout << mod(sum(Z)*x) << endl;
			}
		}
	}
}
扩展欧几里得算法求逆元。
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
using namespace std;
const long long base = 1000000007;
const long long maxn = 50005;
const long long low(long long x){ return x&-x; }
long long T, x, y, n, m, f[maxn], X, Y, Z, a[maxn];

long long mod(const long long a)
{
	return (a % base + base) % base;
}

long long inv(long long x)
{
	long long tot = 1, i = base - 2, ans = x;
	while (i)
	{
		if (i & 1) tot = mod(tot * ans);
		ans = mod(ans * ans);	i >>= 1;
	}
	return mod(tot);
}

long long sum(long long x)
{
	long long tot = 1;
	for (long long i = x; i; i -= low(i)) tot = mod(tot*f[i]);
	return tot;
}

void change(long long x, long long y)
{
	for (long long i = x; i <= n; i += low(i)) f[i] = mod(f[i] * y);
}

int main()
{
	scanf("%lld", &T);
	while (T--)
	{
		scanf("%lld", &n);
		for (long long i = 0; i <= n; i++) f[i] = 1;
		for (long long i = 1; i <= n; i++)
		{
			scanf("%lld", &a[i]);
			change(i, a[i]);
		}
		scanf("%lld", &m);
		while (m--)
		{
			scanf("%lld%lld%lld", &X, &Y, &Z);
			if (X) {
				x = inv(a[Y]);
				change(Y, mod(Z*x));
				a[Y] = Z;
			}
			else
			{
				x = inv(sum(Y - 1));
				cout << mod(sum(Z)*x) << endl;
			}
		}
	}
}

欧拉定理
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
using namespace std;
const long long base = 1000000007;
const long long maxn = 50005;
const long long low(long long x){ return x&-x; }
long long T, x, y, n, m, f[maxn], X, Y, Z, a[maxn];

long long mod(const long long a)
{
	return (a % base + base) % base;
}

long long inv(long long x, long long m)
{
	if (x == 1) return x;
	return inv(m % x, m)*(m - m / x) % m;
}

long long sum(long long x)
{
	long long tot = 1;
	for (long long i = x; i; i -= low(i)) tot = mod(tot*f[i]);
	return tot;
}

void change(long long x, long long y)
{
	for (long long i = x; i <= n; i += low(i)) f[i] = mod(f[i] * y);
}

int main()
{
	scanf("%lld", &T);
	while (T--)
	{
		scanf("%lld", &n);
		for (long long i = 0; i <= n; i++) f[i] = 1;
		for (long long i = 1; i <= n; i++)
		{
			scanf("%lld", &a[i]);
			change(i, a[i]);
		}
		scanf("%lld", &m);
		while (m--)
		{
			scanf("%lld%lld%lld", &X, &Y, &Z);
			if (X) {
				x = inv(a[Y], base);
				change(Y, mod(Z*x));
				a[Y] = Z;
			}
			else
			{
				x = inv(sum(Y - 1), base);
				cout << mod(sum(Z)*x) << endl;
			}
		}
	}
}

精简的扩展gcd

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值