首页 > 其他分享 >How Does the Rook Move?

How Does the Rook Move?

时间:2024-05-15 14:31:40浏览次数:33  
标签:pre Rook int long How Does ans include mod

题目链接
https://codeforces.com/contest/1957/problem/C
思路其实不难,最重要的就是一个问题,从n个数中两两配对,有多少种配法
首先进行全排列,即N!除去左右相等的即2的n/2次方,再除去彼此之间没顺序(N/2)!
代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<unordered_map>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<cmath>
#include<set>
using namespace std;
#define int long long
typedef long long ll;
const int N=1e6+10;
const int mod=1e9+7;
int n;
int a[N];
int b[N];
int pre[N];
int fpre[N];
int qsm(int a,int b)
{
    int ans=1;
    int c=a;
    while(b!=0)
    {
        if(b%2==1)
        {
            ans=ans*c;
            ans%=mod;
        }
        c=c*c;
        c%=mod;
        b>>=1;
    }
    return ans%mod;
}
void init()

{   pre[0]=1;
    pre[1]=1;
    fpre[0]=1;
    for(int i=1;i<=310000;i++)
    {
        pre[i]=pre[i-1]*i;
        pre[i]%=mod;
        fpre[i]=qsm(pre[i],mod-2);
         fpre[i]%=mod;
    }
    

}
int C(int n,int m)
{
    return pre[n]*fpre[m]%mod*fpre[n-m]%mod;
}
void solve()
{
    int n,k;
    
    cin>>n>>k;
    set<int>s;
    for(int i=1;i<=k;i++)
    {
        int x,y;
        cin>>x>>y;
        s.insert(x);
        s.insert(y);
    }
     n=n-s.size();
    if(n==1){
			cout<<1<<endl;
			return;
		}
		if(n==2){
			cout<<3<<endl;
			return;
		}
   
    int ans=0;
    if(n%2==0)
    {
        for(int i=0;i<=n;i+=2)
        {
          ans+=C(n,i)*pre[n-i]%mod*fpre[(n-i)/2]%mod;
            ans%=mod;
        }
        cout<<(ans)%mod<<endl;
    }
    else{
        for(int i=1;i<=n;i+=2)
        {
        ans+=C(n,i)*pre[n-i]%mod*fpre[(n-i)/2]%mod;
            ans%=mod;

        }
            cout<<(ans)%mod<<endl;
    }

}
signed main()
{
    int t;
    cin>>t;
    init();
    while(t--)
    {
   solve();
    }
    return 0;
}

标签:pre,Rook,int,long,How,Does,ans,include,mod
From: https://www.cnblogs.com/yuhaomice/p/18193800

相关文章

  • How to subscribe to GPT4?
    如何在没有海外银行卡、海外手机号的情况下注册GPT4?概述:注册美区AppleID,使用礼品卡充值。免责声明:这个过程在理论上是被Apple禁止的,仅能够保证的是截止2024.5这套方法还在生效。由于账号被封导致的问题概不负责。材料:一台Apple设备。一个没有注册过AppleID(或者只注......
  • How to Solve VAS 6154 Interface License Expired: Unleash Your Automotive Diagnos
    WelcometoourcomprehensiveguideonresolvingtheVAS6154InterfaceLicenseExpiredissue.Ifyou'reanautomotiveenthusiastorprofessional,chancesareyou'veencounteredthisfrustratingproblemduringyourdiagnosticendeavors.Butfret......
  • ShowDoc:打造IT团队高效协作的文档与API管理神器
    介绍ShowDoc:一款适用于IT团队的知识文档与API管理工具ShowDoc是一款专为IT团队设计的知识文档和API管理工具,它允许用户通过Markdown语法轻松地创建和编辑美观的API文档、数据字典文档、技术文档,甚至在线Excel文档。ShowDoc支持多平台客户端,包括Windows、Mac、iOS和Android,为用......
  • ctfshow-菜狗杯-web
    菜狗杯一言既出打开题目就是一个朴实无华的php代码我们分析一下:需要传入一个num的参数,使num==114514,后面经过intval转化后要num==1919810,否则直接结束进程这下就有点难办了,但其实我们只要其实闭合一下这个assert函数,不让这个结束的条件成立就行,payload如下num=114514);//......
  • How to redirect to a specific web page after sign out from Entra ID
    HowtoredirecttoaspecificwebpageaftersignoutfromEntraIDWithsomemorediggingIfoundthebelowchangesresultedinasuccessfulredirecttoapageofmychoosing.Ifoundthatifthe SignedOutCallbackPath issettoanythingotherthan /signo......
  • pinus老项目启动遇'Property connector does not exist on type UserRpc'报错
    跟示例项目对比过,配置代码并无出入,尝试在示例中新增远程调用connectorRemote可用,证明代码配置正确尝试在示例项目中使用工作项目的配置文件包括引用的模块文件目录列表如下 packagespluginspackage.jsonpackage-lock.jsontsconfig.jsonyarn.lock 示例安装模块后,运......
  • how BabyFile app transfer files with Windows PC though data cable
    1.WindowsPCdownload"iTunes"App.Asfollows:2.WindowsPCconnecttheiPhone/iPadwithadatacable,andthen open"iTunes"app, findandclicktheconnecteddevice.asmarkedby①inthepicture. 2.Findand clickthe"Fi......
  • Does One Have to be a Genius to Do Maths? (必须是天才才能做数学吗?)
    Thisisaquestionthatmanypeopletalkabout.AndIthinkitisimportantbecauseincorrectresponsescanmisleadindividualsandsignificantlyinfluencehis/herattitudetostudyingmathematics.Letmequotethefollowingtwotoexpressmyopinion.Th......
  • How to Learn Item Representation for Cold-Start Multimedia Recommendation
    目录概符号说明MotivationMulti-TaskPairwiseRanking(MTPR)代码DuX.,WangX.,HeX.,LiZ.,TangJ.andChuaT.Howtolearnitemrepresentationforcold-startmultimediarecommendation?MM,2020.概作者以往的多媒体推荐对于colditems在训练阶段没有足够的......
  • How-to-install-NetSuite-SuiteCloud-plug-in-to-WebStorm-Updated2021-11-CarlZeng
    Install/upgradeNetSuiteSuiteCloudIDEPlug-inSuiteCloudIDEplug-inSuiteCloudIDEplug-inforWebStormisanintegrateddevelopmentenvironmentforNetSuiteplatformdevelopment.ItprovidesauserinterfaceforSuiteCloudDevelopmentFramework(SDF),......