A:2016
2016
Given a 2×2 matrix
find An where A1=A,An=A×An−1. As the result may be large, you are going to find only the remainder after division by 7.
Special Note: The problem is intended to be easy. Feel free to think why the problem is called 2016
if you either:
- find it hard to solve;
- or, solved all the other problems easily.
Input
The input contains at most 40 sets. For each set:
The first line contains an integer n (1≤n<10100000).
The second line contains 2 integers a11,a12.
The third line contains 2 integers a21,a22.
(0≤aij<7, (a11a22−a12a21) is not a multiple of 7)
Output
For each set, a 2×2 matrix denotes the remainder of An after division by 7.
Sample Input
2 1 1 1 2 2016 1 1 1 2
Sample Output
2 3 3 5 1 0 0 1
思路:大数取模+矩阵乘法;‘
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long //#define mod 1000000007 #define pi (4*atan(1.0)) const int N=1e5+10,M=1e6+10,inf=1e9+10; char a[100010]; struct is { int a[10][10]; }; is juzhenmul(is a,is b) { int i,t,j; is ans; memset(ans.a,0,sizeof(ans.a)); for(i=1;i<=2;i++) for(t=1;t<=2;t++) for(j=1;j<=2;j++) { ans.a[i][t]+=(a.a[i][j]*b.a[j][t]); ans.a[i][t]%=7; } return ans; } int main() { int x,y,z,i,t; while(~scanf("%s",a)) { is ab; scanf("%d%d%d%d",&ab.a[1][1],&ab.a[1][2],&ab.a[2][1],&ab.a[2][2]); x=strlen(a); int flag=0; for(i=0;i<x;i++) { flag*=10; flag+=(a[i]-'0')%2016; flag%=2016; } is ans; memset(ans.a,0,sizeof(ans.a)); ans.a[1][1]=1; ans.a[2][2]=1; while(flag--) ans=juzhenmul(ans,ab); printf("%d %d\n%d %d\n",ans.a[1][1],ans.a[1][2],ans.a[2][1],ans.a[2][2]); } return 0; }
C :
Hamiltonian Path
In ICPCCamp, there are n cities and m directed roads between cities. The i-th road going from the ai-th city to the bi-th city is ci kilometers long. For each pair of cities (u,v), there can be more than one roads from u to v.
Bobo wants to make big news by solving the famous Hamiltonian Path problem. That is, he would like to visit all the n cities one by one so that the total distance travelled is minimized.
Formally, Bobo likes to find n distinct integers p1,p2,…,pn to minimize w(p1,p2)+w(p2,p3)+⋯+w(pn−1,pn) where w(x,y) is the length of road from the x-th city to the y-th city.
Input
The input contains at most 30 sets. For each set:
The first line contains 2 integers n,m (2≤n≤105,0≤m≤105).
The i-th of the following m lines contains 3 integers ai,bi,ci (1≤ai<bi≤n,1≤ci≤104).
Output
For each set, an integer denotes the minimum total distance. If there exists no plan, output -1
instead.
Sample Input
3 3 1 2 1 1 3 1 2 3 1 3 2 1 2 1 1 3 2
Sample Output
2 -1
思路:只能1-2-3-4-。。。-n;1≤ai<bi≤n。。。。
#include<bits/stdc++.h> using namespace std; #define ll long long //#define mod 1000000007 #define pi (4*atan(1.0)) const int N=1e5+10,M=1e6+10,inf=1e9+10; int minn[N]; int main() { int x,y,z,i,t; while(~scanf("%d%d",&x,&y)) { for(i=1;i<=x;i++) minn[i]=inf; for(i=0;i<y;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); if(u==v-1) minn[u]=min(minn[u],w); } int flag=0; ll ans=0; for(i=1;i<x;i++) { if(minn[i]==inf) { flag=1; break; } ans+=minn[i]; } if(flag) printf("-1\n"); else printf("%lld\n",ans); } return 0; }
D:
Heartstone
Bobo is playing Heartstone. There are n minions in the battlefield. The i-th minion has hi hit points (HP).
Bobo uses two kinds of magic. The one is Arcane Shot and the other is Frostbolt. Arcane Shot can deal two points damage to a minion (that is to decrease the minion's HP by two), while Frostbolt can deal three points damage. A minion is killed when its HP is less or equal to zero.
Let f(a) be the minimum number of Frostbolt(s) required to kill all minions, if no more than a Arcane Shot(s) are used. Bobo would like to find out f(0)+f(1)+⋯+f(m) modulo (109+7).
Input
The input contains at most 30 sets. For each set:
The first line contains 2 integers n,m (1≤n≤105,0≤m≤105).
The second line contains n integers h1,h2,…,hn (1≤hi≤104).
Output
For each set, an integer denotes f(0)+f(1)+⋯+f(m) modulo (109+7).
Sample Input
3 2 1 2 3 3 2 2 2 2
Sample Output
6 6
#include<bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define pi (4*atan(1.0)) const int N=1e5+10,M=1e6+10,inf=1e9+10; struct is { int x; bool friend operator <(is a,is b) { if(a.x%3==b.x%3) return a.x<b.x; return a.x%3<b.x%3; } }; int check(int x) { int num=0; if(x%3!=0) num++; num+=x/3; return num; } int main() { int x,y,z,i,t; while(~scanf("%d%d",&x,&y)) { priority_queue<is>q; int sum=0; for(i=0;i<x;i++) { is a; scanf("%d",&a.x); q.push(a); sum+=check(a.x); } ll ans=sum; while(y--&&!q.empty()) { is b=q.top(); q.pop(); sum-=check(b.x); b.x-=2; if(b.x>0) { sum+=check(b.x); q.push(b); } ans+=sum; ans%=mod; } printf("%lld\n",ans%mod); } return 0; }
G:
Rolling Variance
Bobo learnt that the variance of a sequence a1,a2,…,an is
where
Bobo has a sequence a1,a2,…,an, and he would like to find the variance of each consecutive subsequences of length m. Formally, the i-th (1≤i≤n−m+1) rolling variance ri is the variance of sequence {ai,ai+1,…,ai+m−1}.
Input
The input contains at most 30 sets. For each set:
The first line contains 2 integers n,m (2≤m≤n≤105).
The second line contains n integers a1,a2,…,an (|ai|≤100).
Output
For each set, (n−m+1) lines with floating numbers r1,r2,…,rn−m+1.
Your answer will be considered correct if its absolute or relative error does not exceed 10−4.
Sample Input
3 2 1 3 2 5 3 1 3 2 4 5
Sample Output
1.41421356 0.70710678 1.00000000 1.00000000 1.52752523
思路:前缀和;
#include<bits/stdc++.h> using namespace std; #define ll long long //#define mod 1000000007 #define pi (4*atan(1.0)) const int N=1e5+10,M=1e6+10,inf=1e9+10; double a[N],sum[N],sum2[N]; int main() { int x,y,z,i,t; while(~scanf("%d%d",&x,&y)) { sum[0]=0.0;sum2[0]=0.0; for(i=1;i<=x;i++) scanf("%lf",&a[i]),sum[i]=0.0,sum2[i]=0.0; sum[1]=a[1]; sum2[1]=a[1]*a[1]; for(i=2;i<=x;i++) sum[i]=sum[i-1]+a[i],sum2[i]=sum2[i-1]+a[i]*a[i]; double ans=0; for(i=1,t=y;t<=x;t++,i++) { double ave=(sum[t]-sum[i-1])/y; double ping=(sum2[t]-sum2[i-1]); double sumt=(sum[t]-sum[i-1]); ans=sqrt((ping-2.0*ave*sumt+y*ave*ave)/(y-1)); printf("%f\n",ans); } } return 0; }
H:
Super Fast Fourier Transform
Bobo has two sequences of integers {a1,a2,…,an} and {b1,b2,…,bm}. He would like to find
Note that ⌊x⌋ denotes the maximum integer does not exceed x, and |x| denotes the absolute value of x.
Input
The input contains at most 30 sets. For each set:
The first line contains 2 integers n,m (1≤n,m≤105).
The second line contains n integers a1,a2,…,an.
The thrid line contains m integers b1,b2,…,bm.
(ai,bi≥0,a1+a2+⋯+an,b1+b2+…,bm≤106)
Output
For each set, an integer denotes the sum.
Sample Input
1 2 1 2 3 2 3 1 2 3 4 5
Sample Output
2 7
思路:不同的数最多1000个,c++11交;
#include<bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define pi (4*atan(1.0)) const int N=1e5+10,M=1e6+10,inf=1e9+10; int flaga[M],flagb[M]; int a[N],b[N]; ll getnum(int x,int y) { return (ll)sqrt(abs(x-y)); } int main() { int x,y,z,i,t; while(~scanf("%d%d",&x,&y)) { int lena=0,lenb=0; memset(flaga,0,sizeof(flaga)); memset(flagb,0,sizeof(flagb)); for(i=0;i<x;i++) { scanf("%d",&z); if(!flaga[z]) a[lena++]=z; flaga[z]++; } for(i=0;i<y;i++) { scanf("%d",&z); if(!flagb[z]) b[lenb++]=z; flagb[z]++; } ll ans=0; for(i=0;i<lena;i++) for(t=0;t<lenb;t++) { ans+=(ll)(flaga[a[i]]*flagb[b[t]]*getnum(a[i],b[t])); } printf("%lld\n",ans); } return 0; }
J:
Defense Tower
In ICPCCamp, there are n cities and (n−1) (bidirectional) roads between cities. The i-th road is between the ai-th and bi-th cities. It is guaranteed that cities are connected.
In the i-th city, there is a defense tower with power pi. The tower protects all cities with a road directly connected to city i. However, the tower in city i does not protect city i itself.
Bobo would like to destroy all defense towers. When he tries to destroy the tower in city i, any not-destroyed tower protecting city i will deal damage whose value equals to its power to Bobo.
Find out the minimum total damage Bobo will receive if he chooses the order to destroy the towers optimally.
Input
The input contains at most 30 sets. For each set:
The first line contains an integer n (1≤n≤105).
The second line contains n integers p1,p2,…,pn (1≤pi≤104).
The i-th of the last (n−1) lines contains 2 integers ai,bi (1≤ai,bi≤n).
Output
For each set, an integer denotes the minimum total damage.
Sample Input
3 1 2 3 1 2 2 3 3 1 100 1 1 2 2 3
Sample Output
3 2
思路,每次取小的;
#include<bits/stdc++.h> using namespace std; #define ll long long //#define mod 1000000007 #define pi (4*atan(1.0)) const int N=1e5+10,M=1e6+10,inf=1e9+10; int a[N]; int main() { int x,y,z,i,t; while(~scanf("%d",&x)) { for(i=1;i<=x;i++) scanf("%d",&a[i]); ll ans=0; for(i=1;i<x;i++) { int u,v; scanf("%d%d",&u,&v); ans+=min(a[u],a[v]); } printf("%d\n",ans); } return 0; }