本文共 1392 字,大约阅读时间需要 4 分钟。
//#pragma comment(linker, "/STACK:102400000,102400000")#include #include #include #include #include #include #include #include #include #include #include #include using namespace std;typedef long long LL;#define mem(a, b) memset(a, b, sizeof(a))/*例一: 按位求和问题给定A,B(1<=A,B<=10^5),求[A,B]内的所有数的k进制表示下各数位之和dp[i][j] 表示 0 ~ k^i-1 的位数和 (在j进制下)pw[i][j] 表示 j^i 的值对pw处理不理解 建议先做 51nod 1009题目没有提交,与网上代码对拍,无差异。*/#define Elem LLconst int Dight = 65;int bits[Dight];Elem dp[Dight][11];Elem pw[Dight][11];void init(){ for(int i = 2; i <= 10; i++) { pw[0][i] = 1; for(int j = 1; j < Dight; j++) pw[j][i] = pw[j-1][i]*(LL)i; }}Elem dfs(int bit, int pos, int sta, bool limit){ if(pos==0) return (LL)sta; if(!limit && dp[pos][bit]!=-1) return dp[pos][bit] + pw[pos][bit]*(LL)sta; int up = limit ? bits[pos]:(bit-1); Elem res = 0; for(int i = 0; i <= up; i++) res += dfs(bit,pos-1,sta+i,limit&&(i==up)); if(!limit) dp[pos][bit] = res; return res;}Elem solve(Elem n, int bit){ int pos = 0; while(n){ bits[++pos] = n%bit; n /= bit; } return dfs(bit,pos,0,1);}int main(){ init(); int T,k; memset(dp,-1,sizeof(dp));// freopen("G:\\duipai\\in.txt","r", stdin);// freopen("G:\\duipai\\out1.txt","w", stdout); scanf("%d",&T); LL a,b; while(T--){ scanf("%I64d%I64d%d",&a,&b,&k); printf("%I64d\n",solve(b,k)-solve(a-1,k)); } return 0;}
转载地址:http://miaoz.baihongyu.com/