码迷,mamicode.com
首页 > 其他好文 > 详细

codevs 1009 产生数

时间:2017-10-22 22:01:30      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:整数   btn   targe   button   out   合数   pull   黄金   条件   

1009 产生数

 

2002年NOIP全国联赛普及组

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
 
题目描述 Description

  给出一个整数 n(n<10^30) 和 k 个变换规则(k<=15)。
  规则:
   一位数可变换成另一个一位数:
   规则的右部不能为零。
  例如:n=234。有规则(k=2):
    2-> 5
    3-> 6
  上面的整数 234 经过变换后可能产生出的整数为(包括原数):
   234
   534
   264
   564
  共 4 种不同的产生数
问题:
  给出一个整数 n 和 k 个规则。
求出:
  经过任意次的变换(0次或多次),能产生出多少个不同整数。
  仅要求输出个数。

输入描述 Input Description

键盘输人,格式为:
   n k
   x1 y1
   x2 y2
   ... ...
   xn yn

输出描述 Output Description

 屏幕输出,格式为:
  一个整数(满足条件的个数)

样例输入 Sample Input


   234 2
   2 5
   3 6

样例输出 Sample Output

4

数据范围及提示 Data Size & Hint
 

思路:flyed+组合数学

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
string s;
int k,a,b;
bool dis[11][11];
long long sum=1;
int main(){
    cin>>s>>k;
    for(int i=1;i<=k;i++){
        cin>>a>>b;
        dis[a][b]=1;
    }
    for(int k=0;k<=9;k++)
        for(int i=0;i<=9;i++)
            for(int j=0;j<=9;j++)
                if(i!=j&&j!=k&&k!=i&&dis[i][k]==1&&dis[k][j]==1)
                    dis[i][j]=1;
    for(int i=0;i<s.length();i++) {
        int temp=s[i]-0,num=1;
        for(int j=0;j<=9;j++)
            if(dis[temp][j]==1&&temp!=j)    num++;
        sum*=num;
    }
    cout<<sum;
}

 

codevs 1009 产生数

标签:整数   btn   targe   button   out   合数   pull   黄金   条件   

原文地址:http://www.cnblogs.com/cangT-Tlan/p/7709990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!