巧妙啊!
不妨考虑枚举答案,我们不要从小到大枚举,而是从位数之和从小到大枚举。
考虑一个数x,x+1为x的数位之和+1,x×10数位和不变。
于是我们可以在modk的意义下计算x,将x,x+1连一条长度为1的边,将x,x×10连一条长度为0的边。
于是答案就是1→0的距离。
具体实现时使用一种神奇的bfs,具体看代码吧。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include <bits/stdc++.h> #define MAXN 100005 using namespace std; inline int read(){ int x=0,f=1; char ch=getchar(); while (ch<'0'||ch>'9'){ if (ch=='-') f=-1; ch=getchar(); } while (ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^'0'); ch=getchar(); } return x*f; } int d[MAXN],vis[MAXN]; deque<int>Q; inline void Push(int x,int pre,int len,bool flag){ d[x]=min(d[x],d[pre]+len); if (flag) Q.push_front(x); else Q.push_back(x); } int main(){ int K=read(); memset(d,0x3f,sizeof(d)); Q.push_front(1),d[1]=1; while (Q.size()){ int x=Q.front();Q.pop_front(); if (!vis[x]) vis[x]=true; else continue; if (!x){ printf("%d\n",d[x]); return 0; } Push((x+1)%K,x,1,0); Push((x*10)%K,x,0,1); } }
|
CF61E Enemy is weak
水!
离散化之后树状数组随便乱搞就行了。
1234567891011121314151617181920212223242526272829303132333435363738394041424...
CF475D CGCDSSQ
考虑g1=gcd(al,al+1,...,ar),g2=gcd(al,al+1,...,ar,ar+1)g_1=gcd(a_l,a_{l+1},...,a_{r}),g_2=gcd(a_l,a_...