Description
Petya loves lucky numbers. Everybody knows that positive integers are
Lucky number is
One day Petya came across a positive integer
Input
The only line contains a positive integer
Output
Output the least super lucky number that is more than or equal to
Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.
Sample Input
4500
4747
47
47
#include #include #include #include using namespace std; int a[10000]; int len,deep; long long n,mini,minn=100000000000000; void dfs(int n4,int n7,long long x) { if (n4==n7) if (x>=n && x-n<</span>minn) { minn=x-n; mini=x; } if (n4+n7>deep) return; dfs(n4+1,n7,x*10+4); dfs(n4,n7+1,x*10+7); } int main() { scanf("%I64d",&n); long long x=n; while (x>0) { deep++; x=x/10; } if (deep % 2==1 ) deep++; else deep+=2; dfs(0,0,0); printf("%I64d\n",mini); return 0; }
Description
At the beginning, the two dices may face different(which means there exist some i, a
Now Ddy wants to calculate the minimal steps that he has to take to achieve his goal.
Input
For each case, the first line consists of six integers a
The second line consists of six integers b
Output
Sample Input
Sample Output
#include #include #include #include using namespace std; int b[7]; int a[7]; int c[7]; int q[10000][8]; int head, tail; bool f1() { for (int i=1; i<=6; i++) a[i]=q[head][i]; c[5]=a[5]; c[6]=a[6]; c[1]=a[4]; c[2]=a[3]; c[3]=a[1]; c[4]=a[2]; for (int j=0; j<</span>tail; j++) { int i; for (i=1; i<=6; i++) if (c[i]!=q[j][i]) break; if (i==7) return 0; } for (int i=1; i<=6; i++) q[tail][i]=c[i]; q[tail][7]=q[head][7]+1; int i; for (i=1; i<=6; i++) if (c[i]!=b[i]) break; if (i==7) { printf("%d\n",q[tail][7]); return 1; } tail++; return 0; } bool f2() { for (int i=1; i<=6; i++) a[i]=q[head][i]; c[5]=a[5]; c[6]=a[6]; c[1]=a[3]; c[2]=a[4]; c[3]=a[2]; c[4]=a[1]; for (int j=0; j<</span>tail; j++) { int i; for (i=1; i<=6; i++) if (c[i]!=q[j][i]) break; if (i==7) return 0; } for (int i=1; i<=6; i++) q[tail][i]=c[i]; q[tail][7]=q[head][7]+1; int i; for (i=1; i<=6; i++) if (c[i]!=b[i]) break; if (i==7) { printf("%d\n",q[tail][7]); return 1; } tail++; return 0; } bool f3() { for (int i=1; i<=6; i++) a[i]=q[head][i]; c[3]=a[3]; c[4]=a[4]; c[1]=a[6]; c[2]=a[5]; c[5]=a[1]; c[6]=a[2]; for (int j=0; j<</span>tail; j++) { int i; for (i=1; i<=6; i++) if (c[i]!=q[j][i]) break; if (i==7) return 0; } for (int i=1; i<=6; i++) q[tail][i]=c[i]; q[tail][7]=q[head][7]+1; int i; for (i=1; i<=6; i++) if (c[i]!=b[i]) break; if (i==7) { printf("%d\n",q[tail][7]); return 1; } tail++; return 0; } bool f4() { for (int i=1; i<=6; i++) a[i]=q[head][i]; c[3]=a[3]; c[4]=a[4]; c[1]=a[5]; c[2]=a[6]; c[5]=a[2]; c[6]=a[1]; for (int j=0; j<</span>tail; j++) { int i; for (i=1; i<=6; i++) if (c[i]!=q[j][i]) break; if (i==7) return 0; } for (int i=1; i<=6; i++) q[tail][i]=c[i]; q[tail][7]=q[head][7]+1; int i; for (i=1; i<=6; i++) if (c[i]!=b[i]) break; if (i==7) { printf("%d\n",q[tail][7]); return 1; } tail++; return 0; } int main() { int n; while (scanf("%d",&n)!=EOF) { q[0][1]=n; for (int i=2; i<</span>7; i++) scanf("%d",&q[0][i]); for (int i=1; i<</span>7; i++) scanf("%d",&b[i]); q[0][7]=0; int i; for (i=1; i<=6; i++) if (q[0][i]!=b[i]) break; if (i==7) { printf("0\n"); continue; } head=0; tail=1; while (head!=tail) { if (f1()) break; if (f2()) break; if (f3()) break; if (f4()) break; head++; } if (head==tail) printf("-1\n"); } }
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point
* Walking: FJ can move from any point
* Teleporting: FJ can move from any point
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Output
Sample Input
5 17
Sample Output
4
Hint
#include #include #include #include using namespace std; const int maxn=1000000; int q[maxn+100][2]; bool mark[1100000]; int main() { int n,k; scanf("%d%d",&n,&k); if (n==k) { printf("0\n"); return 0; } int head=0; q[0][0]=n; q[0][1]=0; mark[n]=1; int tail=1; while (head!=tail) { int x=q[head][0]; if (x+1<</span>1001000 && !mark[x+1]) { q[tail][0]=x+1; q[tail][1]=q[head][1]+1; if (x+1==k) { printf("%d\n",q[tail][1]); return 0; } mark[x+1]=1; tail=(tail+1)%maxn; } if (x-1>=0 && !mark[x-1]) { q[tail][0]=x-1; q[tail][1]=q[head][1]+1; if (x-1==k) { printf("%d\n",q[tail][1]); return 0; } mark[x-1]=1; tail=(tail+1)%maxn; } if (x*2<</span>1001000 && !mark[x*2]) { q[tail][0]=x*2; q[tail][1]=q[head][1]+1; if (x*2==k) { printf("%d\n",q[tail][1]); return 0; } mark[x*2]=1; tail=(tail+1)%maxn; } head=(head+1)%maxn; } return 0; }