@KirinBill
2017-10-06T17:28:15.000000Z
字数 5239
阅读 1116
题解
套题
要相信自己,但不要过分相信自己的直觉。
目录
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
#include <cstring>
const int MAXN=500005;
int n;
int v[MAXN];
inline void read_chr(char &c){
do{c=getchar();}while(c!='L' && c!='R');
}
template<class T>
long long mge_sort(int l,int r,T a[]){
static T tmp[MAXN];
if(l==r) return 0;
int mid=l+r>>1;
long long ret=mge_sort(l,mid,a)+mge_sort(mid+1,r,a);
int cur=l-1,lp=l,rp=mid+1;
while(lp<=mid && rp<=r){
if(a[lp]>a[rp]){
tmp[++cur]=a[rp++];
ret+=mid-lp+1;
}
else tmp[++cur]=a[lp++];
}
while(lp<=mid) tmp[++cur]=a[lp++];
while(rp<=r) tmp[++cur]=a[rp++];
memcpy(a+l,tmp+l,(r-l+1)*sizeof(T));
return ret;
}
int main(){
setIO("problem");
read(n);
char cmd;
for(int i=1;i<=n;++i){
read_chr(cmd);
read(v[i]);
if(cmd=='L') v[i]=-v[i];
}
write(mge_sort(1,n,v));
return 0;
}
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
const int MAXN=100005,MAXM=5*MAXN,P=998244353;
int n,m;
int he[MAXN],dp[MAXN],inD[MAXN],outD[MAXN];
bool noIn[MAXN];
struct line{int to,nex;}ed[MAXM];
inline void addE(int u,int v){
static int cnt=0;
ed[++cnt]=(line){v,he[u]};
he[u]=cnt;
}
inline int pow(int x,int y){
int ret=1;
for(;y;y>>=1,x=(long long)x*x%P){
if(y&1) ret=(long long)ret*x%P;
}
return ret;
}
inline int inv(int x){return pow(x,P-2);}
void DAG_DP(int u){
if(u==n){
write(dp[u]);
exit(0);
}
int psb=(long long)dp[u]*inv(outD[u])%P;
for(int i=he[u],v;i;i=ed[i].nex){
v=ed[i].to;
dp[v]=(dp[v]+psb)%P;
if(--inD[v]==0) DAG_DP(v);
}
}
int main(){
setIO("deathsong");
read(n),read(m);
for(int i=1,u,v;i<=m;++i){
read(u),read(v);
addE(u,v);
++outD[u],++inD[v];
}
for(int i=1;i<=n;++i) noIn[i]=(inD[i]==0);
dp[1]=1;
for(int i=1;i<=n;++i){
if(noIn[i]) DAG_DP(i);
}
}
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
const int P=1e9+9;
long long n,k;
inline int pow(int x,long long y){
int ret=1;
for(;y;y>>=1,x=(long long)x*x%P){
if(y&1) ret=(long long)ret*x%P;
}
return ret;
}
namespace solve1
{
const int MAXN=1e6+5;
int fib[MAXN],sum[MAXN];
inline void pre_tab(){
fib[1]=1;
for(int i=2;i<MAXN;++i)
fib[i]=(fib[i-1]+fib[i-2])%P;
}
inline int solve(){
for(int i=1;i<=n;++i)
sum[i]=(sum[i-1]+pow(fib[i],k))%P;
return sum[n];
}
}
namespace solve2
{
const int sqrt_5=383008016,X=691504013,Y=308495997,MAXK=1e5+5;
int fac[MAXK],fac_inv[MAXK];
inline int inv(int x){return pow(x,P-2);}
inline void pre_tab(){
fac[0]=1;
for(int i=1;i<MAXK;++i)
fac[i]=(long long)fac[i-1]*i%P;
fac_inv[MAXK-1]=inv(fac[MAXK-1]);
for(int i=MAXK-2;i>=0;--i)
fac_inv[i]=(long long)fac_inv[i+1]*(i+1)%P;
}
inline int C(int n,int m){
return (long long)fac[n]*fac_inv[m]%P*fac_inv[n-m]%P;
}
inline int cal(int a){
int b=k-a;
int z=(long long)pow(X,b)*pow(Y,a)%P;
int ret;
if(z==1) ret=n%P;
else{
ret=(pow(z,n+1)-z+P)%P;
ret=(long long)ret*inv((z-1+P)%P)%P;
}
return (long long)ret*C(k,a)%P;
}
inline int solve(){
int ret=0;
for(int i=0;i<=k;++i){
if(i&1) ret=(ret-cal(i)+P)%P;
else ret=(ret+cal(i))%P;
}
return (long long)ret*pow(inv(sqrt_5),k)%P;
}
}
int main(){
setIO("ice");
int T;
read(T);
solve1::pre_tab();
solve2::pre_tab();
while(T--){
read(n),read(k);
if(n<solve1::MAXN) write(solve1::solve(),'\n');
else write(solve2::solve(),'\n');
}
return 0;
}