4 条题解

  • 0
    @ 2024-10-3 9:23:47
    #include <bits/stdc++.h>
    using namespace std;
    using ull = unsigned long long;
    ull qpow(ull x, ull y){
    	ull res = 1;
    	while (y){
    		if (y & 1) res *= x;
    		x *= x;
    		y >>= 1;
    	}
    	return res;
    }
    ull C(ull n, int m){
    	if (n < m) return 0;
    	static ull num[70];
    	for (int i = 0; i < m; i++) num[i] = n - i;
    	for (int i = 1; i <= m; i++){
    		int x = i;
    		for (int j = 0; j < m && x > 1; j++){
    			int g = __gcd(num[j], (ull)x);
    			if (g > 1){
    				num[j] /= g;
    				x /= g;
    			}
    		}
    		assert(x == 1);
    	}
    	ull res = 1;
    	for (int i = 0; i < m; i++) res *= num[i];
    	return res;
    }
    ull C2[70][70], f[70][2][70];
    int m;
    void init(ull n){
        memset(f, 0, sizeof(f));
        f[60][0][0] = 1;
        for (int i = 0; i <= 63; i++)
            for (int j = 0; j <= i; j++)
                C2[i][j] = C(i, j);
        for (int i = 59; i >= 0; i--)
            for (int p : {0, 1})
                for (int j = 0; j <= 63; j++)
                    for (int num = 0; num < 2; num++){
                        if (!p && num > ((n >> i) & 1)) continue;
                        if (i == 0 && num) continue;
                        int p2 = p || num < ((n >> i) & 1);
                        for (int k = j; k <= 63; k++){
                            ull cur = k == j ? 1 : (i * (k - j) < 64 ? (1ull * num) << (i * (k - j)) : 0);
                            if (cur) f[i][p2][k] += f[i + 1][p][j] * C2[k][j] * cur;
                        }
                    }
    }
    ull n, ans;
    int main(){
    	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    	cin >> m >> n;
    	if (m < 64){
            init(n);
            ans += f[0][1][m];
        }
        n--;
        init(n--);
    	for (int j = 0; j <= min(m, 63); j++)
    		ans += C(m, j) * f[0][1][j];
    	cout << ans;
    	return 0;
    }
    
    • 0
      @ 2024-10-3 9:22:48
      #include<cstdio>
      #include<iostream>
      #include<cstring>
      #include<cmath>
      #define N 66
      #define ull unsigned long long
      using namespace std;
      
      ull s2[N][N];
      
      void init(int n){
      	s2[0][0] = 1;
      	for(int i=1;i<=n;++i)
      	for(int j=1;j<=i;++j)
      		s2[i][j] = j*s2[i-1][j] + s2[i-1][j-1];
      }
      
      inline ull coef(ull n,int j){
      	ull res = 1;
      	for(int i=0;i<j;++i){
      		if((n-i)%j) res *= n-i;
      		else res *= (n-i)/j;
      	}
      	return res;
      }
      
      inline ull solve(ull n,int k){ // \sum_{i=0}^{n-1} i^k
      	ull res = 0;
      	for(int j=0;j<=k;++j)
      		res += s2[k][j]*coef(n,j+1);
      	return res;
      }
      
      inline void multiply(const ull *f,const ull *g,int n,ull *r){
      	static ull h[N];
      	for(int i=0;i<=n;++i) h[i] = 0;
      	for(int i=0;i<=n;++i)
      	for(int j=0;j<=i;++j)
      		h[i] += f[j]*g[i-j];
      	for(int i=0;i<=n;++i) r[i] = h[i];
      }
      
      inline void power(ull *f,int n,int k,ull *r){
      	static ull g[N];
      	g[0] = 1;
      	for(int i=1;i<=n;++i) g[i] = 0;
      	while(k){
      		if(k&1) multiply(g,f,n,g);
      		multiply(f,f,n,f);
      		k >>= 1;
      	}
      	for(int i=0;i<=n;++i) r[i] = g[i];
      }
      
      int k;
      ull n,ans;
      ull f[N];
      
      int main(){
      	init(64);
      	scanf("%d%llu",&k,&n);
      	if(k<64) ans = solve(n,k);
      	else{
      		f[0] = f[1] = 1;
      		power(f,63,k,f);
      		n >>= 1;
      		for(int j=0;j<=63;++j)
      			ans += (f[j]<<j)*solve(n,j);
      	}
      	printf("%llu",ans);
          return 0;   
      }
      
      • 0
        @ 2024-10-3 9:21:29
        #include<bits/stdc++.h>
        #define ull unsigned long long
        using namespace std;
        struct modint
        {
            ull x;
            int pw2;
            modint(){x=0,pw2=0;}
            modint(ull _x,int _pw2){x=_x,pw2=_pw2;}
            modint(ull val)
            {
                assert(val);
                pw2=__builtin_popcountll(val^val-1)-1;
                x=val>>pw2;
            }
        };
        ull decode(modint x)
        {
            assert(x.pw2>=0);
            return x.x<<x.pw2;
        }
        ull inv(ull x)
        {
            assert(x&1);
            ull ret=1;
            for(int i=63;i--;)ret=ret*x,x=x*x;
            return ret;
        }
        modint inv(modint x)
        {
            return modint{inv(x.x),-x.pw2};
        }
        modint operator*(modint x,modint y)
        {
            return modint{x.x*y.x,x.pw2+y.pw2};
        }
        ull qpow(ull x,ull y)
        {
            ull ret=1;
            while(y)
            {
                if(y&1)ret=ret*x;
                x=x*x;
                y>>=1;
            }
            return ret;
        }
        modint Inv[70],fac[70],invfac[70];
        ull pwm[70];
        ull n,m;
        int main()
        {
            scanf("%llu%llu",&m,&n);
            if(m==0)
            {
                printf("%llu\n",n);
                return 0;
            }
            for(int i=0;i<=65;i++)pwm[i]=qpow(i,m);
            fac[0]=invfac[0]=modint(1);
            for(int i=1;i<=65;i++)fac[i]=fac[i-1]*modint(i),invfac[i]=inv(fac[i]);
            modint binom=modint(n);
            ull ans=0;
            for(int t=1;t<=65&&t<=m&&t<n;t++)
            {
                binom=binom*modint(n-t);
                binom=binom*inv(modint(t+1));
                ull stirling=0;
                for(int k=0;k<=t;k++)
                {
                    ull x=decode(fac[t]*invfac[k]*invfac[t-k])*pwm[k];
                    if(t-k&1)stirling-=x;
                    else stirling+=x;
                }
                ans+=decode(binom)*stirling;
            }
            printf("%llu\n",ans);
            return 0;
        }
        
        • 0
          @ 2024-10-2 22:25:31
          #include<bits/stdc++.h>
          #define int unsigned long long
          using namespace std;
          namespace IO{
              char buff[1<<21],*p1=buff,*p2=buff;
              char getch(){
                  return p1==p2&&(p2=((p1=buff)+fread(buff,1,1<<21,stdin)),p1==p2)?EOF:*p1++;
              }
              template<typename T>
              void read(T &x){
                  char ch=getch();int fl=1;x=0;
                  while(ch>'9'||ch<'0'){if(ch=='-')fl=-1;ch=getch();}
                  while(ch<='9'&&ch>='0'){x=x*10+ch-48;ch=getch();}
                  x*=fl;
              }
              template<typename T,typename ...Args>
              void read(T &x,Args& ...args){
                  read(x);read(args...);
              }
              char obuf[1<<21],*p3=obuf;
              void putch(char ch){
                  if(p3-obuf<(1<<21))*p3++=ch;
                  else fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=ch;
              }
              char ch[100];
              template<typename T>
              void write(T x){
                  if(!x)return putch('0');
                  if(x<0)putch('-'),x*=-1;
                  int top=0;
                  while(x)ch[++top]=x%10+48,x/=10;
                  while(top)putch(ch[top]),top--;
              }
              template<typename T,typename ...Args>
              void write(T x,Args ...args){
                  write(x);write(args...);
              }
              void flush(){fwrite(obuf,p3-obuf,1,stdout);}
          }
          using namespace IO;
          const int MAXN=64,mod=(1ull<<63);
          inline int poww(int x,int y){
          	int sum=1;
          	while(y){
          		if(y&1)sum*=x;
          		x=x*x;
          		y>>=1;
          	}
          	return sum;
          }
          int P[MAXN];
          map<int,map<int,int>>f,C;
          inline int solve(int n,int m){
          	if(n==1)return (m==0?1:0);
          	if(f[m][n])return f[m][n];
          	if(n&1)return f[m][n]=solve(n-1,m)+poww(n-1,m);
          	int sum=(m<MAXN?P[m]*solve(n/2,m):0);
          	for(int i=0;i<=m&&i<MAXN;i++)sum+=P[i]*C[m][i]*solve(n/2,i);
          	return f[m][n]=sum;
          }
          signed main(){
          	P[0]=1,C[0][0]=1;
          	int n,m;
          	read(m,n);
          	for(int i=1;i<MAXN;i++)P[i]=P[i-1]+P[i-1];
          	for(int i=1;i<MAXN;i++)
          		for(int j=C[i][0]=1;j<=i;j++)
          			C[i][j]=C[i-1][j]+C[i-1][j-1];
          	for(int i=0;i<=m&&i<MAXN;i++){
          		C[m][i]=1;
          		int sum=0;
          		for(int j=1;j<=i;j++){
          			int x=m-j+1;
          			while(!(x&1))sum++,x>>=1;
          			C[m][i]*=x;
          		}
          		for(int j=1;j<=i;j++){
          			int x=j;
          			while(!(x&1))sum--,x>>=1;
          			C[m][i]*=poww(x,mod-1);
          		}
          		if(sum<MAXN)C[m][i]*=P[sum];
          		else C[m][i]=0;
          	}
          	write(solve(n,m));
          	flush();
          	return 0;
          }
          
          • 1

          信息

          ID
          3214
          时间
          200ms
          内存
          256MiB
          难度
          9
          标签
          (无)
          递交数
          16
          已通过
          2
          上传者