# Libre oj #161. 乘法逆元 2 \ P5431 【模板】乘法逆元2

libre oj

//#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<map>
#include<set>
#include<vector>
#include<cstring>
using namespace std;
typedef long long ll;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 5e6+7;
const int mod=1e9+7;
/*
struct EDGE{int to,nxt,val;}ee[M*2];
*/
template <typename T> void read(T &x) {
x = 0; int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
ll a[M];
ll sm[M];
ll qpow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1)ans=ans*a%mod;
a=a*a%mod;
b/=2;
}
return ans;
}
int main()
{
int n;
ll am;sm[0]=1;
am=qpow(sm[n],mod-2);
ll lm=1;
for(int i=n;i>=1;i--)
{
ll tp=sm[i-1]*am%mod;//i - n乘积的逆元
tp=tp*lm%mod;//i的逆元
lm=lm*a[i]%mod;
a[i]=tp;
}
ll tp=1,ans=0,x=998244353;
for(int i=n;i>=1;i--)
{
ans=(ans+tp*a[i]%mod)%mod;
tp=tp*x%mod;
}
printf("%lld\n",ans);
return 0;
}

//#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<map>
#include<set>
#include<vector>
#include<cstring>
using namespace std;
typedef long long ll;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 5e6+7;
int mod;
/*
struct EDGE{int to,nxt,val;}ee[M*2];
*/
template <typename T> void read(T &x) {
x = 0; int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
ll a[M];
ll sm[M];
ll qpow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1)ans=ans*a%mod;
a=a*a%mod;
b/=2;
}
return ans;
}
int main()
{
int n;
ll x;
ll am;sm[0]=1;
am=qpow(sm[n],mod-2);
ll lm=1;
for(int i=n;i>=1;i--)
{
ll tp=sm[i-1]*am%mod;//i - n乘积的逆元
tp=tp*lm%mod;//i的逆元
lm=lm*a[i]%mod;
a[i]=tp;
}
ll tp=x,ans=0;
for(int i=1;i<=n;i++)
{
ans=(ans+tp*a[i]%mod)%mod;
tp=tp*x%mod;
}
printf("%lld\n",ans);
return 0;
}

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客