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. tomcat https协议

    一.tomcat证书 JDK自带的keytool工具来生成证书 1. 在jdk的安装目录\bin\keytool.exe下打开keytool.exe 2. 在命令行中输入以下命令: keytool - ...

  2. python爬虫---从零开始(三)Requests库

    1,什么是Requests库 Requests是用python语言编写,基于urllib,采用Apache2 Licensed 开源协议的HTTP库. 它比urllib更加方便,可以节约我们大量的工作 ...

  3. CF1179D Fedor Runs for President [DP,斜率优化]

    Codeforces 思路 考虑把连的那两个点中间的链提出来,那么就会变成一条链,链上的每个点挂着一棵子树的形式. 设那些子树的大小为\(S_1,S2,\cdots\),那么新加的简单路径个数就是 \ ...

  4. css3浏览器兼容的前缀

    -moz代表firefox浏览器私有属性 -ms代表ie浏览器私有属性 -webkit代表safari.chrome私有属性

  5. encodeURI()与decodeURI()等转码方法

    只针对文本编码 encodeURI() 只针对文本解码 decodeURI()针对文本和特殊字符的编码  encodeURIComponent()针对文本和特殊字符的解码  decodeURIComp ...

  6. 点击增加删除class

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. OpenJudge-百练-2755

    这道题用递归写的话还是很好写的,我们设递归函数的名称为Ways(w,k) . 它的含义就是,w的大小,取k个物品,有多少种方式. 我们可以知道递归的边界条件就是当w的大小为0的时候,我们的方法数只有一 ...

  8. Django之使用celery异步完成发送验证码

    使用celery的目的:将项目中耗时的操作放入一个新的进程实现 1.安装celery pip install celery 2.在项目的文件夹下创建包celery_tasks用于保存celery异步任 ...

  9. 利用RestTemplate进行http调用

    在对接API的时候,会涉及调用第三方的服务,这时候可以利用RestTemplate进行调用,下面给大家展示一个简单的调用demo. package com.tanlu.user.api.control ...

  10. 使用Docker compose编排Laravel应用

    前言 Laravel官方开发环境推荐的是Homestead(其实就是一个封装好的Vagrant box),我感觉这个比较重,于是自己用Docker compose编排了一套开发环境,在这里分享下. 环 ...