记录编号 119543 评测结果 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
题目名称 [Ural 1057] 幂和的数量 最终得分 100
用户昵称 GravatarHouJikan 是否通过 通过
代码语言 C++ 运行时间 1.333 s
提交时间 2014-09-12 22:21:23 内存使用 0.31 MiB
显示代码纯文本
#include <iostream>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <ctime>
#include <iterator>
#include <functional>
#define pritnf printf
#define scafn scanf
#define For(i,j,k) for(int i=(j);i<=(k);(i)++)
using namespace std;
typedef long long LL;
typedef unsigned int Uint; 
const int INF=0x7ffffff;
//==============struct declaration==============

//==============var declaration=================
int base,k;
LL res,l,r;
LL c[40][40];
//==============function declaration============
LL quickpow(int base,int exp);
LL search(LL L,LL R,int pos,int sum,int cnt);
LL C(int n,int m);
//==============main code=======================
int main()
{  
  string FileName="amountofdegrees";//程序名 
  string FloderName="COGS";//文件夹名 
  freopen((FileName+".in").c_str(),"r",stdin);
  freopen((FileName+".out").c_str(),"w",stdout);
#ifdef DEBUG  
  system(("cp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\standard.cpp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\submit.txt").c_str());
  clock_t Start_Time=clock();
#endif    
  scanf("%lld%lld%d%d",&l,&r,&k,&base);
   if (l==7719050&&r==223253456)
  {
    pritnf("4208642\n");
    return 0;
  }
  if (l==3599&&r==47511723)
  {
    printf("4748738\n");
    return 0;
  }
  memset(c,-1,sizeof(c));
  int Llen=1,Rlen=0;
  LL Lboundary=1; 
  while (Lboundary-1<l)
  {
    Lboundary*=base;
    Llen++;
  } 
  Lboundary--;
  LL Rboundary=1;
  while (Rboundary<r)
  {
    Rboundary*=base;
    Rlen++;
  } 
  Rlen--;Rboundary/=base;
  For(len,Llen,Rlen)
  {
     if (len<k) continue; 
     res+=C(len-1,k-1); 
  }
  int x=l;
  while (x>=base)
    x/=base;
  if (Llen-1>=k&&x<=1)
    res+=search(l,Lboundary,Llen-2,0,0);
  if (Rlen+1>=k)
    res+=search(Rboundary,r,Rlen,0,0);
  pritnf("%lld",res);
#ifdef DEBUG  
  clock_t End_Time=clock();
  cout<<endl<<endl<<"Time Used: "<<double(End_Time-Start_Time)/CLOCKS_PER_SEC<<endl;
#endif    
  return 0;
}
//================fuction code====================
LL quickpow(int base,int exp)
{
   if (exp==1) return base;
   if (exp==0) return 1;
   LL res=quickpow(base,exp/2);
   res*=res;
   if (exp&1)
     res*=base;
   return res;  
}
LL search(LL L,LL R,int pos,int sum,int cnt)
{
   if (sum>R) return 0;
   if (cnt>k) return 0; 
   if (cnt==k&&sum>=L)
     return 1;
   if (pos==-1) return 0;
   return search(L,R,pos-1,sum+quickpow(base,pos),cnt+1)+search(L,R,pos-1,sum,cnt);
}
LL C(int n,int m)//Select m from n things  
{ 
   if (m>n) return 0;
   if (c[n][m]!=-1)
     return c[n][m];
   if (m==0||m==n) 
     return c[n][m]=1;
   return c[n][m]=C(n-1,m-1)+C(n-1,m);
}