国庆欢乐赛3
Done
OI
Start at: 2024-10-5 14:00
3.5
hour(s)
Host:
36
注意freopen与大样例
#include <bits/stdc++.h>
using namespace std;
#define int long long
int x,y,T;
int gcd(int x,int y)
{
if(!y)return x;
return gcd(y,x%y);
}
signed main()
{
freopen("pigeon.in","r",stdin);
freopen("pigeon.out","w",stdout);
cin>>x>>y>>T;
cout<<T/(x*y/gcd(x,y))<<endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
int book[N];
int n;
int a[N];
/*
5
2 3 1 4 5
*/
void dfs(int x)
{
book[x] = 1;
if(book[a[x]]==0){
dfs(a[x]);
}
}
int main()
{
freopen("permutation.in","r",stdin);
freopen("permutation.out","w",stdout);
int T;
cin>>T;
while(T--){
memset(book,0,sizeof(book));
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
int res = 0;
for(int i=1;i<=n;i++){
if(book[a[i]]==0){
res++;
dfs(a[i]);
}
}
cout<<n-res<<endl;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5+5;
int n,m;
struct node{
int v,w;
};
vector<node>e[N];
int u[N],v[N],w[N];
int siz[N],res;
void dfs(int x,int fa,int weight)
{
siz[x] = 1;
for(int i=0;i<e[x].size();i++){
int v = e[x][i].v;
int W = e[x][i].w;
if(v==fa)continue;
dfs(v,x,W);
siz[x]+=siz[v];
}
res+=weight*siz[x]*(n-siz[x]);
}
signed main()
{
freopen("tree.in","r",stdin);
freopen("tree.out","w",stdout);
cin>>n>>m;
for(int i=1;i<n;i++){
cin>>u[i]>>v[i]>>w[i];
e[u[i]].push_back({v[i],w[i]});
e[v[i]].push_back({u[i],w[i]});
}
dfs(1,0,0);
for(int i=1;i<=m;i++){
int x,y;
cin>>x>>y;
int cha = y-w[x];
w[x] = y;
int U = u[x],V=v[x];
if(siz[U]>siz[V])swap(U,V);
res+=cha*siz[U]*(n-siz[U]);
cout<<res<<endl;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e5+5,Mod=1e9+7;
int n,a[N],cnt[500005],f[500005],B[500005],A[500005];
void init()
{
f[0] = 1;
for(int i=1;i<=500000;i++){
f[i] = (f[i-1]+f[i-1])%Mod;
}
}
signed main()
{
freopen("army.in","r",stdin);
freopen("army.out","w",stdout);
init();
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i],cnt[a[i]]++;
//cnt[i]i及i的倍数一共有多少人
for(int i=1;i<=500000;i++){
for(int j=i+i;j<=500000;j+=i){
cnt[i]+=cnt[j];
cnt[i]%=Mod;
}
}
for(int i=1;i<=500000;i++){
if(cnt[i]==0)continue;
B[i] = (cnt[i]*f[cnt[i]-1])%Mod;
}
for(int i=500000;i>=1;i--){
for(int j=i+i;j<=500000;j+=i){
B[i]-=B[j];
B[i]%=Mod;
}
}
int res = 0;
for(int i=2;i<=500000;i++){
res+=B[i]*i%Mod;
res%=Mod;
}
cout<<res<<endl;
return 0;
}
Files
Filename | Size | ||
---|---|---|---|
.DS_Store | 6 KiB | ||
ex_army1.in | 12 Bytes | ||
ex_army1.out | 4 Bytes | ||
ex_army2.in | 31 Bytes | ||
ex_army2.out | 6 Bytes | ||
ex_permutation1.in | 17 Bytes | ||
ex_permutation1.out | 3 Bytes | ||
ex_permutation2.in | 98 Bytes | ||
ex_permutation2.out | 11 Bytes | ||
ex_pigeon1.in | 8 Bytes | ||
ex_pigeon1.out | 3 Bytes | ||
ex_pigeon2.in | 39 Bytes | ||
ex_pigeon2.out | 6 Bytes | ||
ex_tree1.in | 36 Bytes | ||
ex_tree1.out | 8 Bytes | ||
ex_tree2.in | 120 Bytes | ||
ex_tree2.out | 35 Bytes | ||
solution for day3.pdf | 137.9 KiB |
- Status
- Done
- Rule
- OI
- Problem
- 4
- Start at
- 2024-10-5 14:00
- End at
- 2024-10-5 17:30
- Duration
- 3.5 hour(s)
- Host
- Partic.
- 36