一、Consecutive Sum Riddle
#include<iostream>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--){
long long n;
cin >> n;
cout << -(n - 1) << " " << n << endl;
}
return 0;
}
二、Special Numbers
#include <iostream>
using namespace std;
#define _rep(i,a,b) for( int i=(a); i<=(b); ++i)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
const int mod = 1e9 + 7;
int n, k, ans;
signed main() {
IOS;
int t;
cin >> t;
while (t-- && cin >> n >> k) {
ans = 0;
int cnt = 0;
while (k) {
int a = 1;
if (k & 1) {
_rep(j, 1, cnt) {
a = (a * n) % mod;
}
ans = (ans + a) % mod;
}
k = k >> 1;
cnt++;
}
cout << ans << "\n";
}
return 0;
}
三、Make Them Equal
第一种情况:
只有一个字符不同,且该字符是最后一个,输出n-1,不然输出n
第二种情况
在字符串的后半段,如果存在当前位置的字符等于目标字符,
直接输出当前下标位置(记得加一)
如果不存在,就输出n-1和n
其他情况特判。
#include <iostream>
#include <string>
using namespace std;
int t, n, ac;
char tmp;
string s;
void solve() {
cin >> t;
while (t-- && cin >> n >> tmp >> s) {
ac = 0;
int ans = n;
for (int i = 0; i < s.size(); i++) {
if (s[i] != tmp) {
ac++;
if (i + 1 == ans) ans--;
}
}
if (ac == 0) {
cout << ac << "\n";
continue;
}
if (ac == 1) {
cout << ac << "\n";
cout << ans << "\n";
continue;
}
ac = 2;
for (int i = s.size() - 1; i >= 0; i--) {
if (s[i] != tmp) continue;
if (i + 1 > (s.size() / 2)) {
ac = 1;
ans = i + 1;
break;
}
}
cout << ac << "\n";
if (ac == 1) {
cout << ans << "\n";
}
else {
cout << s.size() << " " << s.size() - 1 << "\n";
}
}
}
int main()
{
solve();
return 0;
}