U562064 求和
简单推世子,过了
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
ll T, n;
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> T;
while(T --){
cin >> n;
cout << (n) % MOD * (n + 1) / 2 % MOD * (n + 2) % MOD << '\n';
}
return 0;
}U562085 修理
原本应该 AC 的,空间爆炸了。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e3 + 10;
struct node{
ll x, y;
}a[N];
ll n, m, t, sx, sy, cnt, minx = 1e18, d[20][N][N];
ll dx[] = {0, 0, -1, 1};
ll dy[] = {1, -1, 0, 0};
ll dp[1 << 17][17];
char ch[N][N];
void bfs(ll x, ll y, ll id){
queue<node> q;
for(ll i = 1;i <= n;i ++)
for(ll j = 1;j <= n;j ++)d[id][i][j] = -1;
q.push({x, y});
d[id][x][y] = 0;
while(!q.empty()){
node u = q.front();
q.pop();
for(ll i = 0;i < 4;i ++){
ll tx = u.x + dx[i], ty = u.y + dy[i];
if(tx >= 1 && ty >= 1 && tx <= n && ty <= n && ch[tx][ty] != '#' && d[id][tx][ty] == -1){
d[id][tx][ty] = d[id][u.x][u.y] + 1;
q.push({tx, ty});
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m >> t;
for(ll i = 1;i <= n;i ++)
for(ll j = 1;j <= m;j ++){
cin >> ch[i][j];
if(ch[i][j] == 'S')sx = i, sy = j;
else if(ch[i][j] == 'T')cnt++, a[cnt] = {i, j};
}
a[0] = {sx, sy}, cnt++;
for(ll i = 0;i < cnt;i ++){ll x = a[i].x, y = a[i].y;bfs(x, y, i);}
memset(dp, 0x3f, sizeof(dp));
dp[1][0] = 0;
for(ll i = 0;i < (1 << cnt);i ++)
for(ll j = 0;j < cnt;j ++){
if(!(i & (1 << j)))continue;
for(ll k = 0;k < cnt;k ++){
if(!(i & (1 << k))) continue;
dp[i][j] = min(dp[i][j], dp[i ^ (1 << j)][k] + d[k][a[j].x][a[j].y] + t);
}
}
for(ll i = 1;i <= cnt;i ++) minx = min(minx, dp[(1 << cnt) - 1][i] + d[i][a[0].x][a[0].y]);
cout << minx << '\n';
return 0;
}U562068 经典多米诺
首先分析没有障碍的情况,那么很简单,就是一个斐波那契数列的改版,非常简单,但是如果多了障碍的情况,那么无非就是将几个斐波那契数进行相乘即为它的方案数,所以我们对其进行一个记忆化搜索,由于第 个斐波那契数就已经大于 了,所以使用记忆化搜索就可以过掉这道题了。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 89;
ll T, dp[N], n;
map<ll, ll> f;
ll dfs(ll u, ll p){
if(u == 1)return -1;
if(f.count(u)) return f[u];
f[u] = 1e9;
for(ll i = p;i <= 87 && dp[i] <= u;i ++)
if(u % dp[i] == 0)f[u] = min(f[u], dfs(u / dp[i], i) + i + 1);
return f[u];
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> T;
dp[1] = 1, dp[2] = 2;
for(int i = 3;i <= 87;i ++)dp[i] = dp[i - 1] + dp[i - 2];
while(T --){
f.clear();
f[1] = 1;
cin >> n;
if(n == 1){
cout << 1 << '\n';
continue;
}
ll ans = dfs(n, 2);
cout << (ans == 1e9 ? 0 : ans) << '\n';
}
return 0;
}U396074 外送
由于可以发现,数据范围就是很模板的状压,但是经过仔细的思考就可以发现,直接使用普通的二进制状压是不可以完整的描述整个状态的,因为此题可以一次性取多个包裹,然后一次性全部送过去,所以我们可以使用三进制状压来表示这个状态,然后其它的转移就非常模板了,主要是提取三进制下的某一位数字,给出以下代码:
void init() {
th[1] = 1;
for(int i = 2; i <= q + 1; i ++) { th[i] = th[i - 1] * 3; }
return ;
}
int tmp = i % th[k + 1] / th[k]; // 提取第k位#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 20 + 5, mod = 1e9 + 7;
struct NODE{int s, t, l, r;} a[15];
int n, m, q, d[N][N];
void floyd(){
for(int i = 1;i <= n;i ++)
for(int j = 1;j <= n;j ++)
for(int k = 1;k <= n;k ++)
d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
return;
}
int th[N], dp[N][60005];
void init(){
th[1] = 1;
for(int i = 2;i <= q + 1;i ++)th[i] = th[i - 1] * 3;
return ;
}
void solve(int maxx){
memset(dp, 0x3f, sizeof(dp));
dp[1][0] = 0;
for(int i = 0;i <= maxx;i ++)
for(int j = 1;j <= n;j ++)
for(int k = 1;k <= q;k ++){
int tmp = i % th[k + 1] / th[k];
if(tmp == 0 && dp[j][i] + d[j][a[k].s] <= a[k].r)dp[a[k].s][i + th[k]] = min(dp[a[k].s][i + th[k]], max(a[k].l, dp[j][i] + d[j][a[k].s]));
if(tmp == 1 && dp[j][i] + d[j][a[k].t] <= a[k].r)dp[a[k].t][i + th[k]] = min(dp[a[k].t][i + th[k]], dp[j][i] + d[j][a[k].t]);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m >> q;
for(int i = 1;i <= n;i ++){
for(int j = 1;j <= n;j ++){
if(i == j)d[i][j] = 0;
else d[i][j] = 0x3f3f3f3f;
}
}
int st, ed, val;
while(m--)cin >> st >> ed >> val,d[st][ed] = min(d[st][ed], val);
for(int i = 1;i <= q;i ++)cin >> a[i].s >> a[i].t >> a[i].l >> a[i].r;
floyd();
init();
solve(th[q + 1] - 1);
int ans = 0;
for(int i = 1;i <= n;i ++){
for(int j = 0;j <= th[q + 1] - 1;j ++){
if(dp[i][j] >= 0x3f3f3f3f)continue;
int tmp = 0;
for (int k = 1;k <= q;k ++)tmp += (((j % th[k + 1] / th[k]) == 2) ? 1 : 0);
ans = max(ans, tmp);
}
}
cout << ans;
return 0;
}