HDU 5373 The sharest proble(再帰的呼び出し、詳細)

2387 ワード

タイトルの住所
http://acm.hdu.edu.cn/showproblem.php?pid=5373
The shotest problem
Time Limit:3000/1500 MS(Java/Others)    メモリLimit:65536/65536 K(Java/Others)Total Submission(s):935    Acceepted Submission(s):479
Problem Description
In thisisproblem、we shoul d some some funny change.We sum uup evevevevevevedigit of the n、then inset t t t t to the tailof the numben、then then the ininininininininininben the inininininininininininininininininineeeeeben.then.then.eeeeumbeben.then.then.the inininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininget 123->1236->123612->12361215.
 
Input
Multiple input.
We have two integer n(==n==
104 ) , t(0<=t<==
105)in each row.
When=-1 and t=-1 mean the end of input.
 
Output
For each input,if the final number are divisible by 11,output“Yes”,else output”No.without quot.
 
Sample Input

   
   
   
   
35 2 35 1 -1 -1
 
Sample Output

   
   
   
   
Case #1: Yes Case #2: No
 
考え方:
この数は11で割り切れるかどうかを判断して、数十分の大きさで爆発します。簡単な方法は奇数桁の合計から偶数桁の合計を引いて11で割り切れるかどうかです。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<math.h>
#include<stdlib.h>
using namespace std;
int tot;
__int64 ji,ou,sum;
void solve(int x){
    if(x>=10)    solve(x/10);
    if(tot%2==0){
        ou+=x%10;
    }
    else ji+=x%10;
    sum+=x%10;
    tot++;
    return;
}
int main()
{
    int ncase=0,n,t;
    while(cin>>n>>t)
    {
        ncase++;
        if(n==-1&&t==-1)
            break;
        ji=0,ou=0;
        sum=0,tot=0;
        solve(n);
       // printf("%d %d %d
",ji,ou,sum); for(int i=0;i<t;i++){ solve(sum); } printf("Case #%d: ",ncase); // printf("%d %d %d
",ji,ou,sum); if(abs(ji-ou)%11==0) printf("YES
"); else printf("NO
"); } return 0; }
コードはC++で140 ms+険に渡し、G++で700 ms+を払えばいいです。