现在的位置: 首页 > 综合 > 正文

HDU4432-第37届ACM/ICPC天津现场赛B题

2013年02月09日 ⁄ 综合 ⁄ 共 970字 ⁄ 字号 评论关闭

题目:题目链接

这道题目是一道水题,而我却卡了好久,太囧(/ □ \)了。

题目是说给你一个数字n,让你找到n的因子,把所有的因子转换成m进制。然后对每一位求平方和。再把最后的和转变

成m进制表示。输出就OK。

刚开始写,就直接写了转变函数。然后就枚举因子,1-n。可是一直tle。后来改成1-sqrt(n).就是WA,自己一直在看前

面的,没有注意后面的输出。弄了好久还是WA。最后,SB了。才发现自己在输出的时候竟然没有考虑m大于9的情况,这

时候是需要用字母表示的,而我去一直在用数字表示,SB了。唉.代码:


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <stack>
using namespace std;
int n, m;
#define N 1000001
int mx[N];
int len;
void getm(int x)
{
    int i = 0;
    while(x > 0)
    {
        int tp = x%m;
        mx[i++] = tp;
        x /= m;
    }
    len = i;
}

int main()
{
    while(scanf("%d%d", &n, &m) == 2)
    {
        int sum = 0;
        for(int i = 1; i <= sqrt(n)+1; ++i)
        {
            if(n%i==0)
            {
                int tmp=i;
                while(tmp)
                {
                    sum+=((tmp%m)*(tmp%m));
                    tmp/=m;
                }
                tmp=n/i;
                if(tmp==i)
                    continue;
                while(tmp)
                {
                    sum+=((tmp%m)*(tmp%m));
                    tmp/=m;
                }
            }
        }
        memset(mx, 0, sizeof(mx));
        getm(sum);
        for(int i=len-1; i>=0; i--)
        {
            if(mx[i]>9)
                printf("%c",mx[i]-10+'A');
            else printf("%d",mx[i]);
        }
        printf("\n");
    }
    return 0;
}

后面超时我就把转变函数放进main里面了,大家可以试试直接调用getm函数,看能不能过...


努力努力...

抱歉!评论已关闭.