POJChallengeRound2 Guideposts 【单位根反演】【快速幂】

题目分析:

这题的目标是求$$ \sum_{i \in [0,n),k \mid i} \binom{n}{i}G^i $$

这个形式很像单位根反演。

单位根反演一般用于求:$ \sum_{i \in [0,n),k \mid i} \binom{n}{i}f(x)^i $

推理过程略,实际上也就是交换求和符号的事情。

接着就变成裸的矩阵快速幂了

 

代码:

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 
  4 int m,k,p;long long n;
  5 int l,s,t,gg;
  6 
  7 struct mat{int arr[7][7];}G,bs,mmp;
  8 vector<int> fac; // factor of p
  9 
 10 void buildbase(int w){
 11     for(int i=1;i<=m;i++)
 12     for(int j=1;j<=m;j++) bs.arr[i][j] = 1ll*w*G.arr[i][j]%p;
 13     for(int i=1;i<=m;i++) bs.arr[i][i] ++,bs.arr[i][i] %= p;
 14 }
 15 
 16 mat operator*(mat alpha,mat beta){
 17     memset(mmp.arr,0,sizeof(mmp.arr));
 18     for(int k=1;k<=m;k++){
 19     for(int i=1;i<=m;i++){
 20         for(int j=1;j<=m;j++){
 21         mmp.arr[i][j] += 1ll*alpha.arr[i][k]*beta.arr[k][j]%p;
 22         mmp.arr[i][j] %= p;
 23         }
 24     }
 25     }
 26     return mmp;
 27 }
 28 
 29 mat res;
 30 mat fstpow(mat now,long long pw){
 31     memset(res.arr,0,sizeof(res.arr));
 32     for(int i=1;i<=m;i++) res.arr[i][i] = 1;
 33     long long bit = 1;
 34     while(bit <= pw){
 35     if(bit & pw){res = res*bs;}
 36     bs = bs*bs;bit<<=1;
 37     }
 38     return res;
 39 }
 40 
 41 void init(){
 42     memset(G.arr,0,sizeof(G.arr));
 43     fac.clear();
 44     l = s = t = gg = 0;
 45 }
 46 
 47 void read(){
 48     scanf("%d%d%d",&l,&s,&t);
 49     for(int i=1;i<=l;i++){
 50     int u,v; scanf("%d%d",&u,&v);
 51     G.arr[u][v]++;
 52     }
 53 }
 54 
 55 int fast_pow(int now,int pw){
 56     int ans = 1,dt = now,bit = 1;
 57     while(bit <= pw){
 58     if(bit & pw){ans = 1ll*ans*dt%p;}
 59     dt = 1ll*dt*dt%p; bit<<=1;
 60     }
 61     return ans;
 62 }
 63 
 64 void getgg(){
 65     int z = p-1;
 66     for(int i=2;i*i<=z;i++){
 67     if(z % i == 0){
 68         fac.push_back(i);
 69         while(z % i == 0) z /= i;
 70     }
 71     }
 72     if(z != 1) fac.push_back(z);
 73     for(int i=2;i<=p;i++){
 74     int flag = true;
 75     for(int j=0;j<fac.size();j++){
 76         int z = fast_pow(i,(p-1)/fac[j]);
 77         if(z == 1){flag = false; break;}
 78     }
 79     if(flag){gg = i;break;}
 80     }
 81     gg = fast_pow(gg,(p-1)/k);
 82 }
 83 
 84 void work(){
 85     int w = 1,ans = 0;
 86     for(int i=0;i<k;i++,w = 1ll*w*gg%p){
 87     buildbase(w);
 88     bs = fstpow(bs,n);
 89     ans += bs.arr[s][t]; ans%=p;
 90     }
 91     ans = 1ll*ans*fast_pow(k,p-2)%p;
 92     printf("%d\n",ans);
 93 }
 94 
 95 int main(){
 96     while(scanf("%d%lld%d%d",&m,&n,&k,&p) == 4){
 97     init();
 98     read();
 99     getgg();
100     work();
101     }
102     return 0;
103 }

 

 

转载于:https://www.cnblogs.com/Menhera/p/10394970.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278316.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

用Emesene替换Windows Live Messenger

Tired of Windows Live Messenger bloat and wishing that there was a simpler and cleaner replacement that would let you use your live.com and hotmail.com accounts? Look no further, now you can have all that messenger goodness with Emesene! 厌倦了Windows Liv…

python爬虫笔记(七):实战(三)股票数据定向爬虫

目标分析及描述 #CrawBaiduStocksA.py import requests from bs4 import BeautifulSoup import traceback import redef getHTMLText(url):try:r requests.get(url)r.raise_for_status()r.encoding r.apparent_encodingreturn r.textexcept:return ""def getStockL…

myeclipse和maven的clean和build

转&#xff1a; 详解myeclipse和maven的clean和build 2018年04月20日 11:33:34 群星坠 阅读数&#xff1a;3529 版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 https://blog.csdn.net/qq_35603331/article/details/80002723MyEclipse是一个被广为…

三星Galaxy S20:如何开启黑暗模式

Justin Duino贾斯汀杜伊诺(Justin Duino)Samsung was one of the first Android manufacturers to add Dark Mode to its handsets. If you recently purchased a Galaxy S20, S20, or S20 Ultra, enabling the UI feature and setting it up on a schedule is extremely easy.…

nginx和apache限制IP地址访问的设置方法

一、nginx禁止IP地址访问1、在nginx配置文件中加入这个&#xff1a;2、重启nginx服务二、apache禁止IP地址访问1、更改vhosts.conf文件&#xff1a;NameVirtualHost 192.168.1.191 <VirtualHost 192.168.1.191:99>#DocumentRoot "/usr/local/kk-mail/data/www"…

