HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij
http://acm.hdu.edu.cn/showproblem.php?pid=6395
Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1475 Accepted Submission(s): 539
Your job is simple, for each task, you should output Fn module 109+7.
Then, for the next T lines, each line consists of 6 integers, A , B, C, D, P, n.
1≤T≤200≤A,B,C,D≤1091≤P,n≤109
3 3 2 1 3 5
3 2 2 2 1 4
24
商的个数很少 我们可以 分段矩阵快速幂 怕超时 可以先打1e5的表 一开始以为要用逆元解决问题 真的智障了
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" "<<endl;
using namespace std;
typedef long long ll;
const ll maxn=,inf=0x3f3f3f3f,mod=1e9+;
bool Finish_read;
template<class T>inline void read(T &x)
{
Finish_read=;
x=;
int f=;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')f=-;
if(ch==EOF)return;
ch=getchar();
}
while(isdigit(ch))x=x*+ch-'',ch=getchar();
x*=f;
Finish_read=;
}
template<class T>inline void print(T x)
{
if(x/!=)print(x/);
putchar(x%+'');
}
template<class T>inline void writeln(T x)
{
if(x<)putchar('-');
x=abs(x);
print(x);
putchar('\n');
}
template<class T>inline void write(T x)
{
if(x<)putchar('-');
x=abs(x);
print(x);
}
ll n,a,b,c,d,p;
struct Matrix
{
ll m[maxn][maxn];
Matrix()
{
memset(m,,sizeof(m));
}
void init()
{
for(int i=; i<maxn; i++)
for(int j=; j<maxn; j++)
m[i][j]=(i==j);
}
Matrix operator +(const Matrix &b)const
{
Matrix c;
for(int i=; i<maxn; i++)
{
for(int j=; j<maxn; j++)
{
c.m[i][j]=(m[i][j]+b.m[i][j])%mod;
}
}
return c;
}
Matrix operator *(const Matrix &b)const
{
Matrix c;
for(int i=; i<maxn; i++)
{
for(int j=; j<maxn; j++)
{
for(int k=; k<maxn; k++)
{
c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%mod)%mod;
}
}
}
return c;
}
Matrix operator^(const ll &t)const
{
Matrix ans,a=(*this);
ans.init();
ll n=t;
while(n)
{
if(n&) ans=ans*a;
a=a*a;
n>>=;
}
return ans;
}
};
ll f[];
pair<ll,ll> solve(ll x,ll y,ll z,ll m)
{
Matrix a,b,temp;
b.m[][]=d;b.m[][]=c;b.m[][]=;
b.m[][]=;b.m[][]=;
a.m[][]=x;
a.m[][]=y;
a.m[][]=z;
temp=b^(m);
temp=temp*a;
return mp(temp.m[][],temp.m[][]);
}
int main()
{
int t;
read(t);
while(t--)
{
read(a);read(b);read(c);read(d);read(p);read(n);
f[]=a;f[]=b;
for(int i=;i<=;i++)
f[i]=(c*f[i-]%mod+d*f[i-]%mod+p/i)%mod;
if(n<=)
writeln(f[n]);
else
{
ll l=;
pair<ll,ll> pp(f[l],f[l-]);l++;
while(l<=n)
{
ll temp=p/l;
if(temp==)
{
pp=solve(pp.fi,pp.se,temp,n-l+);
writeln(pp.fi);
break;
}
ll temp2=p/temp;
if(temp2>=n)
{
pp=solve(pp.fi,pp.se,temp,n-l+);
writeln(pp.fi);
break;
}
else
{
pp=solve(pp.fi,pp.se,temp,temp2-l+);
l=temp2+;
}
}
}
}
}
http://acm.hdu.edu.cn/showproblem.php?pid=6386
Age of Moyu
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2237 Accepted Submission(s): 683
The i-th (1≤i≤M) line connects port Ai and Bi (Ai≠Bi) bidirectionally, and occupied by Ci Weitian (At most one line between two ports).
When Mr.Quin only uses lines that are occupied by the same Weitian, the cost is 1 XiangXiangJi. Whenever Mr.Quin changes to a line that is occupied by a different Weitian from the current line, Mr.Quin is charged an additional cost of 1 XiangXiangJi. In a case where Mr.Quin changed from some Weitian A's line to another Weitian's line changes to Weitian A's line again, the additional cost is incurred again.
Mr.Quin is now at port 1 and wants to travel to port N where live many fishes. Find the minimum required XiangXiangJi (If Mr.Quin can’t travel to port N, print −1instead)
For each test case,In the first line, two integers N (2≤N≤100000) and M (0≤M≤200000), representing the number of ports and shipping lines in the city.
In the following m lines, each contain three integers, the first and second representing two ends Ai and Bi of a shipping line (1≤Ai,Bi≤N) and the third representing the identification number Ci (1≤Ci≤1000000) of Weitian who occupies this shipping line.
1 2 1
1 3 2
2 3 1
2 0
3 2
1 2 1
2 3 2
-1
2
解析 这题标程有些错误 而且是原题 既然标程是错的 也就不用按照 题解来写了 感觉现在最靠谱的思路就是 建虚点了 边权相同且相邻的点 建立一个虚点x
其他点到x距离为1的单向边 x到其他点距离为0单向边 把原来的去掉 跑最短路就是答案。这道题真是服了。。。
#include <bits/stdc++.h>
#define Pii pair<int,int>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int maxm=2e5+;
const int maxnn=1e6+;
const int inf=;
vector<int> GG[maxn];
int from[maxm],to[maxm],c[maxm];
int n,cnt,nowc,dis[maxnn]; //cnt:n+虚点数
bool v[maxm],vis[maxnn];
struct node{
int d,x;
bool operator < (const node& b) const
{return d>b.d;}
};
priority_queue<node> Q;
int _first[maxnn],_tip[maxnn],_w[maxnn],_next[maxnn],edge;
inline void dijk()
{
int i,u,vv,len,num;
node tmp;
for(i=;i<=cnt;i++) dis[i]=inf;
dis[]=;
Q.push((node){,});
while(!Q.empty())
{
tmp=Q.top();
Q.pop();
u=tmp.x;
if(vis[u]==true) continue;
vis[u]=true;
num=_first[u];
while(num!=-)
{
vv=_tip[num];
if(dis[u]!=inf&&dis[vv]>dis[u]+_w[num])
{
dis[vv]=dis[u]+_w[num];
Q.push((node){dis[vv],vv});
}
num=_next[num];
}
}
return;
}
inline void dfs(int x)
{
//add(x,cnt,1);
_tip[++edge]=cnt;
_w[edge]=;
_next[edge]=_first[x];
_first[x]=edge; //add(cnt,x,0);
_tip[++edge]=x;
_w[edge]=;
_next[edge]=_first[cnt];
_first[cnt]=edge; int i,len=GG[x].size(),num;
for(i=;i<len;i++)
{
num=GG[x][i];
if(c[num]==nowc&&!v[num])
{
v[num]=true;
if(from[num]==x) dfs(to[num]);
else dfs(from[num]);
}
}
return;
}
int main()
{
int i,m;
while(scanf("%d%d",&n,&m)==)
{
edge=;
cnt=n;
memset(v,,sizeof(v));
memset(vis,,sizeof(vis));
memset(_first,-,sizeof(_first));
for(i=;i<=m;i++)
{
scanf("%d%d%d",&from[i],&to[i],&c[i]);
GG[from[i]].push_back(i);
GG[to[i]].push_back(i);
}
for(i=;i<=m;i++)
if(!v[i])
{
cnt++;
nowc=c[i];
dfs(from[i]);
}
dijk();
if(dis[n]==inf) printf("-1\n");
else printf("%d\n",dis[n]);
//test();
for(i=;i<=n;i++) GG[i].clear();
}
return ;
}
HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij的更多相关文章
- HDU 6395 Sequence(分段矩阵快速幂)题解
题意: 已知\(A,B,C,D,P,n\)以及 \[\left\{ \begin{aligned} & F_1 = A \\ & F_2 = B\\ & F_n = C*F_{ ...
- HDU.2640 Queuing (矩阵快速幂)
HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...
- HDU 5667 构造矩阵快速幂
HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...
- BZOJ 2326 数学作业(分段矩阵快速幂)
实际上,对于位数相同的连续段,可以用矩阵快速幂求出最后的ans,那么题目中一共只有18个连续段. 分段矩阵快速幂即可. #include<cstdio> #include<iostr ...
- 数学--数论--HDU - 6395 Let us define a sequence as below 分段矩阵快速幂
Your job is simple, for each task, you should output Fn module 109+7. Input The first line has only ...
- HDU 6185 Covering 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6185 题意:用 1 * 2 的小长方形完全覆盖 4 * n的矩形有多少方案. 解法:小范围是一个经典题 ...
- HDU 2157(矩阵快速幂)题解
How many ways?? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 6470 【矩阵快速幂】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 写这道题是为了让自己不要忘记矩阵快速幂如何推出矩阵式子的. 注意 代码是TLE的!! #incl ...
- hdu 6395Sequence【矩阵快速幂】【分块】
Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
随机推荐
- mac下iterm2配置安装,通过expact实现保存账号,及通过跳板登陆配置
在参考了几款mac不错的ssh工具外,最终选择使用iterm2.本来打算用FinalShell,安装后发现其icon在访达中根本不现实,而且每次访问还需要输入管理员账号密码,强迫症根本受不了... 官 ...
- 初探ABP--记一些常见的开发问题
1.Update-Database : 无法将“Update-Database”项识别为 cmdlet.函数.脚本文件或可运行程序的名称.请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次. ...
- 洛谷 P1910 L国的战斗之间谍(水题日常)
题目背景 L国即将与I国发动战争!! 题目描述 俗话说的好:“知己知彼,百战不殆”.L国的指挥官想派出间谍前往I国,于是,选人工作就落到了你身上. 你现在有N个人选,每个人都有这样一些数据:A(能得到 ...
- 2015 AlBaath Collegiate Programming Contest(2月14日训练赛)
A (By ggg): 题意:一个人还有x秒到红绿灯,这个红绿灯有g秒绿灯,y秒黄 灯,r秒红灯,问你到红绿灯的时候是什么灯.值得注意的是绿 灯变黄灯时,第g秒是黄灯了. B (By Anxdada) ...
- mybatis中存储过程的调用
dao层 // 调用存储过程 void callProcedureGrantEarnings(@Param("params") Map<String,Object> p ...
- postman对登陆进行压力测试的方法
1.填写完整参数,设置好变量,选择好环境,保存好 2.将变量mobile_phone和password的值以如下图的格式,填写到Excel表格中,然后以csv(逗号分隔)的形式进行保存 3.点击此测试 ...
- GetForgroundWindow函数的不确定性——BUG笔记
HWND GetForgoundWindows() 获取当前前置窗口在windows 7和windows 10下虚拟桌面切换后表现不同. 所以强烈不建议使用此函数!
- 在window下搭建即时即用的hyperledger fabric 的环境
有版本号的严格按要求,遇到不少坑 1)安装git 版本无要求 2)安装go 1.9 配置环境变量 3)安装Vagrant 1.9.4 4)安装VirtualBox 5.1.28 5)在go ...
- sqlserver还原3101
1.出现错误"3101" 2.解决办法:删除数据库之后还原(有风险)或者获得数据库的独占访问权(用sql语句) 参考:https://www.2cto.com/database/2 ...
- 常见的User-Agent
User_Agent = ["Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_2 like Mac OS X; zh-cn) AppleWebKit/533. ...