Yale-Yale Study 10.4

Source: Internet
Author: User
Tags bitset

Yale-Yale 10.4 Morning examination of the situation T1

Inexplicably 20 points.

The examination room in a second glance off the topic: this is not a and check bundle + fast power?

Then happy heart this point if there is a qualitative factor (fa\) is pointing to this qualitative factor, and each number has multiple qualitative factors ...

A number of qualitative factors in the mutual finger \ (fa\) when the point of confusion ....

When you look at the shot, and then use \ (1\) for more than an hour to adjust the code, and finally I do not know what this thing is doing, it is cold.

T2

Wrote a violent enumeration, expecting \ (20\) actual \ (20\)

T3

After seeing the result: how can this problem explode \ (long long\) ???

And then the title ... The author said that the second part of the inside \ (a_i\) the value range given wrong, less than three zeros ...

Cool,\ (30\) hang Up

Title and examination Room Code T1

#include <cstdio> #include <cstring>inline int read () {int n=0,w=1;register char c=getchar ();    while (c< ' 0 ' | | c> ' 9 ') {if (c== '-') W=-1;c=getchar ();}    while (c>= ' 0 ' && c<= ' 9 ') n=n*10+c-' 0 ', C=getchar (); return n*w;} const int N=1e5+1,mod=1e9+7;int n,tot,a[n],prime[n],head[n*10];bool notprime[n],vis[n];struct Edge{int v,nxt;} edge[n<<1];inline void Add (int u,int v) {edge[++tot]= (edge) {V,head[u]};head[u]=tot;}        inline void Get_prime () {for (int i=2;i<n;++i) {if (!notprime[i]) prime[++tot]=i;            for (int j=1;i*prime[j]<=n && j<=tot;++j) {notprime[i*prime[j]]=true;        if (i%prime[j] = = 0) break;    }}}inline void Work (int. now) {int x=1e5,id=now;    Now=a[now];        while (now^1) {++x;            if (now%prime[(int) (X-1E5)]==0) {while (now%prime[x]==0) now/=x;        Add (id,x); }}}void dfs (int now) {Vis[noW]=true; for (int i=head[now];i;i=edge[i].nxt) DFS (EDGE[I].V);}    inline int ksm (long long x,int b) {long long res=1;    for (; b;b>>=1,x=x*x%mod) if (b&1) Res=res*x%mod; return res;}    int main () {freopen ("x.in", "R", stdin);    Freopen ("X.out", "w", stdout);    int T=read ();    Get_prime ();    tot=0;        while (t--) {memset (vis,false,sizeof vis);        memset (head,0,sizeof head);        tot=0;        N=read ();            for (int i=1;i<=n;++i) {a[i]=read ();        Work (i);        } tot=0;                for (int i=1;i<=n;++i) if (!vis[i]) {++tot;            DFS (i);    } printf ("%d\n", (KSM (2,tot) -2+mod)%mod);    } fclose (stdin), fclose (stdout); return 0;}
T2

/* * What's the problem? * AO original D is length ah .... * * That's not ... The violence is good. * String+map Water pitch */#include <string> #include <cstdio> #include <map>inline int read () {int n=0,    W=1;register Char C=getchar (); while (c< ' 0 ' | |    C> ' 9 ') {if (c== '-') W=-1;c=getchar ();}    while (c>= ' 0 ' &&c<= ' 9 ') n=n*10+c-' 0 ', C=getchar (); return n*w;} const int N=91;int n,m,d,tot,ans,head[n];std::string s;std::map<std::string,int> mp;struct Edge{int v,w,nxt;} edge[n* (N-1) *2];inline void Add (int u,int v,int W) {edge[++tot]= (edge) {V,w,head[u]};head[u]=tot;}        void Dfs (int now,int step) {if (Step==d) {if (!mp[s]) Mp[s]=++ans;    return;        } for (int i=head[now];i;i=edge[i].nxt) {s.push_back (edge[i].w+ ' 0 ');        DFS (EDGE[I].V,STEP+1);    S.erase (S.end ()-1);    }}int Main () {freopen ("y.in", "R", stdin);    Freopen ("Y.out", "w", stdout);    N=read (), M=read (), D=read ();        for (int u,v,w,i=1;i<=m;++i) {u=read (), V=read (), W=read (); Add (U,v,w), add (V,U,W);    } dfs (1,0);    printf ("%d", ans);    Fclose (stdin); fclose (stdout); return 0;}
T3

