官网链接
B. A Perfectly Balanced String?
Let’s call a string s perfectly balanced if for all possible triplets (t,u,v) such that t is a non-empty substring of s and u and v are characters present in s, the difference between the frequencies of u and v in t is not more than 1.
For example, the strings “aba” and “abc” are perfectly balanced but “abb” is not because for the triplet (“bb”,‘a’,‘b’), the condition is not satisfied.
You are given a string s consisting of lowercase English letters only. Your task is to determine whether s is perfectly balanced or not.
A string b is called a substring of another string a if b can be obtained by deleting some characters (possibly 0) from the start and some characters (possibly 0) from the end of a.
Input
The first line of input contains a single integer t (1 ≤ t ≤ 2⋅104) denoting the number of testcases.
Each of the next t lines contain a single string s (1 ≤ |s| ≤ 2⋅105), consisting of lowercase English letters.
It is guaranteed that the sum of |s| over all testcases does not exceed 2⋅105.
Output
For each test case, print “YES” if s is a perfectly balanced string, and “NO” otherwise.
You may print each letter in any case (for example, “YES”, “Yes”, “yes”, “yEs” will all be recognized as positive answer).
Example
input
5
aba
abb
abc
aaaaa
abcba
output
YES
NO
YES
YES
NO
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define lowbit(x) (x & -x)
#define fi first
#define se second
#define endl '\n'
#define pb push_back
template <typename T> bool chkMax(T &x,T y){return y > x ? x = y,1 : 0;}
template <typename T> bool chkMin(T &x,T y){return y < x ? x = y,1 : 0;}
template <typename T> void inline read(T &x){
int f = 1;x = 0;char c = getchar();
while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
while(c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48),c = getchar();
x *= f;
}
void solve()
{
string s;
cin >> s;
set<char> S;
for(auto c : s)
S.insert(c);
bool flag = true;
for(int i = 0;i < s.size();i++)
if(s[i] != s[i % S.size()])
{
flag = false;
break;
}
if(flag) cout << "YES" << endl;
else cout << "NO" << endl;
return;
}
int main()
{
IOS;
int T;
cin >> T;
while(T--)
solve();
return 0;
}
方法二:
对于s中出现的每一个字母c我们分别check是否存在一个不包含c的子串,其中某个字母会出现两次或以上,如果是的话说明不满足条件。
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl '\n'
int cnt[30],n;
string s;
bool check(int x)
{
int c[30];
for(int i = 0;i < n;i++)
if(s[i] - 'a' != x)
{
memset(c,0,sizeof(c));//每次进行一次清空
int j = i;
c[s[i] - 'a']++;
while(j + 1 <= n - 1 && s[j + 1] - 'a' != x)
{
j++;
if(++c[s[j] - 'a'] > 1) return false;
}
}
return true;
}
void solve()
{
cin >> s;
n = s.size();
memset(cnt,0,sizeof(cnt));
for(int i = 0;i < n;i++) cnt[s[i] - 'a'] ++;
bool flag = true;
for(int i = 0;i < 26;i++)
if(cnt[i])
if(!check(i))
{
flag = false;
break;
}
cout << (flag ? "YES" : "NO") << endl;
return;
}
int main()
{
IOS;
int T;
cin >> T;
while(T--)
solve();
return 0;
}
C. Palindrome Basis
You are given a positive integer n. Let’s call some positive integer a without leading zeroes palindromic if it remains the same after reversing the order of its digits. Find the number of distinct ways to express n as a sum of positive palindromic integers. Two ways are considered different if the frequency of at least one palindromic integer is different in them. For example, 5=4+1 and 5=3+1+1 are considered different but 5=3+1+1 and 5=1+3+1 are considered the same.
Formally, you need to find the number of distinct multisets of positive palindromic integers the sum of which is equal to n.
Since the answer can be quite large, print it modulo 109+7.
Input
The first line of input contains a single integer t (1 ≤ t ≤ 104) denoting the number of testcases.
Each testcase contains a single line of input containing a single integer n (1 ≤ n ≤ 4⋅104) — the required sum of palindromic integers.
Output
For each testcase, print a single integer denoting the required answer modulo 109+7.
Example
input
2
5
12
output
7
74
思路:
多个回文数组合等于一个给定的数。在4e4范围内有498个回文数字,故可以先预处理出这些回文数,然后跑一遍完全背包即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define lowbit(x) (x & -x)
#define fi first
#define se second
#define endl '\n'
#define pb push_back
template <typename T> bool chkMax(T &x,T y){return y > x ? x = y,1 : 0;}
template <typename T> bool chkMin(T &x,T y){return y < x ? x = y,1 : 0;}
template <typename T> void inline read(T &x){
int f = 1;x = 0;char c = getchar();
while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
while(c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48),c = getchar();
x *= f;
}
const int N = 4e4 + 10,mod = 1e9+7;
vector<int> pal,f(N);
void init()
{
pal.push_back(0);
for(int i = 1;i <= 4e4;i++)
{
string t = to_string(i);
reverse(t.begin(),t.end());
if(to_string(i) == t)
pal.push_back(i);
}
return;
}
void solve()
{
f[0] = 1;
for(int i = 1;i < pal.size();i++)
for(int j = pal[i];j <= 4e4;j++)
f[j] = (f[j] + f[j - pal[i]]) % mod;
return;
}
int main()
{
IOS;
init();
solve();
int T;
cin >> T;
while(T--)
{
int x;
cin >> x;
cout << f[x] << endl;
}
return 0;
}
D. Lost Arithmetic Progression
Long ago, you thought of two finite arithmetic progressions A and B. Then you found out another sequence C containing all elements common to both A and B. It is not hard to see that C is also a finite arithmetic progression. After many years, you forgot what A was but remember B and C. You are, for some reason, determined to find this lost arithmetic progression. Before you begin this eternal search, you want to know how many different finite arithmetic progressions exist which can be your lost progression A.
Two arithmetic progressions are considered different if they differ in their first term, common difference or number of terms.
It may be possible that there are infinitely many such progressions, in which case you won’t even try to look for them! Print −1 in all such cases.
Even if there are finite number of them, the answer might be very large. So, you are only interested to find the answer modulo 109+7.
Input
The first line of input contains a single integer t (1≤t≤100) denoting the number of testcases.
The first line of each testcase contains three integers b, q and y (−109 ≤ b ≤ 109, 1 ≤ q ≤ 109, 2 ≤ y ≤ 109) denoting the first term, common difference and number of terms of B respectively.
The second line of each testcase contains three integers c, r and z (−109 ≤ c ≤ 109, 1 ≤ r ≤ 109, 2 ≤ z ≤ 109) denoting the first term, common difference and number of terms of C respectively.
Output
For each testcase, print a single line containing a single integer.
If there are infinitely many finite arithmetic progressions which could be your lost progression A, print −1.
Otherwise, print the number of finite arithmetic progressions which could be your lost progression A modulo 109+7. In particular, if there are no such finite arithmetic progressions, print 0.
Example
input
8
-3 1 7
-1 2 4
-9 3 11
0 6 3
2 5 5
7 5 4
2 2 11
10 5 3
0 2 9
2 4 3
-11 4 12
1 12 2
-27 4 7
-17 8 2
-8400 420 1000000000
0 4620 10
output
0
10
-1
0
-1
21
0
273000
做法:
思维题 + 找规律
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define lowbit(x) (x & -x)
#define fi first
#define se second
#define endl '\n'
#define pb push_back
template <typename T> bool chkMax(T &x,T y){return y > x ? x = y,1 : 0;}
template <typename T> bool chkMin(T &x,T y){return y < x ? x = y,1 : 0;}
template <typename T> void inline read(T &x){
int f = 1;x = 0;char c = getchar();
while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
while(c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48),c = getchar();
x *= f;
}
ll gcd(ll a,ll b){return b ? gcd(b,a % b) : a;}//求最大公约数
const int mod = 1e9+7;
void solve()
{
ll b,q,y,c,r,z;
cin >> b >> q >> y;
cin >> c >> r >> z;
if((r % q) || (c - b) % q || (c + (z - 1) * r > b + (y - 1) * q) || (c < b))//无解
{
cout << "0" << endl;
return;
}
if(c - r < b || b + (y - 1) * q < c + z * r)//无穷解
{
cout << "-1" << endl;
return;
}
ll ans = 0;
for(int i = 1;i <= r / i;i++)//记得从1开始哦!
{
if(r % i == 0)
{//还需要满足最小公倍数条件
if(r == i * q / gcd(i,q)) ans = (ans + r / i * r / i) % mod;
}
if(i != r / i)
{
ll t = r / i;
if(r == t * q / gcd(t,q)) ans = (ans + r / t * r / t) % mod;
}
}
cout << ans << endl;
return;
}
int main()
{
IOS;
int T;
cin >> T;
while(T--)
solve();
return 0;
}