hdu 5373 The shortest problem 清晰的思路比什么都重要

The shortest problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 392    Accepted Submission(s): 193


Problem Description
In this problem, we should solve an interesting game. At first, we have an integer n, then we begin to make some funny change. We sum up every digit of the n, then insert it to the tail of the number n, then let the new number be the interesting number n. repeat it for t times. When n=123 and t=3 then we can get 123->1236->123612->12361215.
 

Input
Multiple input.
We have two integer n (0<=n<= 104  ) , t(0<=t<= 105 ) in each row.
When n==-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 quote.
 

Sample Input
  
  
35 2 35 1 -1 -1
 

Sample Output
  
  
Case #1: Yes Case #2: No
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5378  5377  5376  5375  5374 

比赛时我写了一个半小时,之前大脑一片混乱。清晰的思路比什么都重要。

先给出一个数n和一个数t,要求对n做t次操作,最后得到数x,问x能不能 被11整除

每一次操作,将现有数的数字累加,其和直接放在现有数右边。
只不过就是t次相同操作吗?因为每次的数字和可以利用之前的结论,可以用all保留当前的数字和(有点像dp的思想)

计算奇数位的和,还有偶数位的和,取差的绝对值,看能否被11整除。

一开始先求出all;
然后t次循环
for(i=1;i<=t;i++)
{
     更新数字和;
    (每次得到一个数字,分别在它对应的奇数和或偶数和上更新)
   
}
注意:我们的目的是要求处理每一位数字,不关心得到的数是多少,每次用现有的和,得到添加的数,再把添加的数做处理,得到数字和加到现有和中,如此t次循环即可。
我晕。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)

using namespace std;
const int INF =0x3f3f3f3f;
//const int maxn=    ;
//const int maxm=    ;
//const int INF=    ;
typedef long long ll;
const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
int n,time;
int kase=0;
int dp=0;
ll sum[2],all;
vector<int>num;
void getall (ll x)
{
    num.clear();
    while(x)
    {
        num.push_back(x%10);
        all+=x%10;
        x/=10;
    }
      for(ll i=num.size()-1;i>=0;i--)
    {
        sum[dp]+=num[i];
        dp^=1;
    }
}

void solve(ll x)
{
    num.clear();
    while(x)
    {
        num.push_back(x%10);
        all+=x%10;
        x/=10;
    }
    for(ll i=num.size()-1;i>=0;i--)
    {
        sum[dp]+=num[i];
        dp^=1;
    }
}
ll abs2(ll x)
{
    return x<0?(-x):x;
}
int main()
{

  while(~scanf("%d%d",&n,&time)&&( n!=-1||time!=-1 ))
  {
      printf("Case #%d: ",++kase);
      all=0;
      dp=0;
      sum[0]=sum[1]=0;
      getall(n);

      for(int i=1;i<=time;i++)
      {
          solve(all);
      }
      ll tmp=abs2(sum[0]-sum[1]);
//      cout<<sum[0]<<" "<<sum[1]<<endl;
      if(tmp%11)  puts("No");
      else puts("Yes");
  }

    return 0;
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值