Wannafly挑战赛11 A B D【规律+逆元+字符串hash】
鏈接:https://www.nowcoder.com/acm/contest/73/A
來源:牛客網
題目描述
白兔學會了分身術。
一開始有一只白兔,接下來會進行k輪操作,每一輪中每一只白兔都會變成p只白兔。
要求k輪后白兔的總數恰好為n。
要求找到兩個正整數p,k,最大化p+k
輸入描述:
輸入一個正整數n(2<=n<=1018)
輸出描述:
輸出一個整數,p+k的最大值
示例1
輸入
2
輸出
3
說明
p=2,k=1
分析:思維題,輸出n+1即可;
#include <bits/stdc++.h> using namespace std; typedef long long LL;int main() {LL n;scanf("%lld", &n);printf("%lld\n", n + 1);return 0; }鏈接:https://www.nowcoder.com/acm/contest/73/B
來源:牛客網
題目描述
已知f[1][1]=1,f[i][j]=a*f[i-1][j]+b*f[i-1]=2,1<=j<=i”>j-1。
對于其他情況f[i][j]=0
有T組詢問,每次給出a,b,n,m,求f[n][m] mod (998244353)
輸入描述:
第一行為一個整數T,表示詢問個數。
接下來一共T行,每行四個整數a,b,n,m。
輸出描述:
一共T行,每行一個整數,表示f[n][m] mod (998244353)
示例1
輸入
2
2 3 3 3
3 1 4 1
輸出
9
27
備注:
T<=100000
1<=m<=n<=100000
0<=a,b<=109
分析:找出規律,用逆元處理一下分母即可;
#include<bits/stdc++.h> using namespace std; #define LL long longconst int p = 998244353; const int N = 100000+11; LL k[N];LL qkm(LL a,LL b,LL c) {LL s=1,base=a%c;while(b){if(b&1) s=s*base%c;base=base*base%c;b>>=1;}return s%c; }LL C(LL n,LL m,LL p){return ( ( k[n]*qkm( ( k[n-m]*k[m]) %p , p-2 , p ) ) %p ); }void init(){k[0]=1;for(int i=1;i<=N;i++)(k[i]=k[i-1]*i)%=p; }int main(){init();int t;cin>>t;while(t--){LL a,b,n,m;scanf("%lld%lld%lld%lld",&a,&b,&n,&m);LL ans=1;ans=(ans*qkm(a,n-m,p))%p;ans=(ans*qkm(b,m-1,p))%p;ans=(ans*C(n-1,m-1,p))%p;printf("%lld\n",ans);}return 0; }鏈接:https://www.nowcoder.com/acm/contest/73/D
來源:牛客網
題目描述
白兔有一個字符串T。白云有若干個字符串S1,S2..Sn。
白兔想知道,對于白云的每一個字符串,它有多少個子串是和T循環同構的。
提示:對于一個字符串a,每次把a的第一個字符移動到最后一個,如果操作若干次后能夠得到字符串b,則a和b循環同構。
所有字符都是小寫英文字母
輸入描述:
第一行一個字符串T(|T|<=10^6)
第二行一個正整數n (n<=1000)
接下來n行為S1~Sn (|S1|+|S2|+…+|Sn|<=10^7),max(|S1|,|S2|,|S3|,|S4|,..|Sn|)<=10^6
輸出描述:
輸出n行表示每個串的答案
示例1
輸入
abab
2
abababab
ababcbaba
輸出
5
2
思路:字符串hash,查詢時不宜用map,用鄰接表維護。。。
#include<bits/stdc++.h> #define ull unsigned long long using namespace std; typedef long long LL;const int mod = 1410753; const int MAXN = 2e6 + 10; const ull base = 233; char str[MAXN], ch[MAXN]; int head[MAXN]; ull has[MAXN], a[MAXN]; int cnt = 0;struct node {ull to;int next; }edge[MAXN];void add_edge(ull x) {int pos = x % mod;edge[cnt].to = x;edge[cnt].next = head[pos];head[pos] = cnt++; }int find(ull x) {int pos = x % mod;int u = head[pos];for(int i = u; ~i; i = edge[i].next) {ull v = edge[i].to;if(v == x) return 1;}return 0; } inline void init_hash() {a[0] = 1;for(int i = 1; i < MAXN; ++i) {a[i] = a[i - 1] * base;}int len = strlen(str + 1);has[0] = 0;for(int i = 1; i <= len + len; ++i) {has[i] = has[i - 1] * base + str[i];if(i >= len) {str[i + 1] = str[i - len + 1];add_edge(has[i] - has[i - len] * a[len]);}} }int main() {memset(head, -1, sizeof(head));scanf("%s", str + 1);int T, len = strlen(str + 1);init_hash();scanf("%d", &T);while(T--) {int sum = 0;scanf("%s", ch + 1);int p = strlen(ch + 1);if(p >= len) {for(int i = 1; i <= p; ++i) {has[i] = has[i - 1] * base + ch[i];if(i >= len) sum += find(has[i] - has[i - len] * a[len]);}}printf("%d\n", sum);}return 0; }總結
以上是生活随笔為你收集整理的Wannafly挑战赛11 A B D【规律+逆元+字符串hash】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 量子相干与量子纠缠_量子硬件101
- 下一篇: 计算机硬件 软件指什么,什么叫软件,什么