50pts求差错
查看原帖
50pts求差错
443675
紊莫turtle楼主2022/12/21 17:34

思路就是折半搜,估计是快速幂溢出的问题?

//Author: Velvet on Luogu(uid=443675)
#include <bits/stdc++.h>
#define int long long
using namespace std;

inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void write(int x){if (x < 0) x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');}
inline void writeln(int x){write(x);putchar('\n');}
inline void writesp(int x){write(x);putchar(' ');}
inline int lowbit(int x) {return x&(-x);}
int n,m,k[10],p[10],ans;
map<int,int> M;
int qpow(int a,int b){
	int rt=1;
	for(;b;b>>=1){
		if(b&1) rt*=a;
		a*=a;
	}
	return rt;
}
void dfs(int x,int sum,int lim){
	if(x>lim){
		M[sum]++;return ;
	}
	for(int i=1;i<=m;i++){
		int qp=qpow(i,p[x]);dfs(x+1,sum+k[x]*qp,lim);
	}
}
void dfs2(int x,int sum,int lim){
	if(x>lim){
		if(M.count(-sum)) ans+=M[-sum];
		return ;
	}
	for(int i=1;i<=m;i++){
		int qp=qpow(i,p[x]);dfs2(x+1,sum+k[x]*qp,lim);
	}
}
signed main(){
	n=read(),m=read();
	for(int i=1;i<=n;i++)cin>>k[i]>>p[i];
	int x=n/2,y=n-x;
	dfs(1,0,x);dfs2(y,0,n);
	writeln(ans);
    return 0;
}

2022/12/21 17:34
加载中...