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

Problem Description
Let us define a sequence as below

⎧⎩⎨⎪⎪⎪⎪⎪⎪F1F2Fn===ABC⋅Fn−2+D⋅Fn−1+⌊Pn⌋

Your job is simple, for each task, you should output Fn module 109+7.

 
Input
The first line has only one integer T, indicates the number of tasks.

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

 
Sample Input
2
3 3 2 1 3 5
3 2 2 2 1 4
 
Sample Output
36
24
 
解析  p/n 不是定值不能直接矩阵快速幂  但是观察发现
23 
 1     2   3   4   5   6   7   8   9  10  11  12  13...100   除数
 23  12  7   5   4   3   3   2    2   2   2   1    1 .... 0      商

商的个数很少 我们可以 分段矩阵快速幂   怕超时 可以先打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

Problem Description
Mr.Quin love fishes so much and Mr.Quin’s city has a nautical system,consisiting of N ports and M shipping lines. The ports are numbered 1 to N. Each line is occupied by a Weitian. Each Weitian has an identification number.

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)

 
Input
There might be multiple test cases, no more than 20. You need to read till the end of input.

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.

 
Output
For each test case output the minimum required cost. If Mr.Quin can’t travel to port N, output −1 instead.
 
Sample Input
3 3
1 2 1
1 3 2
2 3 1
2 0
3 2
1 2 1
2 3 2
 
Sample Output
1
-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 ;
}

转自  https://blog.csdn.net/jerry99s/article/details/81664117

 

HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij的更多相关文章

  1. HDU 6395 Sequence(分段矩阵快速幂)题解

    题意: 已知\(A,B,C,D,P,n\)以及 \[\left\{ \begin{aligned} & F_1 = A \\ & F_2 = B\\ & F_n = C*F_{ ...

  2. HDU.2640 Queuing (矩阵快速幂)

    HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...

  3. HDU 5667 构造矩阵快速幂

    HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...

  4. BZOJ 2326 数学作业(分段矩阵快速幂)

    实际上,对于位数相同的连续段,可以用矩阵快速幂求出最后的ans,那么题目中一共只有18个连续段. 分段矩阵快速幂即可. #include<cstdio> #include<iostr ...

  5. 数学--数论--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 ...

  6. HDU 6185 Covering 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6185 题意:用 1 * 2 的小长方形完全覆盖 4 * n的矩形有多少方案. 解法:小范围是一个经典题 ...

  7. HDU 2157(矩阵快速幂)题解

    How many ways?? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU 6470 【矩阵快速幂】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 写这道题是为了让自己不要忘记矩阵快速幂如何推出矩阵式子的. 注意 代码是TLE的!! #incl ...

  9. hdu 6395Sequence【矩阵快速幂】【分块】

    Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

随机推荐

  1. IOS代码收集

    http://mobile.51cto.com/hot-410417.htm 退回输入键盘: - (BOOL) textFieldShouldReturn:(id)textField{ [textFi ...

  2. BZOJ 1012: [JSOI2008]最大数maxnumber

    ★★   输入文件:bzoj_1012.in   输出文件:bzoj_1012.out   简单对比时间限制:3 s   内存限制:162 MB [题目描述] 现在请求你维护一个数列,要求提供以下两种 ...

  3. how to make a function from using global var to not using it

    let say, we want to find the bottom left node in a tree.one way to do it is using global vars: /** * ...

  4. dos命令及HTML基础语法

  5. C语言常用关键字及运算符操作---关键字

    每个知识点4问: 1. 是什么? 2. 什么时间用? 3. 怎么用? 4.为什么这么用? 1. 32个关键字 //(1)sizeof 的用法 //sizeof 是关键字,让编译器帮我们查看内存空间存储 ...

  6. MySQL-01 MySQL数据库安装指南

    学习要点 MySQL数据库的安装和设置 下载mysql mysql官网:https://www.mysql.com/downloads/ 主要版本: Oracle MySQL Cloud Servic ...

  7. 事物的四大特性(acid)

    如果一个数据库声称支持事务的操作,那么该数据库必须要具备以下四个特性: ⑴ 原子性(Atomicity) 原子性是指事务包含的所有操作要么全部成功,要么全部失败回滚,这和前面两篇博客介绍事务的功能是一 ...

  8. HUST软件与微电子学院第八届程序设计竞赛-小乐乐下象棋

    这题其实很简单,我们可以用一个bfs搜索出所有的,小于k步的,到不同点不同步数的方案数. 我们首先初始化,走到(0,0)点的时候,我们把步数设置为0,但是方法数设置为1,这是因为我们走零步,到一个点, ...

  9. Springboot整合Shiro安全框架

    最近在学习Springboot,在这个过程中遇到了很多之前都没有技术知识,学习了一阵子,稍微总结一些. ---- Shiro框架 shiro框架,是一个相对比较简便的安全框架,它可以干净利落地处理身份 ...

  10. python:零散记录

    1.rstrip()删除末尾指定字符串 例如:A = '1,2,3,4,5,' B = A.rstrip(',') B = '1,2,3,4,5' 2.isdigit()方法 Python isdig ...