/* * Next to my dalao with an hour to cut the problem, and then everywhere said: "Wow T3 good water i greedy can 100 ah * Then someone asked him:" How much complexity of your * he: 100 ah * that person:??? 100 What is the complexity of Ah, I asked is the complexity of * he: Aoao ... Let me see. O (N*Q) 's * that person: ... You go and look at the data range. * He:???    * * * * * * * * * * * * * * * * * HHHHH A simulation * * * #include <cstdio>inline int read () {int n=0,w=1;register char c=getchar (); while (c< ' 0 ' | |    C> ' 9 ') {if (c== '-') W=-1;c=getchar ();}    while (c>= ' 0 ' &&c<= ' 9 ') n=n*10+c-' 0 ', C=getchar (); return n*w;}    const int N=1e5+1;int n,q,x[n];int Main () {freopen ("z.in", "R", stdin);    Freopen ("Z.out", "w", stdout);    N=read (), Q=read ();    for (int i=1;i<=n;++i) x[i]=read ();    int Len,l,r,ans;        while (q--) {len=read ();        Ans=l=0,r=len;                for (int i=1;i<=n;++i) {if (x[i]>r) {ans+=x[i]-r;                L+=x[i]-r;            R=x[i];                    } else if (x[i]<l) {ans+=l-x[i];                    R-=l-x[i];                L=x[i];    }        }    printf ("%d\n", ans);    } fclose (stdin); fclose (stdout); return 0;}
Positive solutions and Code T1

For all \ (Gcd\ne 1\) Two digital connections, the last statistic of the number of Unicom blocks (assuming \ (cnt\)), then the answer is \ (2^{cnt}-2\)

#include <bits/stdc++.h>using namespace Std;const int Maxn=1e5+10,maxa=1e6+10,mod=1e9+7;int T,n,last[maxa],ans ; bool vis[maxn];vector<int> g[maxn];int pcnt,prime[maxa],minp[maxa];bool prm[maxa];inline void init () {for (int i            =2;i<maxa;++i) {if (!prm[i]) {prime[++pcnt]=i;        Minp[i]=i;            } for (int j=1;j<=pcnt&&i*prime[j]<maxa;++j) {prm[i*prime[j]]=true;            MINP[I*PRIME[J]]=PRIME[J];        if (i%prime[j]==0) break;    }}}void dfs (int pos) {vis[pos]=true; for (int i=0;i<g[pos].size (); ++i) if (!vis[g[pos][i]]) DFS (g[pos][i]);}    int main () {freopen ("x.in", "R", stdin);    Freopen ("X.out", "w", stdout);    Init ();    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        for (int i=1;i<=pcnt;++i) last[prime[i]]=0;            for (int i=1,x;i<=n;++i) {vis[i]=false;            G[i].clear ();       scanf ("%d", &x);     while (x>1) {int fac=minp[x];                while (x%fac==0) X/=FAC;                    if (Last[fac]) {g[i].push_back (LAST[FAC]);                G[last[fac]].push_back (i);            } last[fac]=i;        }} Ans=1;        for (int i=1;i<=n;++i) if (!vis[i]) Ans=ans*2%mod,dfs (i);    printf ("%d\n", (ans+mod-2)%mod); } return 0;}
T2

\ (f[i][j][mask]\) Indicates whether there is a path represented as \ (mask\) from \ (i\) to end of \ (j\)

But obviously the complexity isn't good enough.

Consider the meet in the Middle , and for each possible path, enumerate the positions in the middle to judge

\ (O (2^{\frac{d}{2}}\times n\times (n+m)) +2^d\times n\)

Can be optimized with \ (bitset\) , can divide the whole by \ (64\), thus can solve \ (n\) bigger problem

#include <bits/stdc++.h>using namespace Std;const int Maxn=90+10,maxmask=1<<20/2+1;int N,m,d,d1,d2,ans;    bitset<maxn> G0[maxn],g1[maxn],dp[maxmask],f[maxmask];int Main () {freopen ("y.in", "R", stdin);    Freopen ("Y.out", "w", stdout);    scanf ("%d%d%d", &n,&m,&d);        for (int i=1,u,v,c;i<=m;++i) {scanf ("%d%d%d", &u,&v,&c);        if (c) g1[u][v]=g1[v][u]=true;    else g0[u][v]=g0[v][u]=true;    } d2=d/2;d1=d-d2;        for (int. u=n;u;--u) {for (int i=0;i<maxmask;++i) dp[i].reset ();        Dp[1][u]=true; for (int x=1;x<1<<d1;++x) for (int v=1;v<=n;++v) if (Dp[x][v]) {dp[x                    <<1]|=g0[v];                dp[x<<1|1]|=g1[v];    } for (int x=0;x<1<<d1;++x) f[x][u]=dp[1<<d1|x].any (); } for (int i=0;i<1<<d1;++i) for (int j=0;j<1<<d2;++j) if (dp[1<<D2|j]&f[i]). any ()) ++ans;    printf ("%d\n", ans); return 0;}
T3

If there is a \ (x_i\) to make \ (x_{i-1}=x_i\) or \ (x_{i-1}\lt x_i\lt x_{i+1}\), then it can be deleted (when the last \ (x\) is completed completed the task at the same time)

#include <bits/stdc++.h>using namespace Std;typedef long long ll;const int maxn=1e5+10;int n,m;ll TOT,ANS[MAXN]; vector<int> x;vector<pair<int,int> > a;map<int,int> mp;inline ll Calc (ll K) {if (!mp.empty () &am    P;&mp.begin ()->second<0) return tot-(Mp.size ()-1) *k; else return tot-mp.size () *k;} inline void Solve () {priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<    int,int> > > q;    int t=0;        for (int i=0;i<x.size (); ++i) {tot+=abs (x[i]);        Mp[i]=x[i];    Q.push (Make_pair (ABS (X[i]), i));        } while (!q.empty ()) {int id=q.top (). Second,tmp=q.top (). First;q.pop ();        Map<int,int>::iterator p=mp.lower_bound (ID); if (P==mp.end () | | | p->first!=id| |        ABS (P->second)!=tmp) continue;        while (T<a.size () &&abs (P->second) >a[t].first) Ans[a[t].second]=calc (A[t].first), ++t; if (P!=mp.begin ()) if (P!=prev (Mp.end ())) {tmp=p->second,tot-=abs (P->second);                Tmp+=prev (P)->second,tot-=abs (prev (p)->second);                Tmp+=next (P)->second,tot-=abs (Next (p)->second);                Mp.erase (prev (p));                Mp.erase (Next (p));                P->second=tmp,tot+=abs (TMP);            Q.push (Make_pair (TMP), id));                } else{Tot-=abs (P->second);            Mp.erase (P); } else if (p->second>0) if (P!=prev (Mp.end ())) {Tmp=p->second,tot-=abs (P->secon                D);                Tmp+=next (P)->second,tot-=abs (Next (p)->second);                Mp.erase (Next (p));                    if (TMP) {p->second=tmp,tot+=abs (TMP);                Q.push (Make_pair (TMP), id));            } else Mp.erase (p);                } else{Tot-=abs (P->second); Mp.erase(p); }} while (T<a.size ()) Ans[a[t].second]=calc (A[t].first), ++t;}    int main () {freopen ("z.in", "R", stdin);    Freopen ("Z.out", "w", stdout);    scanf ("%d%d", &n,&m);        for (int i=0,p,last=0;i<n;++i) {scanf ("%d", &p);        if (p==last) continue; if (!x.empty () && (X.back () <0&&p<last| |        X.back () >0&&p>last)) X.back () +=p-last;        else X.push_back (p-last);    last=p;        } for (int i=0,l;i<m;++i) {scanf ("%d", &l);    A.push_back (Make_pair (l,i));    } sort (A.begin (), A.end ());    Solve ();    for (int i=0;i<m;++i) printf ("%lld\n", Ans[i]); return 0;}
Afternoon Lectures: Figure Example I

A \ (n\) point \ (m\) edge of the undirected graph from the (1\) point start \ (bfs\), there may be a lot of \ (bfs\) order, Depends on the access order of the out edge. Now give a \ (1\) to \ (n\) to determine if it might be a \ (bfs\) order

\ (N,m\le 2\times 10^5\)

Solution:

First assume that the given \ (bfs\) sequence is legal, so that each node's weight is the position in the given sequence

Then from the \ (1\) point start \ (bfs\), the outbound access order according to the weights from small to large access

Finally, the bfs\ ( bfs\) order is compared with the given sequence, and if it is identical, the sequence is legal.

Example Two

A \ (n\) point \ (m\) edge of the undirected connected graph each point has a weight, now requires a weight for each edge, to meet the weight of each point is equal to all the sum of the connected Benquan, the weight value can be negative.

Solution:

If the figure is a tree, then the scheme is the only one, the direct sentence can be.
Otherwise, you can make a tree, run it again, regardless of the other side.
At this point the root node may not yet satisfy the condition.
At this point consider the other side, a non-tree edge will form a ring.
If it is a even ring, no matter how this non-tree edge changes, it will not affect the root node.
If it is a singular ring, then if the non-tree edge to increase or decrease the weight, the root node will occur 2 of the weight change
Of.
Then we can.

Example Three

Given a \ (n\) point \ (m\) edge of the weighted graph, for an edge of \ (w\) edge, passing will get the proceeds of \ (w\) , then \ (W=\lfloor \ frac{w}{2}\rfloor\)

Ask how much you can gain from 1\

Solution:

All sides of a strong connected component must be able to go to the end.
So after the indentation \ (dp\) can

Example Four

There are \ (m\) individuals, \ (n\) Chair, the i\ person can only sit on the first ( u_i\) or the first (v_i\) Chair. Ask how many options are available to meet no one sitting on the same chair.

Solution:

Take a chair as a point, a person as an edge, into a picture.
Each connected block can be considered separately.
Suppose that there are \ ( v\) points in a connected block, \ (e\) , because of connectivity, there is \ (v?1\le e\), and if \ (e\gt v\) There is no solution, so \ (e\) only \ (v?1\) and \ (v\) two values are taken.
If \ (e=v?1\), then the connectivity block has a \ (v\) scenario: Consider enumerating each point is not put, the other points can be uniquely determined.
If \ (e=v\) and the ring length \ (\gt 1\), then the connected block has a \ (2\) scheme: Consider an edge on the ring, the edge of the method determined after the other can be uniquely determined.

Example Five

Given a \ (v\) point \ (e\) edge of the weighted graph, on the graph has \ (n\) Individuals, the first \ (i\) personal located in the point \ (x_i\), The time a person needs to spend this side of the Benquan through an edge. Now everyone is free to go. Ask for the shortest amount of time to meet the number of people after the end of the node \ (\ge m\)

\ (N,v\le 500\)

Solution:

First \ (floyd\) preprocessing the distance between 22.
Then you can answer the two points. After the two answers, each person will be able to go to the point of the edge.
It can be found that the legal condition is the maximum number of matches \ (\ge m\).
Run a binary map to match it.

Yale-Yale Study 10.4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.