C
C - Divide and Divide (atcoder.jp)
1e17暴力肯定不行
模拟暴力的过程我们发现很多运算是重复的
记忆化一下
cpp
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
#define int long long
typedef pair<int,int> PII;
const int N=1000010;
int a[N];
map<int,int>mp;
int dfs(int now){
if(mp.count(now)) return mp[now];
if(now==1) return 0;
mp[now]=dfs(now/2)+dfs((now+1)/2)+now;
return mp[now];
}
void solve(){
int n;
cin>>n;
cout<<dfs(n)<<endl;
}
signed main(){
int t=1;
//cin>>t;
while(t--){
solve();
}
}
D
https://atcoder.jp/contests/abc340/tasks/abc340_d
cpp
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<array>
using namespace std;
typedef long long LL;
#define int long long
typedef pair<int,int> PII;
const int N=1000010;
int a[N],b[N],c[N];
int dp[N][2];
std::vector<array<int,2>>v[N];
int dist[N];
bool st[N];
void dij(){
priority_queue<array<int,2>,vector<array<int,2>>,greater<array<int,2>>>q;
q.push({dist[1],1});
while(q.size()){
auto t=q.top();
q.pop();
if(st[t[1]]) continue;
st[t[1]]=1;
for(auto c:v[t[1]]){
if(dist[c[0]]>dist[t[1]]+c[1]){
dist[c[0]]=dist[t[1]]+c[1];
q.push({dist[c[0]],c[0]});
}
}
}
}
void solve(){
int n;
cin>>n;
for(int i=2;i<=n;i++){
dist[i]=1e18;
}
for(int i=1;i<n;i++){
cin>>a[i]>>b[i]>>c[i];
v[i].push_back({i+1,a[i]});
v[i].push_back({c[i],b[i]});
}
dij();
cout<<dist[n]<<endl;
}
signed main(){
int t=1;
//cin>>t;
while(t--){
solve();
}
}
E
https://atcoder.jp/contests/abc340/tasks/abc340_e
线段树处理
操作:查询在位置上的值sum,然后修改这个该位置的值为y
然后给整个数组加上sum/n,特殊情况还有剩余的手玩一下模拟即可
cpp
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<array>
using namespace std;
typedef long long LL;
#define int long long
typedef pair<int,int> PII;
const int N=1000010;
int a[N],b[N],c[N];
struct Node
{
int l, r;
int sum,tag;
}tr[N * 4];
void pushup(int u)
{
tr[u].sum=tr[u<<1].sum+tr[u<<1|1].sum;
}
void pushdown(int u)
{
if(tr[u].tag){
tr[u<<1].tag+=tr[u].tag;
tr[u<<1|1].tag+=tr[u].tag;
tr[u<<1].sum+=tr[u].tag;
tr[u<<1|1].sum+=tr[u].tag;
tr[u].tag=0;
}
}
void build(int u, int l, int r)
{
if (l == r) tr[u] = {l, r,a[r],0};
else
{
tr[u] = {l, r,0,0};
int mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);
}
}
void update(int u, int l, int r, int d)
{
if (tr[u].l >= l && tr[u].r <= r)
{
tr[u].sum+=d;
tr[u].tag+=d;
}
else
{
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) update(u << 1, l, r, d);
if (r > mid) update(u << 1 | 1, l, r, d);
pushup(u);
}
}
int query(int u, int l, int r)
{
if (tr[u].l >= l && tr[u].r <= r)
{
return tr[u].sum;
}
else
{
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
int res = 0;
if (l <= mid ) res = query(u << 1, l, r);
if (r > mid) res += query(u << 1 | 1, l, r);
return res;
}
}
void solve(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
build(1,1,n);
for(int i=1;i<=m;i++){
cin>>b[i];
b[i]++;
int sum=query(1,b[i],b[i]);
update(1,b[i],b[i],-sum);
int tmp=sum/n;
update(1,1,n,tmp);
tmp=sum-tmp*n;
if(tmp){
if(tmp<=n-b[i]){
if(b[i]+1<=n)update(1,b[i]+1,b[i]+tmp,1);
}else{
if(b[i]+1<=n)update(1,b[i]+1,n,1);
tmp-=n-b[i];
update(1,1,tmp,1);
}
}
}
for(int i=1;i<=n;i++) cout<<query(1,i,i)<<" ";
}
signed main(){
int t=1;
while(t--){
solve();
}
}
F
根据叉积求面积公式
这道题中x1=0,y1=0,S=1
转化成
现在x2和y2我们已经知道
通过扩展欧几里得我们可以算出
这个公式的x,y的一组解(无解的情况:2%gcd(a,b)!=0)
这时候将x*gcd(a,b),y*gcd(a,b)即可得到答案
cpp
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_set>
using namespace std;
typedef long long LL;
#define int long long
typedef pair<int,int> PII;
const int N=1000010;
int a[N];
int exgcd(int a,int b,int &x,int &y){
if(!b){
x=1,y=0;
return a;
}
int d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
void solve(){
int x,y,a,b;
cin>>x>>y;
//S=1/2*((x2-x1)*(y3-y1)-(y2-y1)*(x3-x1))
//在本题中x1=0 y1=0,2=(已知)x2*y3-(已知)y2*x3
//无解的情况2%gcd(x2,-y2)!=0
int g=exgcd(x,-y,a,b);
if(2%g){
cout<<-1<<endl;
}else{
a*=2/g;
b*=2/g;
cout<<b<<" "<<a<<endl;
}
}
signed main(){
int t=1;
//cin>>t;
while(t--){
solve();
}
}