[国家集训队]墨墨的等式

嘟嘟嘟


那天看高一的在做这道题,觉得很有意思,就拿来看看,发现自己没想出来啊……


首先这是一道图论题!
我们记最小的\(a_i\)\(Min\),那么如果一种方案能达到\(Min * i + t\),那么必定能达到\(Min * j + t (j > i)\)。而如果想有尽量多的解,那么对于等式右边每一个值\(t(t < Min)\),我们都要求出一个最小的\(x\),使\(x \equiv t (mod \ \ Min)\)
又看到\(a_i \leqslant 1e6\),这启发我们建出一张有\(1e6\)个点的图,然后每次转移的时候从\(v\)转移到\((dis[v] + a_i) \% Min\)。最后对于每一个节点,我们单独算在\(Bmin\)\(Bmax\)中他的倍数有多少个。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<assert.h>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 15;
const int maxN = 5e5 + 5;
In ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
In void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
#endif
}

ll Min, Max;
int n, a[maxn];

#define pr pair<ll, int>
#define mp make_pair
bool in[maxN];
ll dis[maxN];
In void dijkstra(int s)
{
  Mem(dis, 0x3f); Mem(in, 0);
  priority_queue<pr, vector<pr>, greater<pr> > q;
  q.push(mp(dis[s] = 0, s));
  while(!q.empty())
    {
      int now = q.top().second; q.pop();
      if(in[now]) continue;
      in[now] = 1;
      for(int i = 1; i <= n; ++i)
	{
	  int v = (dis[now] + a[i]) % a[1];
	  if(dis[v] > dis[now] + a[i])
	    {
	      dis[v] = dis[now] + a[i];
	      q.push(mp(dis[v], v));
	    }
	}
    }
}

int main()
{
  //MYFILE();
  n = read(), Min = read(), Max = read();
  for(int i = 1; i <= n; ++i) a[i] = read();
  sort(a + 1, a + n + 1);
  dijkstra(0);
  ll ans = 0;
  for(int i = 0; i < a[1]; ++i)
    if(dis[i] <= Max)
      {
	ans += (Max - dis[i]) / a[1] + 1;
	if(dis[i] < Min) ans -= (Min - dis[i] + a[1] - 1) / a[1];
      }
  write(ans), enter;
  return 0;
}
posted @ 2019-05-17 07:14  mrclr  阅读(330)  评论(0编辑  收藏  举报