wordweb在线编辑_使用WordWeb享受按需词典和词库功能

wordweb在线编辑Run across an unusual word or need a synonym for a word quickly? Usually that means opening a browser and doing the appropriate search. Now you can have all that word power goodness at your fingertips with WordWeb. 遇到一个不寻常的词还是需…

转://RMAN跨平台可传输表空间和数据库

参考链接&#xff1a; http://blog.itpub.net/23135684/viewspace-776048/ http://blog.sina.com.cn/s/blog_69e7b8d7010164xh.html https://www.2cto.com/database/201311/260446.html 这篇文章翻译自Oracle 11gR2官方文档。详细讨论了使用RMAN工具的CONVERT DATAFILE&#xf…

2139=数据结构实验之图论五:从起始点到目标点的最短步数(BFS)

1 #include<stdio.h>2 #include<string.h>3 int map[1000][1000],visit[1000];4 int step,mark;5 int queue[1000];//用来储存已经遍历了的数据。6 void BFS(int k)7 {8 int i,o0,p0,temp,end0;//temp用来表示当前所在地。o表示下一步从哪个顶点向下出发。9 …

vnc数量限制_通过限制视觉效果在Vista上加速VNC

vnc数量限制This article was written by MetrotekGeek from Metrotek Solutions, a friend of the How-To Geek 本文由Metrotek Solutions的MetrotekGeek撰写&#xff0c;Metrotek Solutions是How-To Geek的朋友 As a computer field tech, I use the remote desktop program…

思科AP-什么是COS AP?

COS:Click OS 所有新的wave 2 AP都带有COS。它建立在IOS之上&#xff0c;但behaves 不同。 COS APs是Click OS APs&#xff08;较新的AP型号&#xff0c;Wave 2等&#xff09; 例如&#xff1a;18xx&#xff0c;28xx&#xff0c;38xx&#xff0c;48xx型号Click OS APs或COS AP。…

[转帖]外壳命名空间扩展

一般介绍 很多人一定用过ZipMagic&#xff0c;对它能把一个压缩文件映射成文件夹感到很奇怪&#xff0c;不知道它使用了什么技术&#xff0c;实际上它用到的技术就是实现了一个外壳的命名空间扩展&#xff08;Shell Namespace Extention&#xff09;。 文件夹和视图&#xff1a…

使Safari在Windows Vista上每20秒停止崩溃

The new Safari for Windows is a very slick browser that beats the pants off everything else in the speed department, but it crashes so much on Windows Vista that it’s virtually unusable. 新的Windows版Safari浏览器非常流畅&#xff0c;可以超越速度部门的所有…

js----与浏览列表有关的对象(浏览器对象)

document  location  history  navigator  screen   frame History 对象包含用户&#xff08;在浏览器窗口中&#xff09;访问过的 URL Location 对象包含有关当前 URL 的信息 Window 对象表示浏览器中打开的窗口 Navigator 对象包含有关浏览器的信息 转载于:https:/…

[svc]jdk+tomcat部署.jforum论坛部署

安装jdk和tomcat jdk1.7.0_13(系列)下载url 我这里用的最新的jdk. 去官网下载即可cd /usr/local/src/ tar xf jdk-8u162-linux-x64.tar.gz -C /usr/local/ ln -s /usr/local/jdk1.8.0_162 /usr/local/jdk tar xf apache-tomcat-8.5.29.tar.gz -C /usr/local/ ln -s /usr/local/…

ipad和iphone切图_如何从iPhone和iPad上的Mail应用程序删除电子邮件帐户

ipad和iphone切图Nicole Lienemann/Shutterstock妮可利尼曼(Nicole Lienemann)/ ShutterstockWhen you add your Google account to your iPhone or iPad in the Settings app, you’re adding your Gmail account to the Mail app. If you prefer to use third-party email cl…

使用nmcli 实现 bond0 网络组 网桥三种模式

使用nmcli 实现 bond 网络组 网桥模式 bond0&#xff08;负载均衡&#xff09; step1&#xff1a;创建一个bond0的主属nmcli connection add con-name bond0 type bond ifname bond0 mode active-backup 之所以不为绿色是因为还没有创建从属&#xff0c;这个bond0相当于一个虚拟…

RabbitMQ是如何运转的?

前言 之前已经介绍了RabbitMQ交换机模型的相关简单概念&#xff0c;都是作为此篇的基础铺垫&#xff0c;如果对此篇不懂的可以先看我上一篇的介绍认识RabbitMQ交换机模型&#xff0c;或者联系评论&#xff0c;分享《RabbitMQ实战指南》电子书给大家&#xff0c;里面虽然有些许错…

如何种植屡获殊荣的青豆

Most people don’t know this yet, but I’ve decided to give up computers and become a farmer instead. Since I’m the helpful type, I’ve decided to share everything I know about farming with you, starting with how I won my prize winning green beans. 大多数…

充分利用Microsoft Planner的6种方法

Microsoft Planner is pretty simple to use, but some of its more useful features aren’t front and center. If you’re just creating and moving tasks, here are six ways to get a bit more out of Planner. Here’s everything you need to know. Microsoft Planner的…

IOS - UTF-8转码问题

2016.07.06 21:45* 字数 61 阅读 921评论 0喜欢 2 IOS中提供的转码。 [utf8str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 转码后发现&#xff0c;与java的不一样。 原来IOS中转码的标准不一致&#xff0c;导致出现错误。 不过&#xff0c;可以使用下…