zoukankan      html  css  js  c++  java
  • Simple Addition

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=31329#problem/V

    使用题目所给函数,单单从某一个数字来看,就是直接求这个数各个数位上的和;而且p=====>q之间的数调用这个函数,其数值都是在1~9之间;因此,求x和y%10的值,然后就直接45*((y-x)/10 );45是1+2+。。。+9的和,后面代表,p和q之间拥有多少个满足条件的组数,然后直接使用DFS递推即可

    #include<map>
    #include<set>
    #include<list>
    #include<cmath>
    #include<ctime>
    #include<deque>
    #include<stack>
    #include<bitset>
    #include<cstdio>
    #include<vector>
    #include<cstdlib>
    #include<cstring>
    #include<iomanip>
    #include<numeric>
    #include<sstream>
    #include<utility>
    #include<iostream>
    #include<algorithm>
    #include<functional>
    
    using namespace std ;
    long long ans , p , q ; 
    long long f( long long n )
    {
    	if( n == 0 )
    		return 0 ;
    	else if( n % 10 )
    	{
    		return n % 10 ;
    	}
    	else
    	{
    		return f( n / 10 ) ;
    	}
    }
    
    void DFS( long long x , long long y )
    {
    	long long i , j ; 
    	if( y - x < 10 )
    	{
    		for( int i  = x ; i <= y ; ++i )
    		{
    			ans += f( i ) ;
    		}
    		return ;
    	}
    	for( i = x ; i % 10 != 0 ; ++i )
    	{
    		ans += f( i ) ;
    	}
    	for( j = y ; j % 10 != 0 ; --j )
    	{
    		ans += f( j ) ;
    	}
    	ans += 45 * ( ( j - i  ) / 10 );
    	DFS( i / 10 , j / 10 ) ;
    }
    int main()
    {
    
    	while( scanf( "%lld%lld" , &p , &q ) != EOF )
    	{
    		if( p == -1 && q == -1 )
    			break ;
    		ans  = 0 ; 
    		DFS( p , q ) ;
    		printf( "%lld
    " , ans ) ;
    	}
    	return 0 ;
    }
    


  • 相关阅读:
    poj 2104(线段树)
    poj 1962(并查集+带权更新)
    hdu 2818(并查集,带权更新)
    hdu 1856
    hdu 3172
    hdu 1325(并查集)
    hdu 5023
    pku 2777(经典线段树染色问题)
    hdu 1671(字典树判断前缀)
    hdu 1247 (字典树入门)
  • 原文地址:https://www.cnblogs.com/pangblog/p/3320152.html
Copyright © 2011-2022 走看看