zoukankan      html  css  js  c++  java
  • [TJOI2018]教科书般的亵渎

    传送门

    做这道题的时候超级有画面感……

    这道题其实不是很难……只要掌握了结论就不是什么问题,不过我因为推错了还是做了好长时间……

    题目其实就是要求你重复多次求

    $$sum_{i=1}^n i^{m+1}$$

    以前有大神写论文告诉我们,这个式子是一个以(n)为自变量的(k+1)(k)是指数)次多项式,那么我们就可以用拉格朗日插值求一下。

    这题的数据范围很小,所以其实可以不使用(O(n))的算法,直接普通的求也是可以过的。然后注意要删去不存在的血量给答案的贡献,因为每次释放亵渎以后,这个血量也会相对应变化,所以其实每次减去的是枚举到的血量减去已经被亵渎消耗过的血量(我就是在这wa的)

    之后就比较正常了,时间复杂度(O(Tn^3)).

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<iostream>
    #include<cmath>
    #include<set>
    #include<vector>
    #include<map>
    #include<queue>
    #define rep(i,a,n) for(int i = a;i <= n;i++)
    #define per(i,n,a) for(int i = n;i >= a;i--)
    #define enter putchar('
    ')
    #define fr friend inline
    #define y1 poj
    #define mp make_pair
    #define pr pair<int,int>
    #define fi first
    #define sc second
    #define pb push_back
    
    using namespace std;
    typedef long long ll;
    const int M = 100005;
    const int INF = 1000000009;
    const double eps = 1e-7;
    const ll mod = 1e9+7;
    
    ll read()
    {
        ll ans = 0,op = 1;char ch = getchar();
        while(ch < '0' || ch > '9') {if(ch == '-') op = -1;ch = getchar();}
        while(ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0',ch = getchar();
        return ans * op;
    }
    
    ll T,n,m,a[M],x[M],y[M];
    
    ll qpow(ll a,ll b)
    {
       ll p = 1;
       while(b)
       {
          if(b & 1) p *= a,p %= mod;
          a *= a,a %= mod;
          b >>= 1;
       }
       return p;
    }
    
    ll lagrange(ll k)
    {
       //rep(i,1,m+3) printf("#%lld %lld
    ",x[i],y[i]);
       ll p = 0;k %= mod;
       rep(i,1,m+3)
       {
          ll cur = 1;
          rep(j,1,m+3)
          {
         if(i == j) continue;
         ll now = (x[i] - x[j] + mod) % mod;
         cur = cur * (k - x[j] + mod) % mod * qpow(now,mod-2) % mod;
          }
          p = p + (cur * y[i] % mod),p %= mod;
       }
       return p;
    }
    
    int main()
    {
       T = read();
       while(T--)
       {
          n = read(),m = read();
          rep(i,1,m) a[i] = read();
          sort(a+1,a+1+m);
          per(i,m,1) {if(a[i] == n) n--,m--; else break;}
          rep(i,1,m+3) x[i] = i,y[i] = qpow(i,m+1),y[i] += y[i-1],y[i] %= mod;
          ll ans = 0;
          rep(i,0,m)
          {
         ans += lagrange(n - a[i]),ans %= mod;
         //printf("#%lld
    ",ans);
         rep(j,i+1,m) ans -= qpow(a[j] - a[i],m+1),ans += mod,ans %= mod;
          }
          printf("%lld
    ",ans % mod);
       }	 
       return 0;
    }
    
    
  • 相关阅读:
    【Nginx 快速入门】反向代理、负载均衡、动静分离
    【Redis】缓存穿透、缓存击穿、缓存雪崩(面试必备)
    【Redis】特殊数据类型
    【Redis】特殊数据类型
    【Redis】特殊数据类型
    typescript 技巧学习
    angular9的学习(十九)
    angular11源码探索二十六[Router整体路由配置]
    angular11源码探索二十五[Router路由事件]
    angular11源码探索二十四[路由检测变更策略]
  • 原文地址:https://www.cnblogs.com/captain1/p/10107143.html
Copyright © 2011-2022 走看看