日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

BJOI 2012 求和

發布時間:2024/1/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BJOI 2012 求和 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

https://www.luogu.org/problemnew/show/P4427

因為這個題,我交了整整兩頁...

題目不難,預處理+lca

一個奇坑點:預處理sum的時候,你是不能取模的,因為有可能取模之后sum值變小,而實際上不是,由于你后一步還要做減法

因此就會出鍋,導致我爆零一整頁正確做法是最后取模,雖然我也不知道為什么這樣不會爆炸,300000 *?998244353這都死哪兒去了...

真是血一般的教訓啊,取模不能亂取

#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<climits> #include<vector> #include<cstdlib> #include<ctime> #include<queue> using namespace std; #define mod 998244353 #define maxn 300000 + 10 typedef long long ll; inline ll read() {ll ans = 0,op = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-') op = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){(ans *= 10) += ch - '0';ch = getchar();}return ans * op; } ll power(ll a,ll b) {ll ans = 1,res = a;while(b){if(b & 1)(ans *= res) %= mod;(res *= res) %= mod;b = b >> 1;}return ans % mod; } struct edge {ll to,next; }e[maxn << 1]; ll fir[maxn],alloc; void adde(ll u,ll v) {e[++alloc].next = fir[u];fir[u] = alloc;e[alloc].to = v;swap(u,v);e[++alloc].next = fir[u];fir[u] = alloc;e[alloc].to = v; } ll sum[maxn][60]; ll dep[maxn],f[maxn][31]; void dfs(ll u,ll fa) {dep[u] = dep[fa] + 1;f[u][0] = fa;for(int i = 1;i <= 50;i++)sum[u][i] = sum[fa][i] + power(dep[u],i);for(int i = 1;i <= 20;i++)f[u][i] = f[f[u][i - 1]][i - 1];for(int i = fir[u];i;i = e[i].next){ll v = e[i].to;if(v == fa) continue;dfs(v,u);} } ll lca(ll x,ll y) {if(dep[x] < dep[y]) swap(x,y);for(int i = 19;i >= 0;i--){if(dep[f[x][i]] >= dep[y]) x = f[x][i];if(x == y) return x;}for(int i = 19;i >= 0;i--){if(f[x][i] != f[y][i]){x = f[x][i];y = f[y][i];}}return f[x][0]; } ll n,m; int main() {n = read();for(int i = 1;i <= n - 1;i++){ll u = read(),v = read();adde(u,v);}dep[0] = -1; dfs(1,0); m = read();for(int i = 1;i <= m;i++){ll u = read(),v = read(),k = read();ll to = lca(u,v);printf("%lld\n" , ((sum[u][k] + sum[v][k]) - (sum[to][k] + sum[f[to][0]][k])) % mod);} }

?

?

轉載于:https://www.cnblogs.com/LM-LBG/p/10343580.html

總結

以上是生活随笔為你收集整理的BJOI 2012 求和的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。