Codeforces Round #359 (Div. 2) C. Robbers' watch (DFS)
来源:互联网 发布:慈溪行知职高老师 编辑:程序博客网 时间:2024/05/23 01:01
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches.
First, as they know that kingdom police is bad at math, robbers use the positional numeral systemwith base7. Second, they divide one day inn hours, and each hour inm minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from0 ton - 1, while the second has the smallest possible number of places that is necessary to display any integer from0 tom - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base7 than this watches have, the required number of zeroes is added at the beginning of notation.
Note that to display number 0 section of the watches is required to have at least one place.
Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches aredistinct. Help her calculate this number.
The first line of the input contains two integers, given in the decimal notation,n andm (1 ≤ n, m ≤ 109) — the number of hours in one day and the number of minutes in one hour, respectively.
Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct.
2 3
4
8 2
5
In the first sample, possible pairs are: (0: 1),(0: 2),(1: 0), (1: 2).
In the second sample, possible pairs are: (02: 1),(03: 1),(04: 1), (05: 1), (06: 1).
题意:给你n和m,找出(a,b)的对数,其中a满足要求:0<=a<n,a的7进制的位数和n-1的7进制的位数相同,b满足要求:0<=b<m,b的7进制的位数和m-1的7进制的位数相同,且a和b的7进制下的位上的数都不相同,即如果a的七进制数为10,b的7进制数为21,这种情况是不行的,因为重复了1
思路:枚举每一位的数,一个标记数组就可以了,当找完a再找b,注意中间的回溯,找完b检查一下就好了
ac代码:
#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<set>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 1010000#define LL long long#define ll __int64#define INF 0xfffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)#define eps 1e-8using namespace std;ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll lcm(ll a,ll b){return a/gcd(a,b)*b;}ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}//headint vis[10];ll a,b;int n,m,lena,lenb;int ans;int A[10],B[10];int QAQ(int x){ int cnt=x?0:1; while(x) x/=7,cnt++; return cnt;}void DFS2(int x){ if(x==lenb+1){ b=0;int k=1; for(int j=1;j<=lenb;j++) b=b+k*B[j],k*=7; if(b<m) ans++; return; } for(int i=0;i<7;i++){ if(!vis[i]){ vis[i]=1;B[x]=i; DFS2(x+1); vis[i]=0; } }}void DFS1(int x){ if(x==lena+1){ a=0;int k=1; for(int j=1;j<=lena;j++) a=a+k*A[j],k*=7; if(a<n) DFS2(1); return; } for(int i=0;i<7;i++){ if(!vis[i]){ vis[i]=1;A[x]=i; DFS1(x+1); vis[i]=0; } }}int main(){ scanf("%d%d",&n,&m); lena=QAQ(n-1); lenb=QAQ(m-1); //cout<<lena<<' '<<lenb<<endl; ans=0; if(lena+lenb>7){ printf("0\n"); return 0; } mem(vis);DFS1(1); printf("%d\n",ans); return 0;}
- Codeforces Round #359 (Div. 2) C. Robbers' watch (DFS)
- Codeforces Round #359 (Div. 2) C. Robbers' watch dfs
- Codeforces Round #359 (Div. 2) C. Robbers' watch
- Codeforces Round #359 (Div. 2)C. Robbers' watch
- Codeforces Round #359 (Div. 2) C. Robbers' watch
- Codeforces Round #359 (Div. 1) A. Robbers' watch 暴力
- CodeForces 686C Robbers' watch (dfs)
- Codeforces Round #359 (Div. 2) C DFS
- CodeForces 686C Robbers' watch
- Codeforces Round #301 C (Div. 2) 【dfs】
- Codeforces Round #321 (Div. 2) C DFS
- Codeforces Round #376 (Div. 2)C(DFS)
- CodeForces 685A 686C Robbers' watch
- codeforces_686C. Robbers' watch(dfs)
- Codeforces Round #359 (Div. 2) D DFS
- Classroom Watch (Codeforces Round #441 (Div.2) )
- Codeforces Round #256 (Div. 2) C. Painting Fence (DFS)
- Codeforces Round #321 (Div. 2)(C)模拟,DFS
- 毕向东Java视频学习笔记【Day10 多态+内部类】
- 加载图片的三种模式
- eclipse编译问题
- typedef int int_array[4]
- Ceph由于更换IP地址导致MON异常
- Codeforces Round #359 (Div. 2) C. Robbers' watch (DFS)
- iShare.js分享插件
- xml操作
- 【全球囧闻】英国公投真不是逗你玩!“脱欧”龙卷风突袭全球……
- OPENSTACK常见问题及注意事项汇总
- ReactJS学习系列课程(props 组件属性)
- ASP.NET MVC Bootstrap 个人博客站 www.zynblog.com
- 动画 方块旋转
- Codeforces Round #359 (Div. 2) 部分题解