[题解+总结]20150913

1、前言

  我总算是有点心情来写写总结了。其实这样也不好。。。貌似总结没考好的才更重要,但是我自认为那都是因为一些七七八八的原因导致的。。。不过话说回来差距还是很大的。这几天已经考了很多次NOIP模拟题,说是模拟题,但其实每一天的难度变化都比较大,有些知识点也明显地涉及到了省选难度。今天感觉是难度比较适中的一次,可以略简单,因为没有数据结构这种码农题,大多数用到的算法不多,但是还是有点价值的。

2、Sum 求和

大概题意:求(1^b+2^b+...+a^b) mod 10000。

总结:虽说这一眼就知道是快速幂,但是当然不够,因为a,b<=10^9,显然O(n)都是很勉强的。一考完问了ZZD立马觉得自己脑抽了。mod 10000不是用来玩的啊!很容易的可以看出,在取模意义下,x^b=(x+10000)^b。所以实际运算范围是n<=10000,然后直接在此基础上乘上倍数就行了。

题解:快速幂+优化

代码:

-------------------------------------------------------------------------------------------------------

#include<cstdio>
#define MOD 10000

typedef long long ll;

ll n,k,ans,t,lans;

inline ll pow(ll a,ll b)
{
  ll base=a,ans=1;
  while (b)
  {
    if (b&1) (ans*=base)%=MOD;
    (base*=base)%=MOD;
    b>>=1;
  }
  return ans;
}

int main()
{
  freopen("sum.in","r",stdin);
  freopen("sum.out","w",stdout);
  scanf("%d",&t);
  while (t--)
  {
    scanf("%I64d %I64d",&n,&k);
    ll ans=0,x=n/MOD,m=n%MOD;
    for (int i=1;i<=10000;i++)
    {
      (ans+=pow(i,k))%=MOD;
      if (i==m) lans=ans;
    }
    (((ans*=x)%=MOD)+=lans)%=MOD;
    printf("%I64d\n",ans);
  }
  return 0;
}

-------------------------------------------------------------------------------------------------------

3、Sequence 序列合并

大概题意:给出两个长度为n的序列,在两者中多次各取1个数组成一个新数,共n^2个,求出最小的n个。

总结:貌似以前做过?第一次做的时候好像还不会堆,想了好久怎么去贪心。现在看起来这道题还是非常裸的。

题解:首先从小到大分别对两个序列进行排序,将a[1]+b[1..n]加入堆中,然后根据堆顶逐步加进去新的数。。。直到得到n个数。

4、Tower 选择坐标

大概题意:平面上有n个点,移动某个点的代价为原位置和新位置的曼哈顿距离。求使得k(k∈[1,n])个点在同一位置上最小代价。

总结:数据范围小的让我以为这算法一定不平凡。虽然我们不能确定每次所选的那个店一定在现有点之上,但是其实想一想就可以发现它一定是在所有已有点中的横坐标中的一个,纵坐标亦是,所以枚举已知坐标就行了,因为显然在一个10^6的地图上枚举是不可行的。

代码:

-------------------------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>

#define MAXN 55
#define INF 0x3f3f3f3f
using std::sort;

int min(int a,int b) { return a<b?a:b; }

int abs(int x) { return x<0?-x:x; }

struct Point
{
  int x,y;
};
Point a[MAXN];

int n,ans[MAXN],dis[MAXN];

int main()
{
  freopen("tower.in","r",stdin);
  freopen("tower.out","w",stdout);
  scanf("%d",&n);
  for (int i=1;i<=n;i++) scanf("%d %d",&a[i].x,&a[i].y);
  memset(ans,INF,sizeof(ans));
  for (int i=1;i<=n;i++)
    for (int j=1;j<=n;j++)
    {
      for (int k=1;k<=n;k++) dis[k]=abs(a[k].x-a[i].x)+abs(a[k].y-a[j].y);
      sort(dis+1,dis+n+1);
      int o=0;
      for (int k=1;k<=n;k++) o+=dis[k],ans[k]=min(o,ans[k]);
    }
  for (int i=1;i<=n;i++) printf("%d\n",ans[i]);
  return 0;
}

-------------------------------------------------------------------------------------------------------

5、Binary 二进制

大概题意:给出三个数a,b,c的二进制,设三者最大长度为L,现需要构造三个数x,y,z,要求满足:他们的二进制形式长度均不超过L;a中1的个数与x相同;b中1的个数与y相同;c中1的个数与z相同;x+y=z。求最小的z。

总结:O(n^2 log n)预处理出长度和1的个数,然后O(n^2)去判断,得了40分。然而还有一种暴力方式可以得50分。好这些都不重要。

题解:数位DP。

代码(from ZZD):

------------------------------------------------------------------------------------------------------

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

