1. 819B Mister B and PR Shifts
大意: 给定排列$p$, 定义排列$p$的特征值为$\sum |p_i-i|$, 可以循环右移任意位, 求最小特征值和对应移动次数.
右移过程中维护增加的个数和减少的个数即可.
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <cmath> #include <set> #include <map> #include <queue> #include <string> #include <cstring> #include <bitset> #include <functional> #include <random> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl ‘\n‘ #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<‘,‘;hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<‘0‘||p>‘9‘)p=getchar();while(p>=‘0‘&&p<=‘9‘)x=x*10+p-‘0‘,p=getchar();return x;} //head const int N = 1e6+50; int n, a[N]; int dl[N], dr[N]; int main() { scanf("%d",&n); REP(i,1,n) scanf("%d",a+i); ll ret = 0, ans = 0; int L = 0, R = 0; REP(i,1,n) { ret += abs(a[i]-i); if (a[i]>i) { ++L; --dl[a[i]-i]; ++dr[a[i]-i]; } else if (a[i]<=i) { ++R; if (a[i]!=1) { --dl[n-i+a[i]]; ++dr[n-i+a[i]]; } } } ans = ret; int pos = 0; REP(i,1,n-1) { ret += R-L; R += dr[i]; L += dl[i]; if (a[n-i+1]!=1) --R,++L; ret -= abs(a[n-i+1]-n-1); ret += abs(a[n-i+1]-1); if (ret<ans) pos = i, ans = ret; } printf("%lld %d\n", ans, pos); }
2. 819C Mister B and Beacons on Field
大意: 给定两个平面点$A(m,0),B(0,n)$
- 求$A$移向原点过程中, 有多少个时刻, 存在一个点$C$使得ABC面积为$S$
- $A$在原点, $B$移向原点过程中, 有多少个时刻, 存在一个点$C$使得ABC面积为$S$
对于第一问, 假设$C$坐标为$(x,y)$, $A$返回时坐标为$(t,0)$
那么有$xn+ty=2S+tn, 0\le t\le m$
就等价于求$[0,m]$中有多少个$t$满足$gcd(n,t)|2S$
对于第二问, 假设$B$返回时坐标为$(0,t)$
那么有$xt=2S, 1\le t\le m$
等价于求$[1,n]$中有多少个$t$满足$t|2S$
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <cmath> #include <set> #include <map> #include <queue> #include <string> #include <cstring> #include <bitset> #include <functional> #include <random> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl ‘\n‘ #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<‘,‘;hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<‘0‘||p>‘9‘)p=getchar();while(p>=‘0‘&&p<=‘9‘)x=x*10+p-‘0‘,p=getchar();return x;} //head const int N = 1e6+50; int gpf[N]; vector<pii> B,C,S; ll ans,n,m,s; void solve(vector<pii> &A, int x) { while (x!=1) { int p = gpf[x], cnt = 0; while (x%p==0) x/=p,++cnt; A.pb(pii(p,cnt)); } } void repr(vector<pii> &A) { sort(A.begin(),A.end()); vector<pii> ret; for (auto &t:A) { if (ret.empty()||t.x!=ret.back().x) ret.pb(t); else ret.back().y += t.y; } A = ret; } void init() { int n1,n2,n3,m1,m2,m3,s1,s2,s3; scanf("%d%d%d%d%d%d%d%d%d",&n1,&n2,&n3,&m1,&m2,&m3,&s1,&s2,&s3); B.clear(),C.clear(),S.clear(); n = (ll)n1*n2*n3, m = (ll)m1*m2*m3, s = (ll)s1*s2*s3; solve(B,n1),solve(B,n2),solve(B,n3),repr(B); S.pb(pii(2,1)); solve(S,s1),solve(S,s2),solve(S,s3),repr(S); } void dfs(int d, ll num, int z) { if (!num) return; if (d==C.size()) return ans+=num*z,void(); dfs(d+1,num,z); REP(i,1,C[d].y+1) num/=C[d].x; dfs(d+1,num,-z); } void dfs2(int d, ll num) { if (num>n) return; if (d==S.size()) return ++ans,void(); dfs2(d+1,num); REP(i,1,S[d].y) num*=S[d].x,dfs2(d+1,num); } void work() { init(); int sz = S.size(), now = 0; REP(i,0,sz-1) { while (now<B.size()&&B[now].x<S[i].x) C.pb(pii(B[now++].x,0)); if (now<B.size()&&B[now].x==S[i].x) { if (B[now].y>S[i].y) C.pb(S[i]); ++now; } } while (now<B.size()) C.pb(pii(B[now++].x,0)); ans = 0; dfs(0,m,1),dfs2(0,1); printf("%lld\n", ans); } int main() { gpf[1] = 1; REP(i,1,N-1) if (!gpf[i]) { for(int j=i;j<N;j+=i) gpf[j]=i; } int t; scanf("%d", &t); while (t--) work(); }
原文地址:https://www.cnblogs.com/uid001/p/11614218.html
时间: 2024-10-17 01:28:30