(规律+思维)1770 数数字

1770 数数字

  1. 1 秒
  2.  
  3. 262,144 KB
  4.  
  5. 20 分
  6.  
  7. 3 级题

统计一下 aaa ⋯ aaan个a × baaa ⋯ aaa⏟n个a × b 的结果里面有多少个数字d,a,b,d均为一位数。

样例解释:

3333333333*3=9999999999,里面有10个9。


 收起

输入

多组测试数据。
第一行有一个整数T,表示测试数据的数目。(1≤T≤5000)
接下来有T行,每一行表示一组测试数据,有4个整数a,b,d,n。 (1≤a,b≤9,0≤d≤9,1≤n≤10^9)

输出

对于每一组数据,输出一个整数占一行,表示答案。

输入样例

2
3 3 9 10
3 3 0 10

输出样例

10
0

题解:在纸上画了画,发现有些规律,想了想,大约9*9种情况;

           我是这么做的,先暴力求出前15位的情况,把 1 ~ 9 出现的次数都进行记录,这样我们只需要对 位数为15的和位数为14的,进行推导分析进行。

           最后肯定是有一位数字在不断地增加,还有个需要注意的地方,已知n和规律,怎么求所要求数出现次数,这里只分析最后不断增加的数:

           用 位数为15的找出那些不变化的个数(k),答案 = m - k

           m :是最后的位数,当 a*b>=10 , m = n+1; 否则,m = n; 

#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=max(tree[rt<<1],tree[rt<<1|1])
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int M=1e3+5;
const int N=5e6+5;
struct fun{
    int num[15];
}f[20];
int main(){
    int t,a,b,d,n;
    scanf("%d",&t);
    while(t--){
        for(int i=1;i<=15;i++) memset(f[i].num,0,sizeof(f[i].num));
        scanf("%d %d %d %d",&a,&b,&d,&n);
        int m=n;
        if(a*b>=10)
            m++;
        for(int i=1;i<=15;i++){
            ll ans=0;
            for(int j=1;j<=i;j++)
                ans=ans*10+a;
            ans*=b;
            while(ans){
                f[i].num[ans%10]++;
                ans/=10;
            }
        }
        if(n<=15){
            printf("%d\n",f[n].num[d]);
            continue;
        }
        if(!f[15].num[d]){
            printf("0\n");
            continue;
        }
        if(f[15].num[d]==f[14].num[d]){
            printf("%d\n",f[15].num[d]);
        }
        else{
            ll k=0;
            for(int i=0;i<=9;i++){
                if(i==d) continue;
                k+=f[15].num[i];
            }
            ll sum=m-k;
            printf("%lld\n",sum);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/black_horse2018/article/details/89739453