A. Consecutive Sum Riddle
题意:
对于正数 x,找到整数(可以为负数)l,r使得 【l,r】之间所有整数的和为 x。
题解:
直接输出1-x和x的就行了
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
int x;
cin>>x;
cout<<1-x<<' '<<x<<endl;
}
B. Special Numbers
题意:
写出从小到大第 k 个满足以下性质的数:由他给的n,问可以分为几个不同的 n 的幂次的和。
题解:
这道题可以把k分解为二进制,再用n进制解释就行了。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
int main()
{
int T;cin>>T;
while (T--)
{
int n,k;cin>>n>>k;
ll res = 1;
ll ans = 0;
for (int i=0;i<32;++i)
{
if (k&1)
{
ans = (ans+res)%mod;
}
res = res*n%mod;
k>>=1;
}
cout<<ans<<endl;
}
}
C. Make Them Equal
题意:
给定一个字符串和一个字符c,当一个x只有当下标i(从1开始)不能被x整除时,s[i]可以变成c,问最小操作次数是多少。
题解:
当字符串中全是c的时候操作就是0次,当字符串中由一个数x满足n/x=1,并且这个位置时c,则只需要一次,因为n/x=1表示1~n中只有一个x的倍数,就是x本身,其他情况都可以通过n和n-1两次操作转化
#include <bits/stdc++.h>
using namespace std;
void slove()
{
int n;
char c;
cin >> n >> c;
string s;
cin >> s;
bool ok = true;
int cnt = 0;
for (auto i : s)
{
if (i != c)
{
ok = false, cnt++;
}
}
if (ok)
{
cout << 0 << endl;
return;
}
if (s[n - 1] != c and cnt == 1)
{
cout << 1 << endl;
cout << n - 1 << endl;
return;
}
if (s[n - 1] == c and cnt)
{
cout << 1 << endl;
cout << n << endl;
return;
}
for (int i = 0; i < n; i++)
{
if (n / (i + 1) <= 1 and s[i] == c)
{
cout << 1 << endl;
cout << i + 1 << endl;
return;
}
}
cout << 2 << endl;
cout << n << ' ' << n - 1 << endl;
}
int main()
{
int t;
cin >> t;
while (t--)
{
slove();
}
}