Bestcoder Round# 80
[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的更多相关文章
- hdu 5667 BestCoder Round #80 矩阵快速幂
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- BestCoder Round #80 1002
HDU 5666 Segment 题意:给你条斜率为-1,常数项为q(q为质数)的直线,连接原点与直线上整数格点,问你在有多少个格点在形成的无数个三角形内,而不在线段上,结果对P取模. 思路:best ...
- hdu5666 BestCoder Round #80
Segment Accepts: 418 Submissions: 2020 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6553 ...
- BestCoder Round #80 待填坑
Lucky Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- hdoj5667 BestCoder Round #80 【费马小定理(膜拜)+矩阵快速幂+快速幂】
#include<cstdio> #include<string> #include<iostream> #include<vector> #inclu ...
- bestcoder Round #7 前三题题解
BestCoder Round #7 Start Time : 2014-08-31 19:00:00 End Time : 2014-08-31 21:00:00Contest Type : ...
- hdu5634 BestCoder Round #73 (div.1)
Rikka with Phi Accepts: 5 Submissions: 66 Time Limit: 16000/8000 MS (Java/Others) Memory Limit: ...
- 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 ...
- BestCoder Round #89 02单调队列优化dp
1.BestCoder Round #89 2.总结:4个题,只能做A.B,全都靠hack上分.. 01 HDU 5944 水 1.题意:一个字符串,求有多少组字符y,r,x的下标能组成等比数列 ...
随机推荐
- Python中请使用isinstance()判断变量类型
一.isinstance() 在Python中可以使用type()与isinstance()这两个函数判断对象类型,而isinstance()函数的使用上比type更加方便. # coding=utf ...
- bash read命令用法
read -p "Enter your student ID: " USERNAMEread -s -p "Enter your password: " PAS ...
- 【Django】Django 如何支持 分组查询、统计?
代码: from django.db.models import Sum alarm_sum_group_items = models.FILE_PROTECT_ALARM.objects.filte ...
- Windows命令行重命名文件
RENAME D:\Cache\xyz.html xyz%date:~0,4%%date:~5,2%%date:~8,2%.tar.gz
- 现在, Delphi 的多线程已经非常易用了!
先看一个非多线程的例子, 代码执行时不能进行其它操作(譬如拖动窗体): {自定义方法: 在窗体上绘制...} procedure MyMethod; var i: Integer; begin ...
- Extjs读取更改或者发送ajax返回请求的结果简单封装
Extjs的submit()方法提交的数据:如下: this.formPanel.getForm().submit({ url:this.saveUrl, ...
- HDU 3833 YY's new problem ()
YY's new problem Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- BST树
http://www.cnblogs.com/bizhu/archive/2012/08/19/2646328.html 4. 二叉查找树(BST) Technorati 标记: 二叉查找树,BST, ...
- 开启InnoDB每表一个独立的表空间
mysql> show variables like '%innodb%'; +---------------------------------+----------------------- ...
- js “+” 连接字符串&数字相加 数字相加出现多位小数 函数调用单引号双引号嵌套和转义字符的使用
一.机制 JavaScript中,加号不仅表示相加还表示字符串连接 当加号两边存在字符串时,加号代表连接,实际上是将两侧都转为了字符串,如 "1" + 1 = "11&q ...