原文:http://blog.csdn.net/skyfisherman/article/details/8477718
1. Little Elephant and Chess
这道题一开始想复杂了,因为看漏了条件,每一行都是8个……直接输出就可以了……
- # include <cstdio>
- # define rep(i,n) for(int i=0;i<n;i++)
- char a[10]; bool flag=true;
- int main(){
- rep(i,8){scanf("%s",a); rep(j,7)if(a[j]==a[j+1])flag=false;}
- puts(flag?"YES":"NO");
- }
2. Little Elephant and Magic Square
这道题目很简单,sum=给出的数的和/2。
- # include <iostream>
- # include <cstdio>
- using namespace std;
- int a[3][3],s,t;
- int main()
- {
- for(int i=0;i<3;i++)
- for(int j=0;j<3;j++)
- scanf("%d",&a[i][j]),
- s+=a[i][j];
- s=s>>1;
- for(int i=0;i<3;i++){
- t=s;
- for(int j=0;j<3;j++)
- t-=a[i][j];
- a[i][i]=t;
- }
- for(int i=0;i<3;i++){
- for(int j=0;j<3;j++)
- printf("%d ",a[i][j]);
- printf("\n");
- }
- }
3. Little Elephant and Bits
删掉第一个出现的0,如果没有则删掉任意一位。
- # include <iostream>
- using namespace std;
- string s;
- int main()
- {
- cin>>s;
- int p=s.find('0');
- s.erase(s.begin()+(p==-1?0:p));
- cout<<s<<endl;
- return 0;
- }
4. Little Elephant and Elections
数位DP。。这道题有点意思。
- # include <iostream>
- # include <cstdio>
- using namespace std;
- #define Lk(n) (n==4||n==7)
- #define N 12
- #define Mod 1000000007;
- int m,n,cnt[N],F[N][N][2];
- long long ans=0;
- void init()
- {
- scanf("%d",&m);int a[N]={0};
- for(;m;m/=10)a[++n]=m%10;
- F[0][0][0]=F[0][0][1]=1;
- for(int i=1;i<=n;i++)
- for(int j=0;j<=n;j++){
- F[i][j][0]=F[i-1][j][0]<<3;
- if(j)F[i][j][0]+=F[i-1][j-1][0]<<1;
- for(int k=0;k<a[i];k++)
- F[i][j][1]+=F[i-1][j-Lk(k)][0];
- F[i][j][1]+=F[i-1][j-Lk(a[i])][1];
- }
- for(int i=0;i<=n;i++)cnt[i]=F[n][i][1];cnt[0]--;
- }
- void DFS(int dep,int left,long long w)
- {
- w%=Mod;
- if(dep==0){ ans+=w;ans%=Mod;return; }
- for(int i=0;i<=left;i++)
- if(cnt[i])DFS(dep-1,left-i,w*cnt[i]--),cnt[i]++;
- }
- int main()
- {
- init();
- for(int i=1;i<=n;i++)
- if(cnt[i])DFS(6,i-1,cnt[i]);
- cout<<ans<<endl;
- }
5. Little Elephant and LCM
- # include <iostream>
- # include <cstdio>
- # include <cstring>
- # include <algorithm>
- using namespace std;
- const int maxn=100010;
- const int Mod=1000000007;
- #define For(i,a,b) for(int i=a;i<=b;i++)
- int a[maxn],c[maxn],s[maxn],M;
- long long power(long long a,int b)
- {
- long long t=1;
- for( ;b;a=a*a%Mod,b>>=1)if(b&1)t=t*a%Mod;
- return t;
- }
- long long res1,res2;
- long long func(int K)
- {
- int i,m=0;
- for(i=1;i*i<K;i++)
- if(K%i==0)
- c[m++]=i,c[m++]=K/i;
- if(i*i==K)c[m++]=i;
- sort(c,c+m);
- c[m]=M+1;res1=res2=1;
- For(i,0,m-1)res1*=power(i+1,s[c[i+1]]-s[c[i]]),res1%=Mod;
- c[--m]=M+1;
- For(i,0,m-1)res2*=power(i+1,s[c[i+1]]-s[c[i]]),res2%=Mod;
- res1-=res2; return res1<0?res1+Mod:res1;
- }
- int main()
- {
- int n,x; scanf("%d",&n);
- For(i,1,n){scanf("%d",&x);a[x]++;if(x>M)M=x;}
- For(i,1,M)s[i+1]=s[i]+a[i];
- long long ans=1;
- For(i,2,M)ans+=func(i),ans-=ans>Mod?Mod:0;
- cout<<ans<<endl;
- }
6. Little Elephant and Broken Sorting
- # include <iostream>
- # include <cstdio>
- using namespace std;
- int n,m,x,y,a[1001];
- double F[1001][1001],ans;
- void Mix(double &a, double &b)
- {
- a=b=(a+b)/2;
- }
- int main()
- {
- scanf("%d%d",&n,&m);
- for(int i=1;i<=n;i++)scanf("%d",a+i);
- for(int i=1;i<n;i++)
- for(int j=i+1;j<=n;j++)
- F[i][j]=a[i]>a[j],F[j][i]=1-F[i][j];
- for(int i=0;i<m;i++){
- scanf("%d%d",&x,&y);
- Mix(F[x][y],F[y][x]);
- for(int i=1;i<=n;i++)
- if(i-x&&i-y)Mix(F[i][x],F[i][y]),Mix(F[x][i],F[y][i]);
- }
- for(int i=1;i<n;i++)
- for(int j=i+1;j<=n;j++)
- ans+=F[i][j];
- printf("%.8lf",ans);
- }
7. Little Elephant and Tree
- # include <iostream>
- # include <cstdio>
- # include <vector>
- #define fe(i,n) for(__typeof(n.begin())i=n.begin();i!=n.end();i++)
- using namespace std;
- const int N=100010;
- struct Node{
- int l,r,w,s;
- }t[N<<2];
- vector<int>a[N],c[N];
- int p[N],q[N],L[N],R[N],s[N],z[N];
- int cnt,n,m,x,y;
- void update(int x)
- {
- if(t[x].w)
- t[x].s=t[x].r-t[x].l+1;
- else if(t[x].r!=t[x].l)
- t[x].s=t[x<<1].s+t[x<<1|1].s;
- else t[x].s=0;
- }
- void build(int x,int l,int r)
- {
- t[x].l=l,t[x].r=r;
- if(l==r)return;
- int m=(l+r)>>1;
- build(x<<1,l,m);
- build(x<<1|1,m+1,r);
- update(x);
- }
- void change(int x,int l,int r,int d)
- {
- if(t[x].l>r||t[x].r<l)return ;
- else
- if(l<=t[x].l&&t[x].r<=r)t[x].w+=d;
- else
- change(x<<1,l,r,d),change(x<<1|1,l,r,d);
- update(x);
- }
- void dfs(int x,int y)
- {
- L[x]=++cnt;
- fe(i,a[x])if(*i!=y)dfs(*i,x);
- R[x]=cnt;
- }
- inline void tcg(int k,int d){change(1,L[k],R[k],d);}
- void DFS(int x,int y)
- {
- fe(i,c[x])tcg(p[*i],1),tcg(q[*i],1);
- z[x]=t[1].s;
- fe(i,a[x])if(*i!=y)DFS(*i,x);
- fe(i,c[x])tcg(p[*i],-1),tcg(q[*i],-1);
- }
- int main()
- {
- scanf("%d%d",&n,&m);
- for(int i=1;i<n;i++){
- scanf("%d%d",&x,&y);
- a[x].push_back(y);
- a[y].push_back(x);
- }
- dfs(1,0);
- build(1,1,n);
- for(int i=0;i<m;i++){
- scanf("%d%d",p+i,q+i);
- c[p[i]].push_back(i);
- c[q[i]].push_back(i);
- }
- DFS(1,0);
- for(int i=1;i<=n;i++)
- printf("%d ",max(z[i]-1,0));
- return 0;
- }