HDU-4734 F(x) 数位DP

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734

  注意到F(x)的值比较小,所以可以先预处理所有F(x)的组合个数。f[i][j]表示 i 位数时F(x)为 j 的个数,方程容易转移:f[i][1<<i+j]=sigma( f[i][j] )。然后求一下f[i][j]的前缀和,就可以直接统计了。。

 //STATUS:C++_AC_31MS_684KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int f[][N];
int T,n,m; int getfa(int a)
{
int i,ret=;
for(i=;a;i++,a/=)
ret+=(a%)*(<<i);
return ret;
} int solve()
{
int i,j,k,acc=,fa,len,t,ret=;
int num[];
for(len=,t=m;t;t/=)num[++len]=t%;
fa=getfa(n);
for(i=len;i>=;i--){
for(j=;j<num[i];j++){
if(fa-acc-j*(<<(i-))>=)ret+=f[i-][fa-acc-j*(<<(i-))];
}
acc+=j*(<<(i-));
}
if(acc<=fa)ret++;
return ret;
} int main(){
// freopen("in.txt","r",stdin);
int i,j,k,t,ca=;
f[][]=;
for(i=;i<=;i++){
for(j=;j<;j++){
t=j*(<<(i-));
for(k=;t+k<N;k++)
f[i][t+k]+=f[i-][k];
}
}
for(i=;i<=;i++)
for(j=;j<N;j++)f[i][j]+=f[i][j-];
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
printf("Case #%d: %d\n",ca++,solve());
}
return ;
}
上一篇:使用MJExtension中要注意的地方


下一篇:ActionController::InvalidAuthenticityToken 解决办法(第二种尤其有效)