[1003 Sequence]

指数循环节,注意a mod p = 0的情况。此时你的循环节如果返回0,这时你会输出1,而实际上应该是0

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; typedef long long ll;
ll n, a, b, c, p; struct Matrix{
ll a[3][3];
void clear(){memset(a, 0, sizeof a);}
void set(){clear(); a[0][0] = a[1][1] = a[2][2] = 1;}
}mat, ans; Matrix operator * (const Matrix& a, const Matrix& b){
Matrix c; c.clear();
for(int i = 0; i < 3; i ++)
for(int j = 0; j < 3; j ++)
for(int k = 0; k < 3; k ++)
(c.a[i][j] += a.a[i][k] * b.a[k][j]) %= (p-1);
return c;
} Matrix power(Matrix a, ll b){
Matrix ret; ret.set();
while(b > 0){
if(b & 1)ret = ret * a;
b >>= 1;
a = a * a;
}return ret;
} ll power_mod(ll a, ll b){
ll ret = 1;
while(b > 0){
if(b & 1)ret = ret * a % p;
b >>= 1;
a = a * a % p;
}return ret;
} int main(){
int test;
scanf("%d", &test);
while(test --){
cin >> n >> a >> b >> c >> p;
if(n == 1){
cout << 1 % p << endl;
continue;
}
if(a % p == 0){
cout << 0 << endl;
continue;
}
mat.clear();
mat.a[0][0] = c, mat.a[1][0] = 1, mat.a[2][0] = b;
mat.a[0][1] = 1;
mat.a[2][2] = 1;
ans.clear();
ans.a[0][0] = b, ans.a[0][1] = 0, ans.a[0][2] = 1;
ans = ans * power(mat, n - 2);
cout << power_mod(a, ans.a[0][0]) << endl;
}
return 0;
}

  

[1005 Road]

建立两棵线段树跑分层图(据说要Dijkstra+Heap?)。注意第二棵的叶子节点向第一棵的叶子节点连边。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#define maxn 800010
using namespace std;
int n, m, k;
struct Edge{
int to, next, dis;
}edge[maxn << 2];
int h[maxn], cnt, S, T, posT;
void add(int u, int v, int d){
cnt ++;
edge[cnt].to = v;
edge[cnt].next = h[u];
edge[cnt].dis = d;
h[u] = cnt;
} vector<int> v1, v2;
#define lc (id << 1)
#define rc (id << 1 | 1) void build(int id, int l, int r){
if(l == r){
if(l == 1)S = id;
posT = max(posT, id);
return;
}
int mid = l + r >> 1;
build(lc, l, mid);
build(rc, mid+1, r);
add(lc, id, 0);
add(rc, id, 0);
} void build2(int id, int l, int r){
if(l == r){
add(id + posT, id, 0);
if(l == n)T = id + posT;
return;
}
int mid = l + r >> 1;
build2(lc, l, mid);
build2(rc, mid+1, r);
add(id + posT, lc + posT, 0);
add(id + posT, rc + posT, 0);
} void ask1(int id, int l, int r, int L, int R){
if(l == L && r == R){
v1.push_back(id);
return;
}
int mid = l + r >> 1;
if(R <= mid)ask1(lc, l, mid, L, R);
else if(L > mid)ask1(rc, mid+1, r, L, R);
else ask1(lc, l, mid, L, mid), ask1(rc, mid+1, r, mid+1, R);
} void ask2(int id, int l, int r, int L, int R){
if(l == L && r == R){
v2.push_back(id);
return;
}
int mid = l + r >> 1;
if(R <= mid)ask2(lc, l, mid, L, R);
else if(L > mid)ask2(rc, mid+1, r, L, R);
else ask2(lc, l, mid, L, mid), ask2(rc, mid+1, r, mid+1, R);
} queue<pair<int, int> > Q;
int dis[maxn][11];
bool vis[maxn][11]; int main(){
int test;
scanf("%d", &test);
scanf("%d%d%d", &n, &m, &k);
build(1, 1, n);
build2(1, 1, n);
int a, b, c, d, w, tot = posT << 1;
for(int i = 1; i <= m; i ++){
scanf("%d%d%d%d%d", &a, &b, &c, &d, &w);
v1.clear(), v2.clear();
ask1(1, 1, n, a, b);
ask2(1, 1, n, c, d);
++ tot;
for(int j = 0; j < v1.size(); j ++) add(v1[j], tot, 0);
for(int j = 0; j < v2.size(); j ++) add(tot, v2[j] + posT, w); ++ tot;
for(int j = 0; j < v2.size(); j ++) add(v2[j], tot, 0);
for(int j = 0; j < v1.size(); j ++) add(tot, v1[j] + posT, w);
} memset(dis, 0x7f, sizeof dis);
Q.push(make_pair(S, 0)); dis[S][0] = 0;
while(!Q.empty()){
int u = Q.front().first, k_ = Q.front().second;
Q.pop(); vis[u][k_] = false;
for(int i = h[u]; i; i = edge[i].next){
int v = edge[i].to;
if(dis[v][k_] > dis[u][k_] + edge[i].dis){
dis[v][k_] = dis[u][k_] + edge[i].dis;
if(!vis[v][k_])vis[v][k_] = true, Q.push(make_pair(v, k_));
}
if(k_ < k && dis[v][k_+1] > dis[u][k_]){
dis[v][k_+1] = dis[u][k_];
if(!vis[v][k_+1])vis[v][k_+1] = true, Q.push(make_pair(v, k_+1));
}
}
} int ans = 0x7fffffff;
for(int i = 0; i <= k; i ++)
ans = min(ans, dis[T][i]);
if(ans > 1e8)printf("CreationAugust is a sb!");
else printf("%d\n", ans);
return 0;
}

  

