luogu P3403 conj. cong shortest

LINK: jumping machine

A thing I wanted to learn a long time ago. Found this thing really magical.

We want to find that all w satisfies \ (w = 1 + ax + by + cz \) . And \ (1 \ leq w \ leq h \)

Violent enumeration does not work.

The approach is as follows: first consider the situation of ax + by first consider combining x and y into a bunch of numbers and then add some z.

Consider setting res = c% z for a floor c. Then as long as (ax + by)% z == res finds the smallest floor, then all floors with% z == res can be reached.

Then for more combinations of ax + by, we only need to reach by z.

Consider whether there is duplication? It is not difficult to find that because of congruence, each floor will only be counted in a remainder at most.

Consider the smallest floor can record all the floors after.

So set \ (f_i \) to indicate that ax + by can run to the smallest floor of the i floor.

It can be easily found that the contribution to the answer for a remainder i is \ (\ frac {(h-f_i)} {z} +1 \)

It mainly uses fixed two parameters and finally increases the third parameter to count.

It can be proved that the scheme is not heavy and leaks.

//#include<bits\stdc++.h>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<queue>
#include<deque>
#include<stack>
#include<vector>
#include<algorithm>
#include<utility>
#include<bitset>
#include<set>
#include<map>
#define ll long long
#define db double
#define INF 1000000000
#define inf 100000000000000000ll
#define ldb long double
#define pb push_back
#define get(x) x=read()
#define gt(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define putl(x) printf("%lld\n",x)
#define gc(a) scanf("%s",a+1)
#define rep(p,n,i) for(RE ll i=p;i<=n;++i)
#define go(x) for(ll i=lin[x],tn=ver[i];i;tn=ver[i=nex[i]])
#define fep(n,p,i) for(RE ll i=n;i>=p;--i)
#define pii pair<ll,ll> 
#define mk make_pair
#define RE register
#define S second
#define gf(x) scanf("%lf",&x)
#define pf(x) ((x)*(x))
#define ull unsigned long long
#define mod 1000000007
#define ui unsigned
#define P 13331ll
using namespace std;
char buf[1<<15],*fs,*ft;
inline char getc()
{
    return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;
}
inline ll read()
{
    register ll x=0,f=1;register char ch=getc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getc();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getc();}
    return x*f;
}
const ll MAXN=100010;
ll n,ans,len;
ll x,y,z;
ll f[MAXN],vis[MAXN];
ll lin[MAXN],ver[MAXN<<1],nex[MAXN<<1],e[MAXN<<1];
priority_queue<pii>q;
inline void add(ll x,ll y,ll z)
{
	ver[++len]=y;
	nex[len]=lin[x];
	lin[x]=len;
	e[len]=z;
}
signed main()
{
	//freopen("1.in","r",stdin);
	get(n);get(x);get(y);get(z);
	if(x==1||y==1||z==1){putl(n);return 0;}
	memset(f,-1,sizeof(f));
	f[1]=1;q.push(mk(1,1));
	rep(0,z-1,i)add(i,(i+y)%z,y),add(i,(i+x)%z,x);
	while(q.size())
	{
		ll x=q.top().S;q.pop();
		if(vis[x])continue;
		vis[x]=1;
		go(x)
		{
			if(f[tn]==-1||f[tn]>f[x]+e[i])
			{
				f[tn]=f[x]+e[i];
				q.push(mk(-f[tn],tn));
			}
		}
	}
	rep(0,z-1,i)if(f[i]!=-1&&n>=f[i])ans=(ans+(n-f[i])/z+1);
	putl(ans);return 0;
}

Guess you like

Origin www.cnblogs.com/chdy/p/12722386.html