zoukankan      html  css  js  c++  java
  • 【牛客小白月赛6】 J 洋灰三角

    题目地址:https://www.nowcoder.com/acm/contest/136/J

    解法一:

      推数学公式求前n项和; 

      当k=1时,即为等差数列,Sn+pn(n1)/2
      当k≠1时,an+p/(k1) k(an1+p/(k-1)),等比数列,
    S= (kn+1+(p1)kn(np+1)k+(n1)p+1) / ((k-1)*(k-1))

      因为是除法取模,故除快速幂外还需逆元;

    Knowledge Point:

      除法取模逆元:https://www.cnblogs.com/ECJTUACM-873284962/p/6847672.html

      费马小定理:若p是质数,且a、p互质,那么a^(p-1) mod p = 1。

     1 #include<iostream>
     2 using namespace std;
     3  
     4 #define LL long long
     5 const LL MOD = 1e9+7;
     6 LL n,k,p;
     7  
     8 LL pow(LL a, LL x)
     9 {
    10     LL tmp=1;
    11     while(x) {
    12         if(x&1)
    13             tmp = tmp*a%MOD;
    14         a = a*a%MOD;
    15         x>>=1;
    16     }
    17     return tmp;
    18 }
    19  
    20 int main()
    21 {
    22     ios::sync_with_stdio(false);
    23     while(cin>>n>>k>>p)
    24     {
    25         if(k == 1)
    26             cout<<(n+p*n*(n-1)/2)%MOD<<endl;
    27         else {
    28             LL t = pow(k,n);
    29             LL ans = t*k+(p-1)*t-k*(n*p+1)+(n-1)*p+1;
    30             ans = (ans%MOD+MOD)%MOD;
    31             ans = ans*pow((k-1)*(k-1), MOD-2)%MOD;
    32             cout<<ans<<endl;
    33         }
    34     }
    35      
    36     return 0;
    37 }

    解法二:

      杜教板子:https://www.cnblogs.com/liubilan/p/9520292.html

      直接套杜教板子;

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <cmath>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <string>
      7 #include <map>
      8 #include <set>
      9 #include <cassert>
     10 #include<bits/stdc++.h>
     11 
     12 #define rep(i,a,n) for (ll  i=a;i<n;i++)
     13 #define per(i,a,n) for (ll  i=n-1;i>=a;i--)
     14 #define pb push_back
     15 #define mp make_pair
     16 #define all(x) (x).begin(),(x).end()
     17 #define fi first
     18 #define se second
     19 #define SZ(x) ((ll )(x).size())
     20 using namespace std;
     21 typedef long long ll;
     22 typedef vector<ll > VI;
     23 
     24 typedef pair<ll ,ll > PII;
     25 const ll mod=1000000007;
     26 ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
     27 // head
     28 
     29 ll  _,n;
     30 namespace linear_seq {
     31     const ll  N=10010;
     32     ll res[N],base[N],_c[N],_md[N];
     33 
     34     vector<ll > Md;
     35     void mul(ll *a,ll *b,ll  k) {
     36         rep(i,0,k+k) _c[i]=0;
     37         rep(i,0,k) if (a[i]) rep(j,0,k) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
     38         for (ll  i=k+k-1;i>=k;i--) if (_c[i])
     39             rep(j,0,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
     40         rep(i,0,k) a[i]=_c[i];
     41     }
     42     ll  solve(ll n,VI a,VI b) { // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
     43 //        prll f("%d
    ",SZ(b));
     44         ll ans=0,pnt=0;
     45         ll  k=SZ(a);
     46         assert(SZ(a)==SZ(b));
     47         rep(i,0,k) _md[k-1-i]=-a[i];_md[k]=1;
     48         Md.clear();
     49         rep(i,0,k) if (_md[i]!=0) Md.push_back(i);
     50         rep(i,0,k) res[i]=base[i]=0;
     51         res[0]=1;
     52         while ((1ll<<pnt)<=n) pnt++;
     53         for (ll  p=pnt;p>=0;p--) {
     54             mul(res,res,k);
     55             if ((n>>p)&1) {
     56                 for (ll  i=k-1;i>=0;i--) res[i+1]=res[i];res[0]=0;
     57                 rep(j,0,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
     58             }
     59         }
     60         rep(i,0,k) ans=(ans+res[i]*b[i])%mod;
     61         if (ans<0) ans+=mod;
     62         return ans;
     63     }
     64     VI BM(VI s) {
     65         VI C(1,1),B(1,1);
     66         ll  L=0,m=1,b=1;
     67         rep(n,0,SZ(s)) {
     68             ll d=0;
     69             rep(i,0,L+1) d=(d+(ll)C[i]*s[n-i])%mod;
     70             if (d==0) ++m;
     71             else if (2*L<=n) {
     72                 VI T=C;
     73                 ll c=mod-d*powmod(b,mod-2)%mod;
     74                 while (SZ(C)<SZ(B)+m) C.pb(0);
     75                 rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
     76                 L=n+1-L; B=T; b=d; m=1;
     77             } else {
     78                 ll c=mod-d*powmod(b,mod-2)%mod;
     79                 while (SZ(C)<SZ(B)+m) C.pb(0);
     80                 rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
     81                 ++m;
     82             }
     83         }
     84         return C;
     85     }
     86     ll  gao(VI a,ll n) {
     87         VI c=BM(a);
     88         c.erase(c.begin());
     89         rep(i,0,SZ(c)) c[i]=(mod-c[i])%mod;
     90         return solve(n,c,VI(a.begin(),a.begin()+SZ(c)));
     91     }
     92 };
     93 
     94 int  main() {
     95     ll   n, k, p;
     96     cin>>n>>k>>p;
     97     ll  sum[120];
     98     
     99     ///求出前10项
    100     sum[1]=1;
    101     for(ll  i=2;i<=10;i++){
    102         sum[i]=(sum[i-1]*k%mod+p)%mod;
    103     }
    104     for(ll  i=2;i<=10;i++){
    105         sum[i]=(sum[i-1]+sum[i])%mod;
    106     }
    107 
    108     vector<ll >v;
    109     for(ll  i=1;i<=10;i++){
    110         v.push_back(sum[i]);
    111 
    112     }
    113     printf("%lld
    ",linear_seq::gao(v,n-1));
    114 
    115 }
  • 相关阅读:
    2019 蓝桥杯国赛 B 组模拟赛 D. 程序设计:公约数
    2019长安大学ACM校赛网络同步赛 M LCM (数论)
    CCPC-Wannafly Winter Camp Day1 (Div2, onsite)
    CCPC-Wannafly Winter Camp Day1 流流流动 (树形dp)
    CCPC-Wannafly Winter Camp Day1 (Div2) 吃豆豆 (DP)
    CCPC-Wannafly Winter Camp Day1 (Div2, onsite) 夺宝奇兵
    CCPC-Wannafly Winter Camp Day1 爬爬爬山 (最短路)
    Comet OJ
    Comet OJ
    hdu-Danganronpa(AC自动机)
  • 原文地址:https://www.cnblogs.com/liubilan/p/9520066.html
Copyright © 2011-2022 走看看