CF594D. REQ

CF594D.REQ 题解与算法分析

CF594D. REQ

Solution

离线询问,按左端点排序。
对于每个质因数都会有p−1p\frac{p-1}{p}pp1的贡献,考虑把贡献维护在当前左端点右边最早出现的ppp倍数的位置上。

每次lll增加的时候,把这一位变成1,并把这一位的质因数的贡献往它之后的位置推,对于每组询问求前缀积,树状数组维护即可。

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=1e9+7;
const int MAXN=1000005;
const int INF=0x3f3f3f3f;
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
vector<int> V[MAXN],S[MAXN];
struct Qnode{int l,r,id; } Q[MAXN];
int pnum=0,flag[MAXN],prime[MAXN],a[MAXN],inv[MAXN],Ans[MAXN],n,num[MAXN];
int quick_pow(int x,int y)
{
	int ret=1;
	for (;y;y>>=1)
	{
		if (y&1) ret=1ll*ret*x%mods;
		x=1ll*x*x%mods;
	}
	return ret;
}
void Init(int n,int m)
{
	for (int i=2;i<=m;i++)
	if (!flag[i])
	{
		prime[++pnum]=i; 
		for (int j=i;j<=m;j+=i) flag[j]=pnum;
	}
	
	for (int i=1;i<=n;i++)
	{
		int t=a[i];
		for (int j=1;j<=pnum&&prime[j]*prime[j]<=t;j++)
		if (t%prime[j]==0)
		{
			while (t%prime[j]==0) t/=prime[j];
			V[i].PB(j),S[j].PB(i);
		}
		if (t>1) V[i].PB(flag[t]),S[flag[t]].PB(i);
	}
	
	inv[0]=1;
	for (int i=1;i<=m;i++) inv[i]=quick_pow(i,mods-2);
}

