【SPOJ】SPCQ - Gopu and Digits Divisibility 数位处理

(sb CSDN有些毛病,先贴这里……)

题意:T组数据,每组数据输入一个n,求最小的不小于n的x,满足x的各位加一起可以整除x。

题解:暴力。直接从n开始枚举x判断各位加一起是否能整除该数。

自己跑程序随机测试1000w个数,最多的一个需要判断512次,平均判断次数。

所以认为这种暴力在随机数据下可以跑得飞快,而即便全是此次测试的极限数据,在题目的10000组数据条件下依然可以在时限内通过该题。

附上代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #define N 101000
 6 #define ll long long
 7 using namespace std;
 8 ll check(ll x)
 9 {
10     int i,j,k;
11     ll sum=0,p=x;
12     while(p)
13     {
14         sum+=p%10;
15         p/=10;
16     }
17     return x%sum;
18 }
19 int main()
20 {
21 //    freopen("1.in","r",stdin);
22
23     int g;
24     ll x;
25     for(scanf("%d",&g);g--;)
26     {
27         cin>>x;
28         while(check(x))x++;
29         cout<<x<<endl;
30     }
31     return 0;
32 }
时间: 2024-08-09 23:56:16

【SPOJ】SPCQ - Gopu and Digits Divisibility 数位处理的相关文章

SPOJ MYQ10 10649. Mirror Number (数位dp)

SPOJ MYQ10 10649. Mirror Number (数位dp) ACM 题目地址:SPOJ MYQ10 Mirror Number 题意: 求[a,b]中镜像回文的个数. 0 <= a<=b <= 10^44 分析: 看到题目和数据范围就知道是数位dp了. 很明显镜像回文只有0,1,8,跟回文的一题一样,在dfs的时候得开个辅助数组记录前面已经选择的数字. 注意还得去掉前导0. 代码: /* * Author: illuz <iilluzen[at]gmail.com

SPOJ BALNUM Balanced Numbers 状压+数位DP

一开始想了一个用二进制状压的方法,发现空间需要的太大,光光memset都要超时 = = 其实不用每次都memset 也可以用三进制,一开始直接打表出所有的状态转移就好 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream&g

spoj IITWPC4F - Gopu and the Grid Problem 线段树

IITWPC4F - Gopu and the Grid Problem no tags Gopu is interested in the integer co-ordinates of the X-Y plane (0<=x,y<=100000). Each integer coordinate contain a lamp, initially all the lamps are in off mode. Flipping a lamp means switching it on if

SPOJ NUMTSN NUMTSN - 369 Numbers(数位dp)

NUMTSN - 369 Numbers no tags 7. 369 numbers A number is said to be a 369 number if The count of 3s is equal to count of 6s and the count of 6s is equal to count of 9s. The count of 3s is at least 1. For Example 12369, 383676989, 396 all are 369 numbe

SPOJ IITWPC4F - Gopu and the Grid Problem (双线段树区间修改 区间查询)

Gopu and the Grid Problem Gopu is interested in the integer co-ordinates of the X-Y plane (0<=x,y<=100000). Each integer coordinate contain a lamp, initially all the lamps are in off mode. Flipping a lamp means switching it on if it is in off mode a

SPOJ CPCRC1C Sum of Digits

题目连接 题意:计算从a到b每个数每位数字相加的和 code: #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; typedef unsigned long long ll; ll cnt[16]; //cnt[i]表示0 到 最大n位数的个位数的和 ll s[16]; //s[i]表示10的i次方 ll num[1

hdu5564--Clarke and digits(数位dp+矩阵快速幂)

// hdu 5564 // 考虑dp,令d(i,j,k)表示长度为i第i位为j余数为k的方案数 // 则d(1,j,j%7) = 1, 0<j<10 d(i+1,x,(k*10+x)%7)+=d(i,j,k) // 发现转移相同,所以我们用矩阵快速幂来计算即可. // dp[k][j] 首位为j 余数为k的方案数 // 把dp写成一维 dp[k*10+j] #include <bits/stdc++.h> using namespace std; typedef long lon

SPOJ RAONE(数位dp)

RAONE - Ra-One Numbers no tags In the War between good and evil . Ra-One is on the evil side and G-One on the good side. Ra-One is fond of destroying cities and its G-one's duty to protect them.. Ra-One loves to destroy cities whose Zip Code has spec

SPOJ LUCIFER (数位dp)

LUCIFER - LUCIFER Number no tags Lucifer is the only human whi has defeated RA-ONE in a computer game .. RA-One is after lucifer for revenge and G-One is there to protect him ... All thi G-One and Ra-one Nonsense has disturbed lucifers life.. He want