Bestcoder Round# 80的更多相关文章

  1. hdu 5667 BestCoder Round #80 矩阵快速幂

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

  2. BestCoder Round #80 1002

    HDU 5666 Segment 题意:给你条斜率为-1,常数项为q(q为质数)的直线,连接原点与直线上整数格点,问你在有多少个格点在形成的无数个三角形内,而不在线段上,结果对P取模. 思路:best ...

  3. hdu5666 BestCoder Round #80

    Segment  Accepts: 418  Submissions: 2020  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 6553 ...

  4. BestCoder Round #80 待填坑

    Lucky Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  5. hdoj5667 BestCoder Round #80 【费马小定理(膜拜)+矩阵快速幂+快速幂】

    #include<cstdio> #include<string> #include<iostream> #include<vector> #inclu ...

  6. bestcoder Round #7 前三题题解

    BestCoder Round #7 Start Time : 2014-08-31 19:00:00    End Time : 2014-08-31 21:00:00Contest Type : ...

  7. hdu5634 BestCoder Round #73 (div.1)

    Rikka with Phi  Accepts: 5  Submissions: 66  Time Limit: 16000/8000 MS (Java/Others)  Memory Limit: ...

  8. hdu 4956 Poor Hanamichi BestCoder Round #5(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956 Poor Hanamichi Time Limit: 2000/1000 MS (Java/Ot ...

  9. BestCoder Round #89 02单调队列优化dp

    1.BestCoder Round #89 2.总结:4个题,只能做A.B,全都靠hack上分.. 01  HDU 5944   水 1.题意:一个字符串,求有多少组字符y,r,x的下标能组成等比数列 ...

随机推荐

  1. 【转】PowerDesigner使用方法小结

    本文转自:http://www.cnblogs.com/afarmer/archive/2012/11/05/2755327.html PowerDesigner多用来进行数据库模型设计,具有SQL语 ...

  2. Android 中的Force Close

    今天写程序时遇到一个问题,领导希望在点击了setting里的force close 后,程序依然能够响应以前用alarmManager注册的receiver. 在网上看到了一些文章,写的是如何建立一个 ...

  3. MySQL的LIMIT与分页优化

    在系统中需要进行分页操作的时候,我们通常会使用LIMIT加上偏移量的办法实现,同时加上合适的ORDER BY子句.如果有对应的索引,通常效率会不错,否则,MySQL需要做大量的文件排序操作. 一个非常 ...

  4. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. hp unix

    HP-UNIX 命令大全 #vgdisplay -v vgname #lvdisplay -v lvfullpath #pvdisplay -v pvfullpath # ioscan –fnkC d ...

  6. Eclipse 代码格式化

    http://blog.csdn.net/prstaxy/article/details/7839197 http://jingyan.baidu.com/article/9158e00044efb6 ...

  7. Oracle主键操作

    http://blog.csdn.net/zhanggnol/article/details/6221895

  8. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. MQ的通讯模式

    1) 点对点通讯:点对点方式是最为传统和常见的通讯方式,它支持一对一.一对多.多对多.多对一等多种配置方式,支持树状.网状等多种拓扑结构. 2) 多点广播:MQ适用于不同类型的应用.其中重要的,也是正 ...

  10. [译]C#控制管理VisualSVN Server

    VisualSVN Server可以用WMI接口管理(Windows Management Instrumentation). VisualSVN Server安装的计算机中,位于%VISUALSVN ...