#define LY(p) freopen (p".in", "r", stdin); freopen (p".out", "w", stdout)
#define LL long long
#ifdef WIN32
#define L_L "%I64d"
#else
#define L_L "%lld"
#endif
using namespace std;
int n, at, bt, ct, T;
LL A, B, C, inf (1LL << 40);
LL f[35][2][35][35][35];

void upd (LL &a, LL x) {a = min (x, a);}

void work (int w, int t, int j, int k, int l) {
  LL now = f[w][t][j][k][l];
  for (int x = 0; x <= 1; x++)
  for (int y = 0; y <= 1; y++)
  if (j + x <= at && k + y <= bt)
  if (l + ((t + x + y) & 1) <= ct)
  upd (f[w + 1][(t + x + y) >> 1][j + x][k + y][l + ((t + x + y) & 1)], now + (x << w) + (y << w));
}

int calc (LL a) {
  int len(0), cnt(0);
  while (a) {
    len ++;
    if (a & 1) cnt ++;
    a >>= 1;
  }
  return n = max (len, n), cnt;
}

int main()
{
  LY("binary");
  scanf ("%d", &T);
  while (T --) {
    scanf (L_L L_L L_L, &A, &B, &C);

    n = 0;
    at = calc (A), bt = calc (B), ct = calc (C);

    for (int i = 0; i <= n + 1; i++)
      for (int t = 0; t <= 1; t++)
        for (int j = 0; j <= at; j++)
          for (int k = 0; k <= bt; k++)
            for (int l = 0; l <= ct; l++)
              f[i][t][j][k][l] = inf;
    f[0][0][0][0][0] = 0;
    for (int i = 0; i < n; i++)
      for (int t = 0; t <= 1; t++)
        for (int j = 0; j <= at; j++)
          for (int k = 0; k <= bt; k++)
            for (int l = 0; l <= ct; l++)
              if (f[i][t][j][k][l] != inf)
                work (i, t, j, k, l);

    printf (L_L"\n", f[n][0][at][bt][ct] == inf? -1 : f[n][0][at][bt][ct]);
  }
  return 0;
}

------------------------------------------------------------------------------------------------------

时间: 2024-08-25 23:14:04

[题解+总结]20150913的相关文章

洛谷 P1079 Vigen&#232;re 密码 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=1079 题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简单易用,且破译难度比较高,曾在美国南北战争中为 南军所广泛使用. 在密码学中,我们称需要加密的信息为明文,用 M 表示:称加密后的信息为密文,用 C 表示:而密钥是一种

8.8联考题解

今天的T1让我怀疑我是不是在做奥赛题--这考的是什么知识点啊这个,会不会用绝对值函数? Evensgn 的债务 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Evensgn 有一群好朋友,他们经常互相借钱.假如说有三个好朋友A,B,C.A 欠 B 20 元,B 欠 C 20 元,总债务规模为 20+20=40 元.Evensgn 是个追求简约的人,他觉得这样的债务太繁杂了.他认为,上面的债务可以完全等价为 A 欠C20 元,B 既不欠别人,别人也不欠他.这样总债务规模就压缩到了 

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

2017ZZUACM省赛选拔试题部分题解----谨以纪念我这卡线滚粗的美好经历

写在前面: 其实心里有些小小的不爽又有点小小的舒畅,为啥捏?不爽当然是因为没被选拔上啦,舒畅捏则是因为没被选拔上反而让自己警醒,学长也提点很多很多."沉下去,然后一战成名"学长如是对我说,我很开心.其实这完全算不算是题解,只是我个人的一些小想法而已.而且到现在还有一题不会...让自己长点记性吧. 题目 A :聪明的田鼠 Time Limit: 1 Sec Memory Limit: 128 MB Description 田鼠MIUMIU来到了一片农田,农田可以看成是一个M*N个方格的矩

LeetCode-001题解

此题目摘自LeetCode001 Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.

leetcode题解: Next Permutation

最近还一直在刷leetcode,当然,更多时候只是将题解写在自己的电脑上,没有分享出来.偶尔想起来的时候,就写出来. public class Solution { public void nextPermutation(int[] nums) { if(nums==null||nums.length<=1) return; nextPermutationHelp( nums,0,nums.length-1); } public void nextPermutationHelp(int []nu

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

HDU 1045 Fire Net 二分图Bipartite题解

本题可以使用DFS直接爆搜出答案,不过这样类型的题目其实是个二分图的题解. 这个二分图,难不在Hungary算法,而是难在于建图.需要挺高的抽象思维的. 建图: 1 把同一行不被X分开的格子标同一个号码,被X分开的标下一个号码,这样做是为了缩点,不需要把所有的格子都分开标号,而且可以更方便建个更加小的图. 2 同理把同一列的格子标号 3 然后判断相同一个格子的行标号和列标号是有路径的,其他不在同一个格子的都是没有路径的. 4 这样就等于以行标号和列标号作为左右顶点,构建成一个二分图了 然后使用H