51NOD 1770 数数字 循环节

1770 数数字
基准时间限制:1 秒 空间限制:262144 KB 分值: 20 难度:3级算法题 收藏  关注
统计一下 aaa ⋯ aaan个a × b 的结果里面有多少个数字d,a,b,d均为一位数。
样例解释:
3333333333*3=9999999999,里面有109。

Input
多组测试数据。
第一行有一个整数T,表示测试数据的数目。(1≤T≤5000)
接下来有T行,每一行表示一组测试数据,有4个整数a,b,d,n。 (1a,b≤90≤d≤91≤n≤10^9)
Output
对于每一组数据,输出一个整数占一行,表示答案。
Input示例
2
3 3 9 10
3 3 0 10
Output示例
10
0
System Message (题目提供者)

明显的寻找循环节
注意最后一次进位 如果d=0 要排除

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
#include<queue>
#include<sstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

int a,b,d,n;

map<pii,int>mp;
vector<pii>vec;
int slove(){
    int ans=0;
    mp.clear();
    vec.clear();
    pii t = make_pair(a*b%10,a*b/10);//first:当前位的值 second:进位的值
    int loopStart = -1,loopEnd = -1;
    for(int i=0;i<n;++i){
        if(mp.find(t)==mp.end()){
            vec.push_back(t);
            mp[t]=i;
            pii tmp;
            tmp.first = (a*b%10 + t.second)%10;
            tmp.second = (a*b + t.second)/10;
            t = tmp;
        }
        else{//发现循环节
            loopStart = mp[t];
            loopEnd = i;
            break;
        }
    }

    for(int i=0;i<loopStart;++i){
        ans += vec[i].first == d;
    }
    if(loopEnd>loopStart){
        int loopD=0;
        for(int i=loopStart;i<vec.size();++i){
            loopD+=vec[i].first == d;
        }
        ans += ((n-loopStart)/(loopEnd-loopStart))*loopD;

        if(d!=0){
            ans += vec[(n-loopStart)%(loopEnd-loopStart) + loopStart].second == d;
        }
    }
    else{
        for(int i=0;i<vec.size();++i){
            ans += vec[i].first==d;
        }
        if(d!=0){
            ans += vec.back().second == d;
        }
    }
    return ans;
}
int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    //freopen("/home/lu/Documents/w.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d%d",&a,&b,&d,&n);
        printf("%d\n",slove());
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值