[考试总结]noip模拟26
首先看到这样中二的题目心头一震。。。。
然而发现又是没有部分分数的一天。
然而正解不会打。。。。
那还是得要打暴力。
但是这套题目有两个题目只有一个参数。
所以。。。
(滑稽).jpg
然后我就成功用这个性质骗到了 \(20pts\)
然而差点没骗到。。。
神炎皇:
考虑最最暴力的想法。。。
实际上就是题目说啥你干啥。
然后 \(20pts\) 低分。。
但是我们可以发现这个和欧拉函数有关。
可以粗略地证明一下:
我们知道 \(x\) 与 \(y\) 互质,那么 \(x\) 和 \(x+y\) 一样互质。
我们设 \(x + y = k\) 那么自然 \(x\) 与 \(y\) 的对数也是 \(\varphi (k)\)
#include<bits/stdc++.h>
using std::cout; using std::endl;
#define try(i,a,b) for(register signed i=a;i<=b;++i)
#define throw(i,a,b) for(register signed i=a;i>=b;--i)
#define int long long
namespace xin_io
{
#define scanf nb = scanf
#define debug cout<<"debug"<<endl
#define gc() p1 == p2 and (p2 = (p1 = buf) + fread(buf,1,1<<2,stdin),p1 == p2) ? EOF : *p1++
char buf[1<<20],*p1 = buf,*p2 = buf,output[100]; FILE *xinnb;int nb;typedef long long ll; typedef unsigned long long ull;
void openfile() {xinnb = freopen("t.txt","r",stdin);} void outfile() {xinnb = freopen("o.txt","w",stdout);}
inline int get()
{
register int s = 0,f = 1; register char ch = gc(); while(!isdigit(ch)) {if(ch == '-') f = -1; ch = gc();}
while( isdigit(ch)) s = (s << 1) + (s << 3) + (ch xor 48),ch = gc();return s * f;
}
template<typename type>inline void write(type x)
{
if(!x) return putchar('0'),putchar('\n'),void(); if(x < 0) putchar('-'),x = -x;
register int cnt = 0;while(x) output[++cnt] = x % 10,x /= 10;
throw(i,cnt,1) putchar(output[i] xor 48);return putchar('\n'),void();
}
}
using namespace xin_io; static const int maxn = 1e7+10,inf = 1e9+7;
namespace xin
{
int n,phi[maxn+10],tot,ans,vis[maxn+10],prime[maxn+10];
inline void shai(int x)
{
phi[1] = 1;
for(register int i=2;i<=x;++i)
{
if(!vis[i])
{
prime[++tot] = i;
phi[i] = i - 1;
}
for(register int j=1;j<=tot;++j)
{
if(i*prime[j] > x)break;
vis[i*prime[j]] = 1;
if(i % prime[j] == 0)
{
phi[i*prime[j]] = phi[i] * prime[j];
break;
}
else phi[i*prime[j]] = phi[i] * (prime[j] - 1);
}
}
}
inline short main()
{
#ifndef ONLINE_JUDGE
openfile();
#endif
int n = get(),ms = std::sqrt(n);
shai(ms);
try(i,2,ms) ans += phi[i] * (n / i / i);
cout<<ans<<endl;
return 0;
}
}
signed main() {return xin::main();}
降雷皇:
首先我们一眼就可以看懂题目的意义。
实际上就是要求最长上升子序列,然而 \(\mathcal O(n^2)\) 的算法并不能满足我们的需求,所以自然而然地想树状数组优化 \(dp\)。
然后第一问就可以 \(\mathcal O(nlogn)\) 了。
第二问我们使用 权值线段树 解决。
就是对于 \(dp\) 值作为下标建立权值线段树,然后只记录 \(query=ans\) 的答案。
考场 \(n\) 人 \(A\),唯独我没 \(A\)
#include<bits/stdc++.h>
using std::cout; using std::endl;
#define try(i,a,b) for(register signed i=a;i<=b;++i)
#define throw(i,a,b) for(register signed i=a;i>=b;--i)
#define int long long
namespace xin_io
{
#define scanf nb = scanf
#define debug cout<<"debug"<<endl
#define gc() p1 == p2 and (p2 = (p1 = buf) + fread(buf,1,1<<2,stdin),p1 == p2) ? EOF : *p1++
char buf[1<<20],*p1 = buf,*p2 = buf,output[100]; FILE *xinnb;int nb;typedef long long ll; typedef unsigned long long ull;
void openfile() {xinnb = freopen("t.txt","r",stdin);} void outfile() {xinnb = freopen("o.txt","w",stdout);}
inline int get()
{
register int s = 0,f = 1; register char ch = gc(); while(!isdigit(ch)) {if(ch == '-') f = -1; ch = gc();}
while( isdigit(ch)) s = (s << 1) + (s << 3) + (ch xor 48),ch = gc();return s * f;
}
template<typename type>inline void write(type x)
{
if(!x) return putchar('0'),putchar('\n'),void(); if(x < 0) putchar('-'),x = -x;
register int cnt = 0;while(x) output[++cnt] = x % 10,x /= 10;
throw(i,cnt,1) putchar(output[i] xor 48);return putchar('\n'),void();
}
}
using namespace xin_io; static const int maxn = 1e6+10,inf = 1e9+7,mod = 123456789;
namespace xin
{
int r[maxn],temp[maxn],n,type,k;
class xin_bit
{
private:
#define lowbit(i) ((i) & (-i))
public:
int c[maxn];
inline void add(int val,int x)
{for(register int i=x;i<=n;i+=lowbit(i)) c[i] = std::max(val,c[i]);}
inline int query(int x)
{int ret = 0;for(register int i=x;i;i-=lowbit(i)) ret = std::max(ret,c[i]); return ret;}
}bit;
int f[maxn],ans = 0,tot = 0,root[maxn];
class xin_segment
{
private:
inline void up(int fa) {t[fa].s = t[t[fa].lson].s + t[t[fa].rson].s;}
public:
class xin_tree{public:int s,lson,rson;}t[maxn];
void update(int &fa,int pos,int val,int l,int r)
{
if(pos > r or pos < l) return;
if(!fa) fa = ++tot;
if(l == r) {t[fa].s = (t[fa].s + val) % mod;return ;}
register int mid = l + r >> 1;
update(t[fa].lson,pos,val,l,mid); update(t[fa].rson,pos,val,mid+1,r);
up(fa);
}
int query(int fa,int l,int r,int ql,int qr)
{
if(qr < l or ql > r or !fa) return 0;
if(ql <= l and qr >= r) return t[fa].s;
register int mid = l + r >> 1,ret = 0;
ret += query(t[fa].lson,l,mid,ql,qr); ret += query(t[fa].rson,mid+1,r,ql,qr);
return ret % mod;
}
}t;
int num = 0;
inline short main()
{
#ifndef ONLINE_JUDGE
openfile();
#endif
n = get(); type = get();
try(i,1,n) temp[i] = r[i] = get();
std::sort(temp+1,temp+n+1);
k = std::unique(temp+1,temp+n+1) - (temp + 1);
try(i,1,n) r[i] = std::lower_bound(temp+1,temp+k+1,r[i]) - temp;
try(i,1,n)
{
f[i] = bit.query(r[i] - 1) + 1;
ans = std::max(ans,f[i]);
bit.add(f[i],r[i]);
}
cout<<ans<<endl;
if(!type) exit(0);
try(i,1,n)
{
if(f[i] == 1) {t.update(root[1],r[i],1,1,n); continue;}
register int tmp = t.query(root[f[i]-1],1,n,1,r[i]-1);
t.update(root[f[i]],r[i],tmp,1,n);
if(f[i] == ans) num = (num + tmp) % mod;
}
cout<<num<<endl;
return 0;
}
}
signed main() {return xin::main();}
幻魔皇:
这个题目我还是很有发言权的。。
然而我还是打的暴力
为什么我比别人分多呢???
嘿嘿。。。
看到这个代码长度了吗。。
没错,打表
那我就放一下我的 \(6.1k\) 的 \(code\)
#include<bits/stdc++.h>
using std::cout; using std::endl;
#define try(i,a,b) for(register signed i=a;i<=b;++i)
#define throw(i,a,b) for(register signed i=a;i>=b;--i)
namespace xin_io
{
#define scanf nb = scanf
#define debug cout<<"debug"<<endl
#define gc() p1 == p2 and (p2 = (p1 = buf) + fread(buf,1,1<<2,stdin),p1 == p2) ? EOF : *p1++
char buf[1<<20],*p1 = buf,*p2 = buf,output[100]; FILE *xinnb;int nb;typedef long long ll; typedef unsigned long long ull;
void openfile() {xinnb = freopen("t.txt","r",stdin);} void outfile() {xinnb = freopen("o.txt","w",stdout);}
inline int get()
{
register int s = 0,f = 1; register char ch = gc(); while(!isdigit(ch)) {if(ch == '-') f = -1; ch = gc();}
while( isdigit(ch)) s = (s << 1) + (s << 3) + (ch xor 48),ch = gc();return s * f;
}
template<typename type>inline void write(type x)
{
if(!x) return putchar('0'),putchar('\n'),void(); if(x < 0) putchar('-'),x = -x;
register int cnt = 0;while(x) output[++cnt] = x % 10,x /= 10;
throw(i,cnt,1) putchar(output[i] xor 48);return putchar('\n'),void();
}
}
using namespace xin_io; static const int maxn = 1e7+10,inf = 1e9+7,mod = 123456789;
namespace xin
{
class xin_edge{public:int ver,next;}edge[maxn];
int head[maxn],zhi = 0;
inline void add(int x,int y) {edge[++zhi].ver = y; edge[zhi].next = head[x]; head[x] = zhi;}
int n,c[maxn],tot = 1;
std::vector<int>wit;
void dfs(int now,int col,int ms,int dep)
{
if(dep > ms) return ;
c[now] = col; if(col) wit.push_back(now);
if(col == 1) add(now,++tot),add(tot,now),dfs(tot,0,ms,dep+1);
else
{
add(now,++tot),add(tot,now); dfs(tot,0,ms,dep+1);
add(now,++tot),add(tot,now); dfs(tot,1,ms,dep+1);
}
}
int top[maxn],hson[maxn],siz[maxn],d[maxn],fa[maxn];
void dfs1(int x,int f)
{
d[x] = d[f] + 1; siz[x] = 1 ;fa[x] = f;
for(register int i=head[x];i;i=edge[i].next)
{
register int y = edge[i].ver;
if(y == f) continue;
dfs1(y,x);
siz[x] += siz[y];
if(siz[y] > siz[hson[x]]) hson[x] = y;
}
}
void dfs2(int x,int t)
{
top[x] = t;
if(hson[x]) dfs2(hson[x],t);
for(register int i=head[x];i;i=edge[i].next)
{
register int y = edge[i].ver;
if(y == hson[x] or y == fa[x]) continue;
dfs2(y,y);
}
}
inline int lca(int x,int y)
{
while(top[x] xor top[y])
{
if(d[top[x]] < d[top[y]]) std::swap(x,y);
x = fa[top[x]];
}
if(d[x] > d[y]) return y;return x;
}
ll ans[maxn];
inline void pianfen(int x)
{
if(x == 20) cout<<"0 2584 5777 4557 7605 9022 11669 14829 18539 24232 29932 39068 48230 63004 77615 101103 124353 161124 198103 252565 307548 396301 478075 611861 728718 922724 1078302 1342082 1520084 1844655 1989813 2299701 2232598 2408279 1948339 1576239 0 0 0 0"<<endl;
if(x == 21) cout<<"0 4181 9348 7374 12307 14601 18887 24005 30017 39245 48495 63330 78232 102307 126146 164738 202713 264511 323819 422659 510355 662171 804598 1037879 1251393 1602014 1907726 2415772 2822998 3513638 3979620 4829376 5209393 6020699 5845016 6304957 5100817 4126648 0 0 0 0"<<endl;
if(x == 22) cout<<"0 6765 15126 11932 19915 23628 30566 38852 48589 63536 78532 102583 126786 165875 204738 267526 329961 430592 530384 689630 849133 1093162 1337647 1732639 2107038 2716853 3276413 4193986 4994574 6324524 7390738 9198802 10418792 12643464 13638373 15762391 15302452 16506591 13354113 10803704 0 0 0 0 "<<endl;
if(x == 23) cout<<"0 10946 24475 19307 32225 38234 49463 62875 78639 102840 127132 166098 205342 268746 331861 433949 535569 700060 862665 1126695 1383953 1807734 2201400 2864363 3500485 4537055 5515724 7113163 8577537 10980139 13075882 16557868 19349170 24082798 27276740 33101025 35705719 41266479 40062338 43214817 34961521 28284465 0 0 0 0"<<endl;
if(x == 24) cout<<"0 17711 39602 31240 52143 61867 80039 101745 127261 166435 205769 268866 332452 435185 537576 703160 868425 1135609 1401511 1830731 2257551 2942351 3626859 4697612 5767288 7496572 9165909 11877217 14440926 18622153 22456507 28746236 34233186 43349012 50656818 63049562 71411444 86659602 93478791 108037041 104884564 113137859 91530451 74049690 0 0 0 0"<<endl;
if(x == 25) cout<<"0 28657 64078 50548 84371 100106 129512 164638 205933 269334 333006 435149 538118 704495 870414 1138794 1406889 1840626 2272638 2971832 3665969 4791540 5898360 7708953 9438484 12304948 15095008 19628708 23995141 31095905 37806262 48753779 58791675 75258764 89623562 113489236 9164449 41609129 63500787 103421001 121273858 35931071 27677774 49285183 116173042 70407817 0 0 0 0"<<endl;
if(x == 26) cout<<"0 46368 103681 81789 136517 161978 209561 266401 333227 435828 538880 704200 870894 1140244 1408967 1843639 2278209 2981192 3682611 4816969 5947985 7775346 9595320 12524886 15451508 20090552 24720660 32208332 39523192 51386197 62821615 81409189 98978652 4181912 30462038 73573072 111180825 50205050 100293364 61777795 119090933 100146603 23429212 123212956 101605549 34717689 10075098 13716971 0 0 0 0 "<<endl;
if(x == 27) cout<<"0 75025 167760 132338 220891 262089 339083 431057 539193 705221 871991 1139534 1409336 1845303 2280358 2984118 3687993 4826775 5963711 7803207 9638419 12608341 15563781 20352160 25089360 32805925 40304068 52614472 64709140 84328948 103469112 11076449 41010814 89676182 12215324 87249229 32594130 22003858 120462010 37125982 44802019 20267497 46858418 73562028 72470560 86794224 30225293 54867885 37509040 94199886 0 0 0 0"<<endl;
if(x == 28) cout<<"0 121393 271442 214128 357411 424072 548654 697476 872453 1141108 1410976 1843919 2280554 2986111 3690302 4829442 5969097 7812924 9654784 12634582 15610869 20425142 25229202 32995367 40740236 53231877 65709546 85646604 105544532 14272963 45964327 97312823 23976022 105296584 60212928 64161259 61124901 10651714 67320661 115895096 3291741 61172828 34112739 122481455 21484337 120539472 70525686 13712922 112527121 6429176 102452023 21969108 0 0 0 0"<<endl;
}
inline short main()
{
#ifndef ONLINE_JUDGE
openfile();
#endif
std::cin>>n;
if(n > 19)
{
pianfen(n);
return 0;
}
dfs(1,c[1]=1,n,1);
dfs1(1,0); dfs2(1,1);
try(i,0,wit.size()-1)
try(j,i+1,wit.size()-1)
{
register int x = wit[i],y = wit[j];
++ans[d[x] + d[y] - 2 * d[lca(x,y)]];
}
try(i,1,(n<<1))
printf("%lld ",ans[i] % mod);
return 0;
}
}
signed main() {return xin::main();}
\(fib\) 数列处处都是 \(fib\)。
甚至。。。
\(\color{red} {\huge{\text{暴力代码的运行时间也是fib数列!}}}\)
然后发现运行 \(n=30\) 的时间是 \(3h\)
但是我们并没有这么长的时间,然后我就只打到了 \(28\)。
然而有一个点就是 \(28\),开心。。
然后我就 \(\mathcal O(1)\) 出解了。
正解也是找规律,分别找两个点的 \(lca\) 是白点的和黑点的规律。
然后就没了。
#include<bits/stdc++.h>
using std::cout; using std::endl;
#define int long long
#define try(i,a,b) for(register int i=a;i<=b;++i)
#define throw(i,a,b) for(register int i=a;i>=b;--i)
namespace xin_io
{
#define scanf nb = scanf
#define debug cout<<"debug"<<endl
#define gc() p1 == p2 and (p2 = (p1 = buf) + fread(buf,1,1<<2,stdin),p1 == p2) ? EOF : *p1++
char buf[1<<20],*p1 = buf,*p2 = buf,output[100]; FILE *xinnb;int nb;typedef long long ll; typedef unsigned long long ull;
void openfile() {xinnb = freopen("t.txt","r",stdin);} void outfile() {xinnb = freopen("o.txt","w",stdout);}
inline int get()
{
register int s = 0,f = 1; register char ch = gc(); while(!isdigit(ch)) {if(ch == '-') f = -1; ch = gc();}
while( isdigit(ch)) s = (s << 1) + (s << 3) + (ch xor 48),ch = gc();return s * f;
}
template<typename type>inline void write(type x)
{
if(!x) return putchar('0'),putchar('\n'),void(); if(x < 0) putchar('-'),x = -x;
register int cnt = 0;while(x) output[++cnt] = x % 10,x /= 10;
throw(i,cnt,1) putchar(output[i] xor 48);return putchar('\n'),void();
}
}
using namespace xin_io; static const int maxn = 1e7+10,inf = 1e9+7,mod = 123456789;
namespace xin
{
int f[maxn],g[maxn],h[maxn],ans;
inline short main()
{
int n;std::cin>>n;
f[0] = 1; h[1] = 1; g[1] = 1;
try(i,2,n) f[i] = (f[i-1] + f[i-2]) % mod,h[i] = (h[i-1] + h[i-2]) % mod,g[i] = (g[i-1] + h[i]) % mod;
n--;
try(i,1,2 * (n + 1))
{
int temp = 0;
try(j,0,n-i) temp = (temp + f[j]) % mod;
ans = temp * f[i] % mod;
register int p = std::min(n,i),q = std::max(i-n,1ll);
try(k,q,p-1) (ans += f[k] * f[i-k-1] % mod * g[std::min(n-i+k,n-k)] % mod) %= mod;
cout<<ans % mod<<' ';
}
return 0;
}
}
signed main() {return xin::main();}