BestCoder 的两道水题 HDU 4989 4990

Summary

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 127    Accepted Submission(s): 91


Problem Description
Small W is playing a summary game. Firstly, He takes N numbers. Secondly he takes out every pair of them and add this two numbers, thus he can get N*(N - 1)/2 new numbers. Thirdly he deletes the repeated number of the new numbers. Finally he gets the sum of the left numbers. Now small W want you to tell him what is the final sum.
 

Input
Multi test cases, every case occupies two lines, the first line contain n, then second line contain n numbers a 1, a 2, ……a n separated by exact one space. Process to the end of file.
[Technical Specification]
2 <= n <= 100
-1000000000 <= a i <= 1000000000
 

Output
For each case, output the final sum.
 

Sample Input
      
      
4 1 2 3 4 2 5 5
 

Sample Output
      
      
25 10
Hint
Firstly small W takes any pair of 1 2 3 4 and add them, he will get 3 4 5 5 6 7. Then he deletes the repeated numbers, he will get 3 4 5 6 7, Finally he gets the sum=3+4+5+6+7=25.
题意很明显了,做法也就是两重循环的暴力注意去重即可,保存答案要用LONG LONG 也就没啥了。感觉能用MAP去重? MAP不熟练所以没有去写。。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

long long a[10005];
long long v[210000][2];
int n;

int main()
{
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        memset(v,0,sizeof(v));
        int k=0;
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                v[k++][0]=a[i]+a[j];
            }
        }
        long long ans=0;
        for(int i=0;i<k;i++)
        {
            for(int j=i+1;j<k;j++)
            {
                if(v[i][0]==v[j][0])
                {
                    v[j][1]=1;
                }
            }
        }
        for(int i=0;i<k;i++)
        {
            if(v[i][1]==0)
            {
                ans+=v[i][0];
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}



Reading comprehension

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 283    Accepted Submission(s): 131


Problem Description
Read the program below carefully then answer the question.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include<vector>

const int MAX=100000*2;
const int INF=1e9;

int main()
{
  int n,m,ans,i;
  while(scanf("%d%d",&n,&m)!=EOF)
  {
    ans=0;
    for(i=1;i<=n;i++)
    {
      if(i&1)ans=(ans*2+1)%m;
      else ans=ans*2%m;
    }
    printf("%d\n",ans);
  }
  return 0;
}
 

Input
Multi test cases,each line will contain two integers n and m. Process to end of file.
[Technical Specification]
1<=n, m <= 1000000000
 

Output
For each case,output an integer,represents the output of above program.
 

Sample Input
   
   
1 10 3 100
 

Sample Output
   
   
1 5
 
题意转化就是F(n)=F(n-1)*2+1 n为奇数  F(n)=2*F(n-1) n为偶数。
这样的递推关系很容易想到矩阵快速幂维数是2*2的。。。
分奇数偶数的话 构造的矩阵不相同,但是没关系,可以合并在一起比如n为奇数是构造的矩阵为A 偶数时为B 可以将两个矩阵相乘 快速幂n/2即可 然后再看初始N是奇数还是偶数,如果是奇数就再左乘一个矩阵即可。
当然这题还能分奇数偶数找规律不过要用到逆元 然后直接写普通的快速幂也能求解。。。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
long long n,mod;
struct juzhen{

long long m[2][2];

};

juzhen mut(juzhen a,juzhen b)
{
    juzhen ans;
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<2;j++)
        {
            ans.m[i][j]=0;
            for(int k=0;k<2;k++)
            {
                ans.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod;
                ans.m[i][j]%=mod;
            }
        }
    }
    return ans;
}

juzhen power(juzhen a,long long p)
{
    juzhen ans;
    memset(ans.m,0,sizeof(ans.m));
    for(int i=0;i<2;i++)
    {
        ans.m[i][i]=1;
    }
    while(p)
    {
        if(p&1)
            ans=mut(ans,a);
        a=mut(a,a);
        p>>=1;
    }
    return ans;
}

int main()
{
    while(cin>>n>>mod)
    {
        juzhen ans1,ans2;
        memset(ans1.m,0,sizeof(ans1.m));
        memset(ans2.m,0,sizeof(ans2.m));
        ans1.m[0][0]=2;
        ans1.m[0][1]=1;
        ans1.m[1][0]=0;
        ans1.m[1][1]=1;
        ans2.m[0][0]=2;
        ans2.m[0][1]=ans2.m[1][0]=0;
        ans2.m[1][1]=1;
        if(n%2==0)
        {
            n=n/2;
            juzhen ans=mut(ans2,ans1);
           // cout<<ans.m[0][1]<<endl;
            ans=power(ans,n);
            cout<<ans.m[0][1]%mod<<endl;
        }
        else
        {
            n=n/2;
            juzhen ans=mut(ans2,ans1);
            ans=power(ans,n);
            ans=mut(ans1,ans);
            cout<<ans.m[0][1]%mod<<endl;

        }
    }
    return 0;
}

4991是树状数组不会。
4992是求一个数的所有原根,原来在POJ上做过一道求原根的裸题,大致知道这题应该怎么思考,但是没去写。。。
最近快被高斯消元逼疯了,增广矩阵构造不出来简直心塞,偷偷刷了两道水题愉悦一下心情= =。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值