zoukankan      html  css  js  c++  java
  • Gym-100814K 数位DP 模拟除法

    Johnny is a brilliant mathematics student. He loves mathematics since he was a child, now he is working on his PhD thesis. He faces a small mathematical problem, he has a n digit integer number (let us call it s) , he wants to find how many substring of s are divisible by a prime number p.

    His supervisor professor is on vacation now, so can you play his role and help him with that?

    https://cn.vjudge.net/contest/174050#problem/K

    #include <bits/stdc++.h>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define INF 30000000
    #define MOD 1000000007
    #define mem(a,b) memset((a),b,sizeof(a))
    #define TS printf("!!!
    ")
    #define pb push_back
    #define pai pair<int,int>
    #define ref(i,x,y)for(int i=x;i<=y;++i)
    #define def(i,x,y)for(int i=x;i>=y;--i)
    //std::ios::sync_with_stdio(false);
    using namespace std;
    //priority_queue<int,vector<int>,greater<int>> que;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> pai1;
    const double EPS=1e-8;
    const double PI=acos(-1);
    //next_permutation
    int T,n,p,number[1000001],s[1000001],t[201];
    unsigned long long a,b;
    int main()
    {
        scanf("%d",&T);
        ref(i,1,T)
        {
                    //freopen("input.txt","r",stdin);
                    //freopen("out.txt","w",stdout);
            cin>> a >> b >> n >> p;
            ref(i,1,n)
            {
                number[i]=a*10/b;
                a=a*10;
                a%=b;
            }
            if(p==2||p==5||p==10)
            {
                long long ans=0;
                ref(i,1,n)if(number[i]%p==0)ans+=i;
                cout<< ans<< endl;
                continue;
            }
            int tmp=1;
            s[n+1]=0;
            def(i,n,1)
            {
                s[i]=(s[i+1]+tmp*number[i])%p;
                tmp=tmp*10%p;
            }
            ref(i,0,p)t[i]=0;
            def(i,n+1,1)t[s[i]]++;
            long long ans=0;
            ref(i,0,p-1)
            if(t[i]>1)ans+=1LL*t[i]*(t[i]-1)/2;
            cout<< ans<< endl;
        }
    }
  • 相关阅读:
    eclipse的安装
    第一章:Javascript语言核心
    jQuery理解之(二)功能函数
    jQuery理解之(一)动画与特效
    jQuery实现单击和鼠标感应事件。
    jQuery使用之(五)处理页面的事件
    jQuery使用之(四)处理页面的表单元素
    jQuery使用之(三)处理页面的元素
    jQuery使用之(二)设置元素的样式
    jQuery使用之(一)标记元素属性
  • 原文地址:https://www.cnblogs.com/Aragaki/p/7565989.html
Copyright © 2011-2022 走看看