当前位置:网站首页>(nowcoder22529c) diner (inclusion exclusion principle + permutation and combination)
(nowcoder22529c) diner (inclusion exclusion principle + permutation and combination)
2022-07-29 03:32:00 【AC__ dream】
Topic link : Sign in — major IT Written interview preparation platform _ Cattle from
subject :

The sample input :
1000000Sample output :
270258012 analysis : set up Ai It means the first one i Yes cp The property of adjacency , Then we are asking for N((1-A1)(1-A2)……(1-An)), Show first i Yes cp Adjacent number , because n Yes cp There is no distinction , So we can choose any i Yes cp Indicates that it is adjacent , The number of selected schemes is C(n,i), Each pair cp Next to each other, we regard them as a point , So the total is n*2-i A little bit , Because it is arranged in a ring , Then we need to take any point as the head node , The rest of the points can be fully arranged , Then the number of permutation schemes is (n-2*i-1)!, Each pair cp The internal arrangement is arbitrary , So there are two situations , All in all i Yes cp, So contribution is 2^i, Then apply the formula of the principle of inclusion and exclusion to get :
This question has strict requirements for time and space , So we must optimize as much as possible , I attach the non optimized code and the optimized code for you to compare
Unoptimized code ( Overtime )
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const int N=6e7+10,mod=1e9+7;
ll fac[N],inv[N];
ll qpow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1) ans=ans*a%mod;
b>>=1;
a=a*a%mod;
}
return ans;
}
ll C(ll a,ll b)
{
return fac[a]*inv[b]%mod*inv[a-b]%mod;
}
int main()
{
ll n;
cin>>n;
if(n==1)
{
puts("0");
return 0;
}
fac[0]=inv[0]=1;
for(int i=1;i<=2*n;i++)
fac[i]=fac[i-1]*i%mod;
inv[2*n]=qpow(fac[2*n],mod-2);
for(int i=2*n-1;i>=1;i--)
inv[i]=inv[i+1]*(i+1)%mod;
ll ans=0;
int sign=1;
for(int i=0;i<=n;i++)
{
ans=(ans+sign*C(n,i)*fac[2*n-i-1]%mod*qpow(2,i))%mod;
ans=(ans+mod)%mod;
sign=-sign;
}
printf("%lld",ans);
return 0;
}Optimized code :
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const int N=3e7+10,mod=1e9+7;
int inv[N];
ll qpow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1) ans=ans*a%mod;
b>>=1;
a=a*a%mod;
}
return ans;
}
int main()
{
ll n;
cin>>n;
if(n==1)
{
puts("0");
return 0;
}
inv[0]=1;
for(int i=1;i<=n;i++)
inv[i]=1ll*inv[i-1]*i%mod;
inv[n]=qpow(inv[n],mod-2);
for(int i=n-1;i>=1;i--)
inv[i]=1ll*inv[i+1]*(i+1)%mod;
ll ans=0;
ll sign;
ll fac1=1,fac2=1,p2=1;
ll inv2=qpow(2,mod-2);//inv2 Record 2 About mod Inverse element
//fac1 Record fac[n],fact2 Record fac[2*n-i-1],p2 Record qpow(2,i)
for(int i=1;i<=n;i++) fac1=fac1*i%mod,p2=p2*2%mod;
for(int i=1;i<n;i++) fac2=fac2*i%mod;
if(n&1) sign=-1;
else sign=1;
for(int i=n;i>=0;i--)
{
// ans=(ans+sign*C(n,i)*fac[2*n-i-1]%mod*qpow(2,i))%mod;
ans=(ans+sign*fac1*inv[i]%mod*inv[n-i]%mod*fac2%mod*p2)%mod;
fac2=fac2*(2*n-i)%mod;
p2=p2*inv2%mod;
sign=-sign;
}
ans=(ans%mod+mod)%mod;
printf("%lld",ans);
return 0;
}边栏推荐
- Singleton and invariant modes of concurrent mode
- Exness: dove resolution helped gold rebound, and the focus turned to U.S. GDP
- 再学EXKMP(EXKMP模板)
- 逐步分析类的拆分之案例——五彩斑斓的小球碰撞
- 军品研制过程-转阶段
- Score addition and subtraction of force deduction and brushing questions (one question per day 7/27)
- Code speed optimization
- Three military product baselines (functional baseline, distribution baseline, product baseline) and the documents contained in the baseline
- Whole process record of yolov3 target detection
- Idea configuration web container and war packaging
猜你喜欢

【C】 Array

通过递归实现多级联动

Asynchronous callback future mode of concurrent mode

Exness: dove resolution helped gold rebound, and the focus turned to U.S. GDP

Singleton mode (hungry and lazy)
![Leetcode 1331 array sequence number conversion [map] the leetcode path of heroding](/img/be/d429d0c437dc5ed7cb4448e223a83a.png)
Leetcode 1331 array sequence number conversion [map] the leetcode path of heroding

04 | background login: login method based on account and password (Part 1)

容斥原理

Pp-yoloe details

Configure vscade to realize ROS writing
随机推荐
(2022杭电多校三)1011-Link is as bear(思维+线性基)
ROS-Errror:Did you forget to specify generate_ messages(DEPENDENCIES ...)?
Ten thousand words detailed Google play online application standard package format AAB
ShardingSphere之水平分表实战(三)
Score addition and subtraction of force deduction and brushing questions (one question per day 7/27)
Microcomputer principle operation
1.5 nn. Module neural network (III)
Redis之sentinel哨兵集群怎么部署
Introduction and advanced level of MySQL (11)
Introduction and comparison of unicast, multicast (target broadcast, multicast), broadcast, flooding, flooding
How to realize multi line annotation in MATLAB
GJB common confused concepts
Bingbing learning notes: operator overloading -- implementation of date class
最新二开版漫画小说听书三合一完整源码/整合免签接口/搭建教程/带采集接口
Suffix automata (SAM) board from Jly
JS regular expression finds the number of times a character (string) appears (one line of code)
Kubernetes-1.24.x feature
机器学习【Numpy】
three. JS Part 54 how to pass structure array to shader
暴力递归到动态规划 01 (机器人移动)