【单调队列】 HDOJ One hundred layer

对于每一层只考虑左边的t个,右边的t个是同样的问题。。。。dp数组使用滚动数组,s1代表现在的状态,s2代表上一层的状态。。。。那么转移就是dp[s2][j] = max(dp[s2][j], dp[s1][k] - sum[k - 1] + sum[j])。。。。sum是前缀和数组。k是j前t个到j。。我们发现sum[j]可以看成常数,然后dp[s1][k] - sum[k-1] 可以用单调队列优化。。。

#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 100005
#define maxm 100005
#define eps 1e-7
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL; 
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

int p[maxn];
int a[maxn];
int res[maxn];
int cnt, tcnt, n;

void init()
{
	for(int i = 2; i <= 100000; i++) if(p[i] == 0)
		for(int j = i + i; j <= 100000; j += i) p[j] = 1;
	cnt = 0;
	for(int i = 3; i <= 100000; i++) if(p[i] == 0) res[cnt++] = i;
}

void solve()
{
	if(n == 1) return;
	for(int i = 1; (LL)(i + 1) * (i + 1) <= n; i++) {
		if(n % (i + 1) == 0) {
			a[tcnt++] = i;
			n /= (i + 1);
			solve();
			break;
		}
	}
}

int cmp(int aa, int bb)
{
	return aa > bb;
}

void work()
{
	tcnt = 0;
	solve();
	if(n != 1) a[tcnt++] = n - 1;
	sort(a, a+tcnt, cmp);
	LL ans = 1;
	for(int i = 0; i < tcnt; i++) ans = ans * powmod(res[i], a[i]);
	printf("%lld\n", ans);
}

int main()
{
	init();
	while(scanf("%d", &n)!=EOF) {
		work();
	}
	
	return 0;
}


你可能感兴趣的:(单调队列)