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;
        }
    }
  • 相关阅读:
    docker容器升级脚本
    Haproxy状态监控配置教程
    负载均衡之Haproxy配置详解(及httpd配置)
    升级代码脚本
    升级数据库脚本(加入事务)
    监控端口,新疆模拟用户登录脚本
    mongodb3.2系统性学习——3、update()操作
    Java Socket编程
    php错误级别设置
    php 用于绘图使用的颜色数组
  • 原文地址:https://www.cnblogs.com/Aragaki/p/7565989.html
Copyright © 2011-2022 走看看