4425. 改变数字
思路:特判除了第一位不变成0,模拟即可
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL fpower(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=1e5+10;
void solve()
{
string s;
cin>>s;
if(s[0]!='9')
{
if(s[0]-'0'>(9-(s[0]-'0')))s[0]=9-(s[0]-'0')+'0';
}
for(int i=1;i<s.size();i++)
{
if(s[i]-'0'>(9-(s[i]-'0')))s[i]=9-(s[i]-'0')+'0';
}
cout<<s<<endl;
}
signed main()
{
io;
solve();
return 0;
}
4426. 整除子串
思路:发现能被4整除的是每两位一个周期,所以:
1.如果位数>=2,只要后两位能被4整除,那么这个数就能被4整除
2.如果位数==1,那么只有这个位能被4整除,整个数字不一定被4整除
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL fpower(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=1e5+10;
void solve()
{
string s;
cin>>s;
LL res=0;
for(int i=0;i<s.size();i++)
{
if((s[i]-'0')%4==0)res++;
if(i&&(s[i]-'0'+(s[i-1]-'0')*10)%4==0)res+=i;
}
cout<<res<<endl;
}
signed main()
{
io;
solve();
return 0;
}
4427. 树中节点和
思路:
给出奇数层数的s值,根据前缀和公式 w[i]=s[i]-s[i-1]
,题目要求w[i]最小且为非负,则 s[i-1]应该小于等于
s[i]取最大,s[i-1]==s[i],(i是层数)
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL fpower(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,res;
const int N=1e5+10;
int h[N],e[N],ne[N],idx;
int s[N];
int w[N];
int p[N];
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void solve()
{
cin>>n;
memset(h,-1,sizeof h);
p[1]=1;
for(int i=2;i<=n;i++)
{
int x;
cin>>x;
p[i]=x;
add(x,i);
}
for(int i=1;i<=n;i++)cin>>s[i];
w[1]=s[1];
for(int i=2;i<=n;i++)
{
if(s[i]==-1)
{
s[i]=INF;
for(int j=h[i];~j;j=ne[j])
{
int u=e[j];
s[i]=min(s[i],s[u]);//取下一层的最小值
}
if(h[i]==-1)//叶子结点
{
s[i]=s[p[i]];//父节点
}
}
}
for(int i=2;i<=n;i++)
{
w[i]=s[i]-s[p[i]];
if(w[i]<0)
{
cout<<-1<<endl;
return;
}
}
LL res=0;
for(int i=1;i<=n;i++)res+=w[i];
cout<<res<<endl;
}
signed main()
{
io;
solve();
return 0;
}