int mul[MAXN];
void add(int x,int y) { if (!x) return; for (;x<=n;x+=x&(-x)) mul[x]=1ll*mul[x]*y%mods; }
int query(int x) { int ans=1; for (;x;x-=x&(-x)) ans=1ll*ans*mul[x]%mods; return ans; }
signed main()
{
	n=read();
	for (int i=1;i<=n;i++) a[i]=read();
	Init(n,1e6);
	
	int m=read();
	for (int i=1;i<=m;i++) Q[i].l=read(),Q[i].r=read(),Q[i].id=i;
	sort(Q+1,Q+m+1,[&](Qnode x,Qnode y){ return (x.l<y.l)||(x.l==y.l&&x.r<y.r); });
	
	for (int i=0;i<=n*2;i++) mul[i]=1;
	for (int i=1;i<=n;i++) 
	{
		add(i,a[i]);
		for (auto v:V[i])
			if (S[v][0]==i) add(i,1ll*(prime[v]-1)*inv[prime[v]]%mods);
	}
	for (int i=1,l=1;i<=m;i++)
	{
		while (l<Q[i].l)
		{
			add(l,inv[a[l]]);
			for (auto v:V[l])
			{
				if (num[v]<S[v].size()-1) add(S[v][num[v]+1],1ll*(prime[v]-1)*inv[prime[v]]%mods),num[v]++;
				add(l,1ll*prime[v]*inv[prime[v]-1]%mods);
			}
			l++;
		}
		Ans[Q[i].id]=query(Q[i].r);
	}
	for (int i=1;i<=m;i++) printf("%d\n",Ans[i]);
	return 0;
}
int cookies_reinit_conn_req() { int ret = 0; if (s_cookie_ctx.conn_req != NULL) { if(s_cookie_ctx.conn_req->param) { free(s_cookie_ctx.conn_req->param); s_cookie_ctx.conn_req->param = NULL; } tcp_connection_free(s_cookie_ctx.conn_req); s_cookie_ctx.conn_req = NULL; } RELAY_DEBUG("updating cookie context.\n"); cookie_update_ctx(); RELAY_DEBUG("initiating cookies requesting connection.\n"); if(0 != (ret = cookies_init_conn_req())) { RELAY_ERROR_LOG("init conn_req fail(erro: %d).\n", ret); } return 0; } static int cookie_update_ctx() { char *relay_address = s_cookie_ctx.relay_address; char *relay_ip = s_cookie_ctx.relay_ip; char curr_relay_address[LEN_MAX_HOSTNAME + 1] = {0}; char curr_relay_ip[RELAY_IP_LEN + 1] = {0}; int curr_relay_port = -1; int ret = 0; ret = get_relay_server_info(curr_relay_address, LEN_MAX_HOSTNAME, curr_relay_ip, RELAY_IP_LEN, &curr_relay_port); if (ret < 0) { RELAY_ERROR("failed when getting relay server info.\n"); return -1; } memset(relay_address, 0, sizeof(s_cookie_ctx.relay_address)); strncpy(relay_address, curr_relay_address, sizeof(s_cookie_ctx.relay_address)); memset(relay_ip, 0, sizeof(s_cookie_ctx.relay_ip)); strncpy(relay_ip, curr_relay_ip, sizeof(s_cookie_ctx.relay_ip)); s_cookie_ctx.relay_port = curr_relay_port; return 0; } int cookies_init_conn_req() { char *relay_address = s_cookie_ctx.relay_address; int relay_port = s_cookie_ctx.relay_port; int ret = 0; webParam_t *param = (webParam_t *)malloc(sizeof(webParam_t)); if(param == NULL) { RTSP_ERROR("alloc memory failed!"); return ERROR_1; } memset(param, 0 , sizeof(webParam_t)); param->connect = 0; snprintf(param->domain, RELAY_SERVER_DOMAIN_MAX_LEN+1, "%s", relay_address); s_cookie_ctx.conn_req = tcp_connection_new(relay_address, relay_port, TRUE, WEB_SOCKET_CRT, cookie_req_success_handle, cookie_req_recv_data_handle, cookie_req_failed_handle, param); if (NULL == s_cookie_ctx.conn_req) { RELAY_ERROR("creating connection to %s:%d failed.\n", relay_address, relay_port); free(param); return ERROR_2; } if (0 != strlen(s_cookie_ctx.relay_ip)) { snprintf(s_cookie_ctx.conn_req->domain_ip, DOMAIN_IP_STRLEN+1, "%s", s_cookie_ctx.relay_ip); HTTP_DEBUG("domain ip :%s", s_cookie_ctx.conn_req->domain_ip); } ret = tcp_connection_start(s_cookie_ctx.conn_req); return ret; } static int cookie_req_recv_data_handle(TCP_CONNECTION_T * connection) { RETURN_VALUE_IF((!connection || !connection->param), ERROR); HttpHeader_S *header = &s_cookie_ctx.header; int ret_code = -1; int ret = 0; int is_ssl_error = 0; /* 1.读取数据并缓存 */ do { char *buf = header->buf + header->recv_len; int read_maxsize = 0; if (header->recv_len < HTTP_HEADER_MAXSIZE) { read_maxsize = HTTP_HEADER_MAXSIZE - header->recv_len; } ret = tcp_read_data(connection, buf, read_maxsize); if (ret > 0) { header->recv_len += ret; } } while (ret > 0 && header->recv_len < HTTP_HEADER_MAXSIZE); /* 2.读数据错误处理 */ if (ret <= 0 && ret != TPSSL_ERROR_WANT_READ) { is_ssl_error = 1; } /* 3.解析是否接收到完整http报文头 */ int is_header_complete = 0; while (header->indx < header->recv_len && is_header_complete == 0) { /* 判断是否接受完成header,以"\r\n\r\n"字符串为结束符 */ is_header_complete = http_header_is_complete(header, header->buf[header->indx]); header->indx++; } if (is_header_complete == 0) { RELAY_INFO_LOG("cookie header is not complete, read_len=%d", header->recv_len); int is_buf_full = (header->recv_len < HTTP_HEADER_MAXSIZE) ? 0 : 1; if (is_ssl_error || is_buf_full) { RELAY_ERROR_LOG("cookie read ret=%d! recv_len=%d", ret, header->recv_len); goto ssl_exit; } return 0; } /* cookie����Ļ�Ӧ HTTP/1.1 200 OK Cache-control: no-cache="set-cookie" Pragma: no-cache Server: relayd Set-Cookie: AWSELB=A157B75B1638823EAD177532DF42AA3A50A76A02CF100C5A4E8CA1F22FDB14169154761B62532992AFB962EF9F0A0F1E56DF57EB64605CD77C0CF01073E30C2E778170E9CA;PATH=/ Content-Length: 0 Connection: Close */ char buf_cookie[LEN_MAX_BUF + 1] = {0}; ret = cookie_get_from_http_headers(header->buf, buf_cookie, LEN_MAX_BUF); if (ret < 0) { RELAY_ERROR_LOG("failed get cookie from http headers(erro: %d)", ret); goto ssl_exit; } else if (1 == ret) { RELAY_ERROR_LOG("no cookie in resp of cookies req."); goto ssl_exit; } RELAY_INFO_LOG("cookie update:%s", buf_cookie); strncpy(s_cookie_ctx.cookie, buf_cookie, sizeof(s_cookie_ctx.cookie) - 1); s_cookie_ctx.cookie_timestamp = get_sys_uptime(); ret_code = 0; ssl_exit: http_header_reset(&s_cookie_ctx.header); tcp_connection_stop(connection); if (ret_code != 0) { ++s_cookie_ctx.cookie_failed_times; cookie_check_retry_times(); } return ret_code; } static int cookie_req_success_handle(TCP_CONNECTION_T * connection) { char req[LEN_MAX_PACKET + 1]; int reqlen; int ret = 0; /* ����cookies�ı��� GET /status HTTP/1.1 User-Agent: knock-knock Host: relay-ipc.tplinkcloud.com.cn Content-Length: 0 Connection: close */ reqlen = snprintf(req, LEN_MAX_PACKET+1, "GET /status HTTP/1.1\r\n" "User-Agent: knock-knock\r\n" "Host: %s\r\n" "Content-Length: 0\r\n" "Connection: close\r\n\r\n", s_cookie_ctx.relay_address); if( reqlen <= 0 || reqlen >= sizeof(req) ) { RELAY_ERROR_LOG("buffer not enough for cookie request\n" "buffer size is %d, but %d is needed\n", LEN_MAX_PACKET,reqlen); goto failed; } /* reset接收缓冲区 */ http_header_reset(&s_cookie_ctx.header); ret = tcp_send_data(connection, req, reqlen); if (ret <= 0) { RELAY_ERROR_LOG("send cookie req failed\n"); goto failed; } RELAY_DEBUG("elb cookie requesting, req:\n[%s]\n", req); return 0; failed: RELAY_ERROR("cookie request connection failed. stop it\n"); if (connection != NULL) { if(connection->param) { free(connection->param); connection->param = NULL; } tcp_connection_free(connection); s_cookie_ctx.conn_req = NULL; } ++s_cookie_ctx.cookie_failed_times; cookie_check_retry_times(); return -1; } 分析一下以上函数的过程
10-06
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值