2017年ACM-ICPC青岛站(现场赛)E题 伯利亚定理

来源:互联网 发布:轻松的工作 知乎 编辑:程序博客网 时间:2024/04/28 23:20
#include<bits/stdc++.h>using namespace std;string s,ans[2005];int cnt,sum[35];map<string,int> flag;void dfs(string s){    if(flag[s])        return;    ans[cnt++]=s;    flag[s]=1;    string tmp;    tmp=s;    swap(tmp[0],tmp[15]);    swap(tmp[3],tmp[12]);    swap(tmp[6],tmp[9]);    swap(tmp[18],tmp[21]);    swap(tmp[24],tmp[26]);    dfs(tmp);    tmp=s;    swap(tmp[6],tmp[17]);    swap(tmp[7],tmp[16]);    swap(tmp[8],tmp[15]);    swap(tmp[26],tmp[29]);    swap(tmp[18],tmp[20]);    dfs(tmp);    tmp=s;    swap(tmp[2],tmp[17]);    swap(tmp[5],tmp[14]);    swap(tmp[8],tmp[11]);    swap(tmp[23],tmp[20]);    swap(tmp[27],tmp[29]);    dfs(tmp);    tmp=s;    swap(tmp[0],tmp[11]);    swap(tmp[1],tmp[10]);    swap(tmp[2],tmp[9]);    swap(tmp[24],tmp[27]);    swap(tmp[21],tmp[23]);    dfs(tmp);    tmp=s;    swap(tmp[0],tmp[15]);    swap(tmp[1],tmp[16]);    swap(tmp[2],tmp[17]);    swap(tmp[3],tmp[12]);    swap(tmp[4],tmp[13]);    swap(tmp[5],tmp[14]);    swap(tmp[6],tmp[9]);    swap(tmp[7],tmp[10]);    swap(tmp[8],tmp[11]);    swap(tmp[21],tmp[18]);    swap(tmp[22],tmp[19]);    swap(tmp[23],tmp[20]);    swap(tmp[24],tmp[26]);    swap(tmp[27],tmp[29]);    dfs(tmp);    tmp=s;    swap(tmp[0],tmp[11]);    swap(tmp[3],tmp[14]);    swap(tmp[6],tmp[17]);    swap(tmp[1],tmp[10]);    swap(tmp[4],tmp[13]);    swap(tmp[7],tmp[16]);    swap(tmp[2],tmp[9]);    swap(tmp[5],tmp[12]);    swap(tmp[8],tmp[15]);    swap(tmp[21],tmp[23]);    swap(tmp[18],tmp[20]);    swap(tmp[24],tmp[27]);    swap(tmp[25],tmp[28]);    swap(tmp[26],tmp[29]);    dfs(tmp);    tmp[4]=s[4];    tmp[13]=s[13];    tmp[0]=s[2];    tmp[1]=s[5];    tmp[2]=s[8];    tmp[5]=s[7];    tmp[8]=s[6];    tmp[7]=s[3];    tmp[6]=s[0];    tmp[3]=s[1];    tmp[9]=s[11];    tmp[10]=s[14];    tmp[11]=s[17];    tmp[14]=s[16];    tmp[17]=s[15];    tmp[16]=s[12];    tmp[15]=s[9];    tmp[12]=s[10];    tmp[21]=s[27];    tmp[22]=s[28];    tmp[23]=s[29];    tmp[27]=s[20];    tmp[28]=s[19];    tmp[29]=s[18];    tmp[20]=s[26];    tmp[19]=s[25];    tmp[18]=s[24];    tmp[26]=s[21];    tmp[25]=s[22];    tmp[24]=s[23];    dfs(tmp);    tmp[4]=s[4];    tmp[13]=s[13];    tmp[0]=s[6];    tmp[1]=s[3];    tmp[2]=s[0];    tmp[5]=s[1];    tmp[8]=s[2];    tmp[7]=s[5];    tmp[6]=s[8];    tmp[3]=s[7];    tmp[9]=s[15];    tmp[10]=s[12];    tmp[11]=s[9];    tmp[14]=s[10];    tmp[17]=s[11];    tmp[16]=s[14];    tmp[15]=s[17];    tmp[12]=s[16];    tmp[21]=s[26];    tmp[22]=s[25];    tmp[23]=s[24];    tmp[27]=s[21];    tmp[28]=s[22];    tmp[29]=s[23];    tmp[20]=s[27];    tmp[19]=s[28];    tmp[18]=s[29];    tmp[26]=s[20];    tmp[25]=s[19];    tmp[24]=s[18];    dfs(tmp);}void solve(string s){    bool vis[35]={false},flag=false;    int cnt=0;    for(int i=0;i<s.length();i++)    {        if(vis[i])            continue;        else            vis[i]=true;        int j=i;        while(s[j]!=i+65)        {            j=s[j]-65;            vis[j]=true;        }        cnt++;    }   sum[cnt]++;}void init(){    for(int i=0;i<30;i++)        s+=i+65;    dfs(s);    for(int i=0;i<cnt;i++)        solve(ans[i]);}typedef long long ll;ll n,p;ll quick_mod(ll n,ll m,ll mod){    ll res=1;    while(m)    {        if(m&1)            res=res*n%mod;        n=n*n%mod;        m>>=1;    }    return res;}void cal(ll n,ll p){    ll res=0;    for(int i=0;i<=30;i++)        if(sum[i])            res=(res+sum[i]%(p*cnt)*quick_mod(n,i,p*cnt)%(p*cnt))%(p*cnt);    printf("%lld\n",res/cnt);}int main(){    init();    int t;    scanf("%d",&t);    while(t--&&scanf("%lld%lld",&n,&p)!=EOF)        cal(n,p);    return 0;}/*0 1 23 4 56 7 89 10 1112 13 1415 16 1721 22 2318 19 2024    2725    2826    29*/

原创粉丝点击